[英]getting null while trying to send and receive a form value using json and jquery
Here's the HTML part that may be wrong perhaps on the form statement (not sure): 这是HTML部分,可能在form语句上可能是错误的(不确定):
<div id='data'></div>
<form action="">
<input type="text" name="nomeInput" value="" />
</form>
Here's the javascript part: 这是JavaScript部分:
$(document).ready(function(){
$.post("testeBasico_1.php", {nomeInput : $('#nomeInput').val()}, function(resposta) {
for (var x = 0, tamanhoDados = resposta.dados.length; x < tamanhoDados; x++){
$('#data').append(resposta.dados[x]+'<br>');
}
//issue line
$('#data').append('<br />'+resposta.venhoDoInput);
}, "json");
});
Here's the php part: 这是php部分:
$response = (object) array(
'success' => TRUE,
'dados' => array("1", "2", "3"),
'venhoDoInput' => $_POST['nomeInput']
);
echo json_encode($response);
When I try this, I get null on 'venhoDoInput' regardless the input field being filled or not. 当我尝试此操作时,无论输入字段是否填写,我在“ venhoDoInput”上都为空。
What am I missing here? 我在这里想念什么? (it should be something very very basic), I'm just hoping that, by knowing that, I can better understand those evil code lines.
(这应该是非常非常基本的东西),我只是希望,通过了解,我可以更好地理解那些邪恶的代码行。
Thanks a lot in advance, 非常感谢提前,
Note: If I dump($_POST['nomeInput'] on the server side script, I get nothing displayed... that's probably because I'm using js to display that data into the browser. And I'm not quite sure how to debug server side here... :s 注意:如果我在服务器端脚本上转储了($ _POST ['nomeInput'],则什么也没有显示……这可能是因为我正在使用js将数据显示到浏览器中。在这里调试服务器端...:s
You are using an id selector, but the element you are trying to select does not have the id set. 您正在使用ID选择器,但是您要选择的元素没有设置ID。 Add the attribute id="nomeInput" to the input.
将属性id =“ nomeInput”添加到输入中。
Edit: Your code will submit the form on page load. 编辑:您的代码将在页面加载时提交表单。 In order to have it submit upon actual form submission, you need to wrap it a submit listener for the form.
为了使它在实际提交表单时提交,您需要将其包装为表单的提交侦听器。
HTML: HTML:
<div id='data'></div>
<form action="" id="myForm">
<input type="text" name="nomeInput" value="" />
</form>
jQuery: jQuery的:
$(document).ready(function(){
$('#myForm').submit(function() {
$.post("testeBasico_1.php", {nomeInput : $('#nomeInput').val()}, function(resposta) {
for (var x = 0, tamanhoDados = resposta.dados.length; x < tamanhoDados; x++) {
$('#data').append(resposta.dados[x]+'<br>');
}
//issue line
$('#data').append('<br />'+resposta.venhoDoInput);
}, "json");
return false;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.