AJAX PHP函数的onchange选择框中 [英] AJAX PHP function onchange select box

查看:101
本文介绍了AJAX PHP函数的onchange选择框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,对此,我感到很困惑。我有一个动态生成的S使用的mysqli查询选择框:

I have a problem about which I am very confused. I have a select box with s dynamically generated using a mysqli query:

$result = mysqli_query($db, "SELECT * FROM `users` WHERE `user_id` > 0");
echo '<html><form name="contacts" method="post"><select name="contacts"><option value="Contact list" onchange="func()">Contact List</option>';
while($row = $result->fetch_assoc()){
    echo '<option value = '.$row['user_name'].'>'.$row['user_name'] . '</option>';
}
echo '</select></form>';

我是完全新的AJAX,但我需要使用jQuery和Ajax在以后的查询中THIS.VALUE变量传递给一个PHP变量使用。

I am completely new to AJAX, but I need to use jquery and ajax to pass the this.value variable to a php variable for use in a later query.

下面是我的脚本(其中大部分是在网上找到):

Here is my script (most of which was found online):

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
$("#contacts").change(function() {
    //get the selected value
    var selectedValue = this.value;

    //make the ajax call
    $.ajax({
        url: 'function.php',
        type: 'POST',
        data: {option : selectedValue},
        success: function() {
            console.log("Data sent!");
        }
    });
});
</script>

现在,当我点击选择框的值,没有任何反应。没有任何警告或错误,等等。

Now, when I click a value in the select box, nothing happens. There are no warnings or errors, etc.

请帮我。

P.S。 function.php确实存在。这仅仅是一个简单的回显现在(用于测试目的)

p.s. function.php does exist. It is just a simple echo for now (for testing purposes)

更新:这里是FUNCION.PHP:

UPDATE: HERE IS FUNCION.PHP:

<?php
/*$val = $_REQUEST['selectedValue'];
echo $val;*/

function function(){
$val = $_REQUEST['selectedValue'];
echo $val;
}
?>

更新:谢谢大家对你的帮助。我现在已经得到它的工作在镀铬的网段检查显示被请求的function.php,但是我仍然没有得到回音(我用外部的.js文件才能正常工作)。我Ĵ查询功能也是成功(成功的功能相呼应到控制台)

UPDATE: Thank you everyone for all your help. I have now got it to work in that the network section of chrome inspect shows the function.php being requested however I still don't get the echo (I used external .js files to get it to work). My J query function is also successful (the success function echoes into the console)

推荐答案

您的选择框没有ID,你正在看变更 事件$(#联系人)

Your select box has no ID and you are watching the change event of $("#contacts").

修改

echo '<html><form name="contacts" method="post"><select name="contacts"><option value="Contact list" onchange="func()">Contact List</option>';

echo '<html><form name="contacts" method="post"><select name="contacts" id="contacts"><option value="Contact list">Contact List</option>';
                                                                        ^^^^^^^^^^^^^ here

您还只需要一个事件处理程序,所以我已经删除了内嵌其中一个似乎不反正做任何事情。

You also only need one event handler, so I have removed the inline one which doesn't seem to do anything anyway.

编辑::如果选择正在使用AJAX,以及创建,您需要事件委派:

If the select is created using ajax as well, you need event delegation:

$("body").on('change', '#contacts', function() {
   ^^^^ for example

编辑2:您的变量称为 $ _ REQUEST ['选项'] ,而不是 $ _ REQUEST ['了selectedValue '] 。你也不能要求你的-badly命名 - 功能,使你不会得到任何PHP的输出,除了从像解析错误错误:语法错误,意想不到的'功能'...

Edit 2: Your variable is called $_REQUEST['option'] and not $_REQUEST['selectedValue']. You are also not calling your -badly named - function so you will not get any output from php except from an error like Parse error: syntax error, unexpected 'function' ....

这篇关于AJAX PHP函数的onchange选择框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆