简体   繁体   English

从jquery ajax调用中获取数据回php以在将来的SQL查询中使用

[英]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. 我希望从下拉框中获取所选字段,以便在以后的下拉框中使用它,但是我无法弄清楚如何通过ajax将变量回显到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:

<?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:

<?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? 您的ajax响应是否包含预期的选项字符串?

Modifying the innerHTML of a select does not work in IE. 修改选择的innerHTML在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. 在你的ajax成功函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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