0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

When I run this code it correctly populates the "Trainer" dropdown, and once a Trainer is selected, the "Pokemon" dropdown gets populated with all of the species of pokemon the trainer has, but I am unable to get the Trainer's name out to pass it to the jquery ajax to use in a SQL query to find all of the nicknames of the pokemon owned by a trainer of a certain species.
Man... completely didn't get that at all from your question. If you need to get the selected trainer name for that call, the trainer name is already on your page, and you can just get that value and use it for the ajax call. Replace $trainer_name with $('select[name="trainer_has_update_pokemon"]').val().
Where should I replace $trainer_name with $('select[name="trainer_has_update_pokemon"]').val() ?
@TimSchley There is only one $trainer_name in your above code.
@TimSchley glad I could help. just make sure you understand why it works haha.

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.