-1

Hmm I really don't get it but my code somehow returns an empty string? Here's the code:

 <form id="form1" name="form1" method="post" action="">
  <p>

  </p>
  <p>
    <select name="lbox" id="listbox">
    <?php

        $sqlString = "SELECT * FROM admintbl";
        $con = mysql_connect("localhost","root","");
        mysql_select_db("dbmain", $con);
        $dat = mysql_query($sqlString);
        $user = "";
        while ($row_i = mysql_fetch_assoc($dat)) { 

            if ($user !== $row_i['user']) {
                $user = "";
            }

            if ($user == "") {
                $user = $row_i['user'];
            ?>
            <option value=<?php $user ?>><?php echo $user ?></option>";
            <?php
            }
            ?>
        <?php }

        echo mysql_error();
        mysql_close($con);

    ?>
    </select>
    <input type="submit" id="submit" name="submit"/>
  </p>
</form>

And here is where I am trying to get the value from the listbox but is empty.

<?php

    if (isset($_POST['submit'])) {
        $check = $_POST['lbox'];
        echo "<script> alert('$check')</script>";
        if ($check == "mac") {
            echo "<script> alert('this message')</script>";
        }

    }   
?>

Any idea on what to do or where am I wrong? I just cant really get it why. The "mac" is set to be one of the values in the option in listbox. Thanks and more power to you guys!

  • 1
    You didn't `echo` your option values. Also, there's a trailing `";` on the option line which shouldn't be here. Also also, [please don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – roberto06 Feb 10 '17 at 13:07
  • What is this logic `if ($user !== $row_i['user']) {$user = "";....if ($user == "") { $user = $row_i['user'];`? Just do `$user = $row_i['user']`. You also should quote your attribute value, that could be an issue in the future. – chris85 Feb 10 '17 at 13:10
  • Thanks alot! Jeez! and that's what I "just" overlooked? In anycase. If mysql_* is depreciated should I replace them with mysqli_*? @roberto06 – John Cedrick Agapito Feb 10 '17 at 13:12
  • @chris85 those were there to check if in the table the user is repeated. If it is then it will just continue the loop until the last placed value in user is not equal to what in the database then insert in again to list box avoiding repetition of items in the list box. – John Cedrick Agapito Feb 10 '17 at 13:18
  • If it is not equal you set it to empty, if it is empty you then set it to the value. Just set it to the value. – chris85 Feb 10 '17 at 13:20
  • @JohnCedrickAgapito You could use either [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`PDO`](http://php.net/manual/en/book.pdo.php). I strongly recommand you read the documentation before switching libraries. – roberto06 Feb 10 '17 at 13:23
  • thanks @roberto06 I'll prolly do that when time is sufficient. – John Cedrick Agapito Feb 11 '17 at 06:35
  • @chris85 hm? I need the value to be empty again because that's where Iam basing the loop. – John Cedrick Agapito Feb 11 '17 at 06:35

1 Answers1

0

Try this code

    <form id="form1" name="form1" method="post" action="">
  <p>

  </p>
  <p>
    <select name="lbox" id="listbox">
    <?php

        $sqlString = "SELECT * FROM admintbl";
        $con = mysql_connect("localhost","root","");
        mysql_select_db("dbmain", $con);
        $dat = mysql_query($sqlString);
        $user = "";
        while ($row_i = mysql_fetch_assoc($dat)) { 

            if ($user !== $row_i['user']) {
                $user = "";
            }

            if ($user == "") {
                $user = $row_i['user'];
            ?>
            <option value=<?php echo $user; ?>><?php echo $user; ?></option>";
            <?php
            }
            ?>
        <?php }

        echo mysql_error();
        mysql_close($con);

    ?>
    </select>
    <input type="submit" id="submit" name="submit"/>
  </p>
</form>
sandip kakade
  • 1,346
  • 5
  • 23
  • 49