jQuery Ajax 不从外部 PHP 返回变量
我有这段代码,它从表单中获取信息并将其发送到 form.php 进行处理。 form.php 回显应该输入到#success 的变量(全名:$fullname 电子邮件:$email 链接:$link 说明:$instr)。我的代码在 Firefox 和 Chrome 中有两种不同的行为方式。
在 Firefox 中,我被发送到 form.php,它会正确显示输出,但显然我不应该被发送到那里,我应该留在我的主页上并在 #success 中看到该输出。基本上,ajax 不起作用。
在 Chrome 中,ajax 确实可以工作,但只能将全名:电子邮件:链接:说明:拉入#success。本质上,jQuery 并不是通过 POST 传递变量。
mainpage.php:
<script type="text/javascript">
$(document).ready(function() {
var data = $(form).serialize();
$('#form').submit(function(event) {
$.ajax({
url: '../wp-content/themes/MC/form.php',
type: 'POST',
data: data,
success: function(result) {
$('#success').html(result);
}
});
event.preventDefault();
});
})
</script>
<div id="exchange_inside">
<div id="formdiv">
<br>
<form method="post" id="form">
Name / Nom:<input type="text" name="fullname" /><br />
E-mail / Courriel:<input type="text" name="email" /><br /> Your Daily URL / Votre URL Quotidien:<input type="text" name="link" /><br />
Voting Instructions / Instructions de Vote:<textarea name="instr" style="width: 450px; height: 100px;"/></textarea><br />
<input type="submit" value="Submit" />
</form>
</div>
</div>
<div id="success">
</div>
表单.php:
<?php
if (isset($_POST['fullname'])){
$fullname = $_POST['fullname'];
}else{
echo "No name";
}
if (isset($_POST['email'])){
$email = $_POST['email'];
}else{
echo "No email";
}
if (isset($_POST['link'])){
$link = $_POST['link'];
}else{
echo "No link";
}
if (isset($_POST['instr'])){
$instr = $_POST['instr'];
}else{
echo "No instructions";
}
echo "Name: " .$fullname. " E-mail: " .$email. " Link: " .$link. " Instructions: " .$instr;
?>
I have this code that takes info from a form and sends it to form.php where it is processed. form.php echoes the variables (Fullname: $fullname E-mail: $email Link: $link Instructions: $instr) which are supposed to be inputted to #success. My code behaves two different ways in firefox and chrome.
In Firefox, I am sent to form.php which displays the output properly but obviously I shouldn't be sent there, I should stay on my main page and see that output in #success. Basically, the ajax doesn't work.
In Chrome, the ajax does work but only pulls Fullname: Email: Link: Instructions: into #success. In essence, the jQuery is not passing the variables via POST.
mainpage.php:
<script type="text/javascript">
$(document).ready(function() {
var data = $(form).serialize();
$('#form').submit(function(event) {
$.ajax({
url: '../wp-content/themes/MC/form.php',
type: 'POST',
data: data,
success: function(result) {
$('#success').html(result);
}
});
event.preventDefault();
});
})
</script>
<div id="exchange_inside">
<div id="formdiv">
<br>
<form method="post" id="form">
Name / Nom:<input type="text" name="fullname" /><br />
E-mail / Courriel:<input type="text" name="email" /><br /> Your Daily URL / Votre URL Quotidien:<input type="text" name="link" /><br />
Voting Instructions / Instructions de Vote:<textarea name="instr" style="width: 450px; height: 100px;"/></textarea><br />
<input type="submit" value="Submit" />
</form>
</div>
</div>
<div id="success">
</div>
form.php:
<?php
if (isset($_POST['fullname'])){
$fullname = $_POST['fullname'];
}else{
echo "No name";
}
if (isset($_POST['email'])){
$email = $_POST['email'];
}else{
echo "No email";
}
if (isset($_POST['link'])){
$link = $_POST['link'];
}else{
echo "No link";
}
if (isset($_POST['instr'])){
$instr = $_POST['instr'];
}else{
echo "No instructions";
}
echo "Name: " .$fullname. " E-mail: " .$email. " Link: " .$link. " Instructions: " .$instr;
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试通过将
event.preventDefault();
移至ajax
调用之前稍微更改一下 jquery 并修复var data = $("#form" ).serialize();
:在
$(document).ready
末尾添加分号以方便衡量。如果
ajax
由于某种原因失败或在ajax
调用中出现错误,则在ajax
调用之前移动event.preventDefault()
会阻止表单提交。ajax
调用。主要是使用这种表单提交方法,您需要首先停止表单的默认行为。Try changing up the jquery a bit by moving up
event.preventDefault();
to before theajax
call and fix yourvar data = $("#form").serialize();
:Put a semicolon on the end of
$(document).ready
for good measure.Moving
event.preventDefault()
before theajax
call stops the form from submitting if theajax
fails for some reason or there is an error in theajax
call. Primarily with this method of form submit, you want to stop the default behavior of the form first.尝试去掉
$('#form').submit
,它在不使用 ajax 的情况下提交表单,如下所示:编辑:
我误会了。尝试在按下提交后序列化数据,而不是在页面加载时序列化数据,以解决 Chrome 问题。
您可能还想尝试将提交按钮更改为常规按钮并将 ajax 绑定到其 onclick 事件。这将避免在没有ajax的情况下提交表单的问题,并修复Firefox问题。
Try getting rid of the
$('#form').submit
, which is submitting the form without ajax, like this:Edit:
I misunderstood. Try serializing the data after submit has been pressed, rather than when the page loads, to fix the Chrome problem.
You may also want to try changing the submit button to a regular button and binding the ajax to its onclick event. This would avoid problems with the form submitting without ajax, and fix the Firefox problem.
希望有帮助
Hope that helps