jquery向php传输数据的问题
我想使用 Ajax 从 #f1 (.Val()) 获取数据并通过 PHP (echo) 显示它,它在 Ajax 中成功,但我无法在 PHP 中获取数据。 它总是说:假的。这是代码:(第86~93行不重要)。有人可以帮忙吗...?
<form action="index.php" method="post" id="submit-form">
<div class="d4">
username:
</div>
<div class="d5">
<input type="text" , class="in" , name="username" placeholder="username" id="f1">
</div>
<div class="d4">
password:
</div>
<div class="d5">
<input type="text" , class="in" , name="sing2" placeholder="password">
</div>
<div class="d4">
email:
</div>
<div class="d5">
<input type="email" , class="in" , name="sing3" placeholder="email">
</div>
<div class="d5">
<input type="button" , class="in1" , name="sing4" value="submit">
</div>
</form>
<script type="text/javascript">
$("#f1").keypress(function(){
$.ajax({
type:'POST',
url:'index.php',
data:{
uen:$("#f1").val(),
},
success: function(data) {
alert($("#f1").val());
},
failure: function(data) {
alert("0");
},
});
});
});
</script>
<?php
if(isset($_POST['uen'])){
echo $_POST['uen'];
}else{
echo 'false';
}
?>
I want to use Ajax to get data from #f1 (.Val()) and show it by PHP (echo) and it is successful in Ajax but I cant get data in PHP.
and it always says : false. here is the code:(line 86~93 is not important). can anybody help please...?
<form action="index.php" method="post" id="submit-form">
<div class="d4">
username:
</div>
<div class="d5">
<input type="text" , class="in" , name="username" placeholder="username" id="f1">
</div>
<div class="d4">
password:
</div>
<div class="d5">
<input type="text" , class="in" , name="sing2" placeholder="password">
</div>
<div class="d4">
email:
</div>
<div class="d5">
<input type="email" , class="in" , name="sing3" placeholder="email">
</div>
<div class="d5">
<input type="button" , class="in1" , name="sing4" value="submit">
</div>
</form>
<script type="text/javascript">
$("#f1").keypress(function(){
$.ajax({
type:'POST',
url:'index.php',
data:{
uen:$("#f1").val(),
},
success: function(data) {
alert($("#f1").val());
},
failure: function(data) {
alert("0");
},
});
});
});
</script>
<?php
if(isset($_POST['uen'])){
echo $_POST['uen'];
}else{
echo 'false';
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对 Ajax 请求的响应将放置在 success 函数的 data 参数中,但您忽略了它。
除非您正在检查浏览器开发人员工具的“网络”选项卡中的数据,否则您无法知道它是否返回
false
或其他内容。当您发出初始 GET 请求来呈现页面时,这段代码:
...将在 HTML 文档上呈现
false
(因为它是 GET 请求)。后续 Ajax 请求不会及时返回并更改对较早请求的响应以导致浏览器更新 DOM。
为此,您需要读取 success 函数中
data
的值,并使用 JavaScript 基于它修改 DOM。The response to the Ajax request will be placed in the
data
argument to the success function, but you are ignoring it.Unless you are examining the data in the Network tab of the browser's developer tools, you can't know if it is returning
false
or something else.When you make the initial GET request to render the page, this chunk of code:
… will render
false
on the HTML document (because it is a GET request).Subsequent Ajax requests will not travel back in time and alter the response to the earlier request to cause the browser to update the DOM.
To do that you need to read the value of
data
in the success function and use JavaScript to modify the DOM based on it.我在您的代码中看到一些错误,包括表单输入标签中的逗号。这是应该编写的代码:
jquery 代码中也有一些语法错误,正确的方法如下:
最后,修改 PHP 代码片段,使其仅解释使用“async”发送的请求post 变量,在我的示例中,这应该位于名为“ajax_request.php”的文件中,如下所示:
在用户名字段中键入内容会将“keypress”事件异步发送到 PHP 代码,并将响应发送到浏览器控制台(最初显示处于警戒状态)。
我希望对您有所帮助
I see some errors in your code, including commas in the form input tags. Here is the code as it should be written:
You also have some syntax errors in the jquery code, the correct way is the following:
At the end, modify your PHP snippet so that it only interprets requests that are sent with the "async" post variable, this in my example should be in a file called "ajax_request.php" as follows:
Typing to the username field will asynchronously send the "keypress" event to the PHP code and the response to the browser console (it was originally shown in alert).
I hope to be helpful