user1763208
user1763208

Reputation: 47

dynamically updating select boxes with php mysql ajax

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.

Upvotes: 0

Views: 5387

Answers (2)

Talha
Talha

Reputation: 1

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

Upvotes: 0

Revent
Revent

Reputation: 2109

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>';
}

Upvotes: 2

Related Questions