Mir
Mir

Reputation: 41

Generating SELECT OPTIONS with inline code?

<body>

    <H1>4a</H1>

    <form action="hw4b.php" method="post">

        <?php
            $con = mysqli_connect("localhost","[credential]","","[credential]")
            or die("Failed to connect to database " . mysqli_error());
        ?>

        <select name="id" value="id">

            <script>
                for (x=1;x<=101;x++)
                {
                    document.write("<option value="+x+">"+
                    <?php echo mysqli_query($con, "SELECT LASTNAME FROM CUSTOMERS WHERE CUSTOMERID == "+x+";")?> 
                    +"</option>");
                }
            </script>

        </select>

        <input type="submit" value="SEND IT">

    </form>

</body>

So this should put the corresponding LASTNAME into the select, but it just fills every row with "NaN". I'm sure this is some stupid minor error, but I've been staring at it too long.

Upvotes: 1

Views: 255

Answers (3)

siksolutions
siksolutions

Reputation: 11

There are several issues I think. You are using a comparison operator in the SELECT statement, it should just be =, not ==. Also, mysqli_query returns a mysqli_result, not a value like "Johnson" for LASTNAME. And, maybe most importantly, it doesn't make sense to do this with javascript since you're writing the values to the document before sending it to the browser anyway.

The code should look something like this (not tested)

<select name="id" value="id">
    <?php
    $query = 'SELECT LASTNAME, CUSTOMERID FROM CUSTOMERS WHERE CUSTOMERID >= 1 AND CUSTOMERID <= 101 ORDER BY CUSTOMERID ASC';
    $result = mysqli_query($con, $query);
    if (!$result) {
       echo 'some error handling';
    } else {
       while ($row = $result->fetch_assoc()) {
           echo '<option value="' . $row['CUSTOMERID'] . '">' . $row['LASTNAME'] . '</option>';
       }
    }
    ?>
</select>

Upvotes: 0

Ondřej Navr&#225;til
Ondřej Navr&#225;til

Reputation: 601

Note that your for cycle is in javascript (between <script> tags), yet you try to fill in some data in php.

Everything in PHP happens on server side, i.e. is interpreted, packed into a http response and returned to the client, where it is unpacked and javascript is executed.

You need to either put both into javascript, or both into php.

<select>
<?php
for ($i = 0; $i < 100; i++){
  ///make some select here
  echo "<option value="$i"> ...output the select </option>"
}
?>
</select>

This way, all options are generated on server side and transferred to client as text

<select>
 <option value="0">...</option>
 <option value="1">...</option>
 ...

Other option is to export the database data into javascript, and then access it in javascript.

<script>
//or perhaps better
var myOtherData = <?=json_encode($somePHPData)?>;
</script>
//now you can use for loop with document.write and one of the variables you exported...

You need to be very careful and sure which execution happens on server, and which on client side.

Upvotes: 1

Ahmad Alinat
Ahmad Alinat

Reputation: 141

you should query the results of mysqli_query do something like this:

  <select name="id" value="id">
 <?php 
  $query = mysqli_query($con, "SELECT LASTNAME FROM CUSTOMERS WHERE WHERE CUSTOMERID >=1 and CUSTOMERID <= 101 ;");
  while ($row = mysqli_fetch_array($query))
  echo "<option id='".$row['LASTNAME']."'>".$row['LASTNAME']."</option>";
?>
</select>

notes:

  • no need for javascript usage
  • please escape the query parameter
  • id of the option is the value that will be sent to the server, makes more since to send LASTNAME
  • avoid using a query at a loop

Upvotes: 2

Related Questions