简体   繁体   中英

Get data from jquery ajax call back to php to use in future SQL queries

I am looking to get the selected field from a dropdown box in order to use it in a future dropdown box, but I can't figure out how to echo the variable through the ajax back to the html.

<p>Trainer</p>
<select name = "trainer_has_update_pokemon">
<option>Select Trainer</option>
<?php
$query = "SELECT name FROM Trainer";

if ($stmt = $mysqli->prepare($query)) {
$stmt->execute();
$stmt->bind_result($name);
while ($stmt->fetch()) {
    echo"<option>$name</option>";
}


$stmt->close();
}


?>
</select>

Pokemon

<select name = "type_of_update_pokemon">
</select>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){ 
    $('select[name="trainer_has_update_pokemon"]').change(function(){ // when trainer_has_update_pokemon changes
        $.ajax({
            type:"POST", //send a post method
            url:'pkmn_dropdown.php', // path to ajax page
            data:"trainer_name="+$(this).val(), //set trainer_name to value
            success:function(response){ // retrieve response from php
                $('select[name="type_of_update_pokemon"]').html(response); // update select
            }
        });
    });
});
</script>


<select name="nickname_of_update_pokemon">
</select>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){ 
    $('select[name="type_of_update_pokemon"]').change(function(){ // when trainer_has_update_pokemon changes
        $.ajax({
            type:"POST", //send a post method
            url:'nickname_dropdown.php', // path to ajax page
            data:"pkmn_name="+$(this).val() & "trainer_name=" +$trainer_name,//set trainer_name to value
            success:function(response){ // retrieve response from php
                $('select[name="nickname_of_update_pokemon"]').html(response); // update select
            }
        });
    });
});
</script>

the php for the name dropdown:

<?php

//connect to db

    $trainer_name = $_POST['trainer_name']; 
    $query = "SELECT DISTINCT p.name FROM Pokemon p WHERE p.owner_id = (SELECT t.trainer_id FROM Trainer t WHERE t.name = '$trainer_name')";
    if ($stmt = $mysqli->prepare($query)) {
        $stmt->execute();
        $stmt->bind_result($pkmn_name);
        while ($stmt->fetch()) {
            echo"<option>$pkmn_name</option>";
        }
        $stmt->close();
        echo $trainer_name;
    }?>

The php for the nickname dropdown:

<?php

//connect to db

$pkmn_name = $_POST['pkmn_name']; 
$query = "SELECT p.nickname FROM Pokemon p, Trainer t WHERE p.name = '$pkmn_name AND p.owner_id = t.trainer_id AND t.name = $trainer_name";
if ($stmt = $mysqli->prepare($query)) {
    $stmt->execute();
    $stmt->bind_result($nickname);
    while ($stmt->fetch()) {
        echo"<option>$nickname</option>";
    }
    $stmt->close();
}?>

Any ideas how to get the selected trainer name from the first dropdown box so i can use it in the nickname dropdown box

What exactly is happening when you run the above code? Does the nickname select lose all of it options? Does your ajax response contain the options string as expected?

Modifying the innerHTML of a select does not work in IE. You will need to add actual elements. Try something like this.

$( 'select[name="nickname_of_update_pokemon"]' ).empty().append( $( response ) );

In your ajax success function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM