通过 jQuery 将下拉框中的值发送到 PHP
我正在尝试从下拉列表中的两个框中获取值并将它们发送到 PHP 文件,该文件将根据所选的组合从 mySQL 数据库中绘制适当的字段,并将其显示在 div 中,而无需使用 AJAX 刷新页面。我已经整理了第二部分,但我仍停留在第一部分。
这是 HTML: http://jsfiddle.net/SYrpC/
这是我在头部的 Javascript 代码主文档的内容:
var mode = $('#mode');
function get() {$.post ('data.php', {name: form.him.value, the_key: #mode.val()},
function(output) {$('#dare').html(output).show();
});
}
我的 PHP(用于测试目的)是:
$the_key = $_POST['the_key'];
echo $the_key;
在 PHP 中将其作为变量后,我可以对其进行操作,但我无法将其获取到那里。我哪里出错了?感谢您的回复!
I'm trying to take values from a dropdown two boxes and send them to a PHP file which will draw an appropriate field from a mySQL database depending on the combination chosen and display it in a div without refreshing the page using AJAX. I have the second part sorted, but I'm stuck on the first part.
Here is the HTML: http://jsfiddle.net/SYrpC/
Here is my Javascript code in the head of the main document:
var mode = $('#mode');
function get() {$.post ('data.php', {name: form.him.value, the_key: #mode.val()},
function(output) {$('#dare').html(output).show();
});
}
My PHP (for testing purposes) is:
$the_key = $_POST['the_key'];
echo $the_key;
After I have it in PHP as a variable I can manipulate it, but I'm having trouble getting it there. Where am I going wrong? Thanks for your replies!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还需要一个回调函数来让服务器响应 POST。
$.post('ajax/test.html', 函数(数据) {
$('.结果').html(数据);
});
此代码片段将发布到 ajax/test.html,并且匿名函数将在其回复时调用具有响应的参数数据。然后,它在此匿名函数中将结果类设置为具有服务器响应的值。
帮助 ?如果您需要更多信息,请告诉我,我们可以解决这个问题。
另外,jQuery 中的 $.post 是
$.ajax({
类型:'发布',
网址: 网址,
数据:数据,
成功:成功
数据类型:数据类型
});
You need a callback function as well to have the server response to the POST.
$.post('ajax/test.html', function(data) {
$('.result').html(data);
});
This snippet will post to ajax/test.html and the anonymous function will be called upon its reply with the parameter data having the response. It then in this anonymous function sets the class with result to have the value of the server response.
Help ? Let me know and we can work through this if you need more information.
Additionally, $.post in jQuery is a short form of
$.ajax({
type: 'POST',
url: url,
data: data,
success: success
dataType: dataType
});
你的jquery选择器是错误的:
your jquery selectors are wrong:
你想在你的ajax请求中添加一个回调,这并不难,这里我什至给你一个例子:
上面的大多数回调都是可选的,在你的情况下我会做以下事情:
你应该总是设置的回调分别是
url,success
和data
如果需要,请阅读 文档了解更多信息。You want to add a callback to your ajax request, its not too hard to do, here ill even give you an example:
Most of the above callbacks are optional, and in your case i would do the following:
the ones you should always set are
url,success
anddata
if needed, please read The Documentation for more information.