PHP 函数在 PreventDefault() 之后对提交的响应不同
我正在使用 PHP 文件(作为 wordpress 插件的一部分),其中包含多个函数。
其中一个构建 html 表单,另一个处理提交的表单。
我已在构建表单的函数中添加了一个 submit()
函数,我在其中调用了一个异步函数。
为了使其在提交表单之前完成,我使用 preventDefault()
并在异步函数完成时调用 submit()
。
问题是,我在 preventDefault()
之后调用 submit()
会导致处理提交表单的 PHP 函数产生与原始提交不同的结果。
这是 PHP 代码:
if(isset($_POST['submit-reg']))
{
$output = sreg_process_form($atts);
return $output;
}
else
{
$data = array();
$form = simplr_build_form($data, $atts);
return $form;
}
当我不使用 preventDefault()
和 submit()
时,条件为肯定并且表单正常提交。
当我使用它们时,第二部分将被执行,并且表单将被重建而不是提交。
所以问题是: preventDefault()
和 submit()
中的什么导致 isset($_POST['submit-reg']))
是错误的,我该如何修复它?
编辑:这是JS:
$('#simplr-reg').submit(function(event)
{
event.preventDefault();
codeAddress(function(success){}); //callback from googlemaps
});
function codeAddress(callback)
{
var geocoder = new google.maps.Geocoder();
geoCodingStarted = true;
geocoder.geocode( { 'address': address}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
$("input[name=latitude]").val(latitude);
$("input[name=longitude]").val(longitude);
$('#simplr-reg').submit();
}
});
} // end of code address
I'm using a PHP file (as part of a wordpress plugin), which contains several functions.
One of them builds an html form and another one deals with the submitted form.
I've added to the function that builds the form a submit()
function in which I call an asynchronous function.
In order for it to finish before the form is submitted I'm using preventDefault()
and a call to submit()
when the async function finishes.
The problem is that my call to submit()
after preventDefault()
causes different results in the PHP function that handles the submitted form than the original submit.
Here's the PHP code:
if(isset($_POST['submit-reg']))
{
$output = sreg_process_form($atts);
return $output;
}
else
{
$data = array();
$form = simplr_build_form($data, $atts);
return $form;
}
When I don't use preventDefault()
and submit()
the condition is positive and the form is submitted normally.
When I do use them the second part is executed and the form is being rebuilt instead of submitted.
So the question is: what in preventDefault()
and then submit()
causes isset($_POST['submit-reg']))
to be false and how can I fix it?
edit: Here's the JS:
$('#simplr-reg').submit(function(event)
{
event.preventDefault();
codeAddress(function(success){}); //callback from googlemaps
});
function codeAddress(callback)
{
var geocoder = new google.maps.Geocoder();
geoCodingStarted = true;
geocoder.geocode( { 'address': address}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
$("input[name=latitude]").val(latitude);
$("input[name=longitude]").val(longitude);
$('#simplr-reg').submit();
}
});
} // end of code address
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您通过正常方法提交表单时,
submit-reg
就会被设置。当您使用 PreventDefaul() 并异步发送表单时,该值永远不会设置,因为表单永远不会提交,尽管它是异步处理的。您应该使用另一种方法来检查表单是否正在处理或构建。也许是一个隐藏字段。或页面上动态设置的另一个标志。来自 W3C
当您使用
preventDefault()
时,您可以防止这种情况发生,从而使变量“submit-reg”不存在,因为它从未被设置。来自 jQuery 站点的更多信息
When you submit the form via normal methods then
submit-reg
is set. When you use preventDefaul() and send the form asynchronously then the value is never set, because the form is never submited, altough it's processed asynchronously. You should use another method to check is the form is being processed or built. Perhaps a hidden field. or another flag set dinamicaly on the page.From the W3C
When you use
preventDefault()
you prevent this from happening making the variable ´submit-reg` unexistant because it is never set.More info from jQuery site
如果 PHP 的行为不同,那么它的输入也会不同。在这两种情况下都执行
print_r($_POST)
并看看有什么不同。 PHP 并不关心您在页面上运行了一些 Javascript。它所看到的只是一些传入的表单值。如果传入不同的值,那么它的行为就会不同。因此,请检查这些输入并找出您的 Javascript 更改表单数据的原因。
If PHP's behaving differently, then its inputs are different. do
print_r($_POST)
in both situations and see what's different. PHP couldn't care less that you ran some Javascript on the page. All it'll see are some form values coming in.If different values come in, then it'll behave differently. So check those inputs and figure out why your Javascript is changing the form data.