[英]Trying to pass variable from input radio to PHP via jQuery.post()
I'm trying to load a html table created by a php code. 我正在尝试加载由php代码创建的html表。 The table should be generated by an sql query that depends on a variable from a radio input selector, but somehow i can't pass that variable via jQuery.post().
该表应由依赖于单选输入选择器中变量的sql查询生成,但是以某种方式我无法通过jQuery.post()传递该变量。 I made a simple version that has the same issue so I hope someone can help me with that:
我制作了一个具有相同问题的简单版本,因此希望有人可以帮助我:
test.php: test.php的:
<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
$("input[name$='selectOP']").on("change", function() {
var op = $(this).val();
$.post('ajax.php', {varphp: op});
$("#div1").load('ajax.php', function(){
});
});
});
</script>
<body>
<label class="radio-inline"><input type="radio" value="110" name="selectOP" id="selectOP">110 </label>
<label class="radio-inline"><input type="radio" value="115" name="selectOP" id="selectOP">115 </label>
<div id="div1">
</div>
</body>
</html>
ajax.php: ajax.php:
<?php
$var= "Something";
echo $var;
//$varphp = $_POST['varphp'];
//echo $varphp;
?>
So, with the two last lines of ajax.php commented, code successfully runs and the var $var loads inside div1. 因此,用ajax.php的最后两行注释后,代码成功运行,并且var $ var加载到div1中。 But if i uncomment those lines, apparently the code stops, and nothing is loaded on div1.
但是,如果我取消注释这些行,则显然代码停止了,并且div1上未加载任何内容。 What am I doing wrong?
我究竟做错了什么?
$.post('ajax.php', {varphp: op});
$("#div1").load('ajax.php', function(){
$});
These two lines are actually making two separate ajax requests, which means the server will pick them up separately. 这两行实际上是在发出两个单独的ajax请求,这意味着服务器将分别接收它们。 Only the first one has a POST parameter, and only the second one sets the content of the DOM object.
只有第一个具有POST参数,只有第二个才设置DOM对象的内容。
Try something like this: 尝试这样的事情:
$("#div1").load('ajax.php', {
varphp: op
});
You are calling $.post where you pass the argument and then calling load with no arguments. 您在传递参数的位置调用$ .post,然后不带参数调用load。 Of course you can't get $_POST['varphp'] when you call 'load' because you are not passing this variable.
当然,当您调用“ load”时,您不会获得$ _POST ['varphp'],因为您没有传递此变量。
you must use one of them. 您必须使用其中之一。 you can do with this:
您可以这样做:
$.post('ajax.php', {varphp: op}, function(data){ $("#div1").html(data); }, 'html' );
or 要么
$("#div1").load('ajax.php', {varphp: op});
but not both 但不是两个
thus, your code can be 因此,您的代码可以是
$(document).ready(function(){ $(文件)。就绪(函数(){
$("input[name$='selectOP']").on("change", function() {
var op = $(this).val();
$.post('ajax.php', {varphp: op}, function(data){ $("#div1").html(data); }, 'html' );
});
});
or 要么
$(document).ready(function(){ $(文件)。就绪(函数(){
$("input[name$='selectOP']").on("change", function() {
var op = $(this).val();
$("#div1").load('ajax.php', {varphp: op});
});
});
A good practice I like is to use AJAX w/ deferred promises. 我喜欢的一个好习惯是使用带有延期承诺的AJAX。 You can try converting your code to the following:
您可以尝试将代码转换为以下代码:
$.ajax({
type: 'post', // you can switch to GET, POST, etc.
url: 'ajax.php',
data: {varphp: op},
})
.done(function(data) {
$('#div1').html(data);
})
.fail(function(data) {
// if your code fails, you can see the errors here in the console
console.log(data);
})
.always(function(data) {
// do something every time
});
The codes within the done
, fail
, or always
callback functions will run depending on whether the AJAX call is a success, fail, or neither respectively. done
, fail
或always
回调函数中的代码将运行,具体取决于AJAX调用是成功,失败还是两者都不是。 With this, you can troubleshoot your code easily as well, especially when the AJAX call error'd out (look inside the fail
function). 这样,您也可以轻松地对代码进行故障排除,尤其是当AJAX调用错误结束时(请查看一下
fail
函数)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.