0

I am trying to populate an initial select box with results from mysql via php. Then I would like the second select box to update with additional information related to what was chosen in the first box.

Here I am selecting some campaign names, then in the second box i would like to update with the versions of the campaign stored in mysql.

here is the name script:

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js">  </script>
    <script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

    <script type="text/javascript">
        $(document).ready(function(){
            $("#campaign").change(function(){
                 var campaign = $("#campaign").val();
                 $.ajax({
                    type:"post",
                    url:"getversion.php",
                    data:"campaign="+campaign,
                    success: function(data) {
                      $("#version").html(data);
                    }
                 });
            });
       });
    </script>
 </head>
 <body>

    Campaign :
    <select name="campaign" id="campaign">
      <option>-Select a Campaign-</option>
    <?php 
    include "db_conn.php"; 
    $result = mysql_query("SELECT campaign, time FROM dfa_data GROUP BY campaign");
    while($row = mysql_fetch_array($result)){

    echo "<option value=$row[campaign]>$row[campaign]</option>";

    } ?>
    </select>


    Version :
    <select name="version" id="version">
        <option>-Select a Version-</option>
    </select>
  </body>
</html>

then there is another script that pulls in the second select box data, although it does not populate and I do not have any idea why.

<?php
include "db_conn.php";

$campaign = $_POST["campaign"];

$result = mysql_query("SELECT * FROM dfa_data where campaign='$campaign' GROUP BY time");
   while($rowa = mysql_fetch_array($result)){
     echo"<option value=$rows[time]>$rows[time]</option>";

   }
?>

Can anyone show me what I am doing wrong and why the second select box will not populate. Thanks in advance.

1

2 Answers 2

2

Not sure if this is your issue, but it's probably AN issue. In your second script you have:

while($rowa = mysql_fetch_array($result)){
  echo"<option value=$rows[time]>$rows[time]</option>";
}

You are fetching into $rowa, but trying to access $rows. Try this instead.

while($row = mysql_fetch_array($result)){
   echo '<option value="'.$row['time'].'">'.$row['time'].'</option>';
}
Sign up to request clarification or add additional context in comments.

2 Comments

The syntax change in both scripts fixed the issue. Thank you very much.
You're welcome. Sometimes is just takes a second pair of eyes. :)
0

I think rowa and rows are the errors. Try row instead of both

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.