I am validating a form with ajax and jquery in WordPress post comments textarea for regex. But there is an issue when i want to alert a error message with return false. Its working fine with invalid data and showing alert and is not submitting. But when i put valid data then form is not submit. May be issue with return false.
我正在用ajax和jquery为regex在WordPress post comments textarea中验证一个表单。但是当我想要用return false警告错误消息时,会有一个问题。它工作良好,数据无效,显示警报,不提交。但是,当我输入有效数据时,表单就不会提交。可能是返回错误的问题。
I tried making variable and store true & false and apply condition out the ajax success block but did not work for me.
我尝试将变量和true & false存储起来,并将条件应用到ajax的成功块中,但没有成功。
Its working fine when i do it with core php, ajax, jquery but not working in WordPress .
当我使用核心php、ajax和jquery时,它工作得很好,但是在WordPress中不工作。
Here is my ajax, jquery code.
这是我的ajax jquery代码。
require 'nmp_process.php';
add_action('wp_ajax_nmp_process_ajax', 'nmp_process_func');
add_action('wp_ajax_nopriv_nmp_process_ajax', 'nmp_process_func');
add_action('wp_head', 'no_markup');
function no_markup() {
?>
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery('form').submit(function (e) {
var comment = jQuery('#comment').val();
jQuery.ajax({
method: "POST",
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: 'action=nmp_process_ajax&comment=' + comment,
success: function (res) {
count = res;
if (count > 10) {
alert("Sorry You Can't Put Code Here.");
return false;
}
}
});
return false;
});
});
</script>
<?php
}
And i'm using wordpress wp_ajax hook.
我使用wordpress wp_ajax挂钩。
And here is my php code.
这是我的php代码。
<?php
function nmp_process_func (){
$comment = $_REQUEST['comment'];
preg_match_all("/(->|;|=|<|>|{|})/", $comment, $matches, PREG_SET_ORDER);
$count = 0;
foreach ($matches as $val) {
$count++;
}
echo $count;
wp_die();
}
?>
Thanks in advance.
提前谢谢。
3 个解决方案
#1
3
Finally, I just figured it out by myself.
最后,我自己算出来了。
Just put async: false
in ajax call. And now it is working fine. Plus create an empty variable and store Boolean values in it and then after ajax call return that variable.
在ajax调用中输入async: false。现在它运行得很好。加上创建一个空变量并在其中存储布尔值,然后在ajax调用之后返回该变量。
Here is my previous code:
以下是我之前的代码:
require 'nmp_process.php';
add_action('wp_ajax_nmp_process_ajax', 'nmp_process_func');
add_action('wp_ajax_nopriv_nmp_process_ajax', 'nmp_process_func');
add_action('wp_head', 'no_markup');
function no_markup() {
?>
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery('form').submit(function (e) {
var comment = jQuery('#comment').val();
jQuery.ajax({
method: "POST",
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: 'action=nmp_process_ajax&comment=' + comment,
success: function (res) {
count = res;
if (count > 10) {
alert("Sorry You Can't Put Code Here.");
return false;
}
}
});
return false;
});
});
</script>
<?php
}
And the issue that i resolved is,
我解决的问题是,
New code
新代码
var returnval = false;
jQuery.ajax({
method: "POST",
url: '<?php echo admin_url('admin-ajax.php'); ?>',
async: false, // Add this
data: 'action=nmp_process_ajax&comment=' + comment,
Why i use it
为什么我使用它
Async:False
will hold the execution of rest code. Once you get response of ajax, only then, rest of the code will execute.
Async:False将保存rest代码的执行。一旦获得了ajax的响应,就只需要执行其余的代码。
And Then simply store Boolean in variable like this ,
然后把布尔值存储在这样的变量中,
success: function (res) {
count = res;
if (count > 10) {
alert("Sorry You Can't Put Code Here.");
returnval = false;
} else {
returnval = true;
}
}
});
// Prevent Default Submission Form
return returnval; });
That's it.
就是这样。
Thanks for the answers by the way.
谢谢你的回答。
#2
2
Try doing a ajax call with a click event and if the fields are valid you submit the form:
尝试使用单击事件进行ajax调用,如果字段有效,则提交表单:
jQuery(document).ready(function () {
jQuery("input[type=submit]").click(function (e) {
var form = $(this).closest('form');
e.preventDefault();
var comment = jQuery('#comment').val();
jQuery.ajax({
method: "POST",
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: {'action':'nmp_process_ajax','comment':comment},
success: function (res) {
var count = parseInt(res);
if (count > 10) {
alert("Sorry You Can't Put Code Here.");
} else {
form.submit();
}
}
});
});
});
note : you call need to call that function in php and return only the count!
注意:您调用需要在php中调用该函数,并且只返回计数!
#3
1
Instead of submitting the form bind the submit button to a click event.
将submit按钮绑定到单击事件,而不是提交表单。
jQuery("input[type=submit]").on("click",function(){
//ajax call here
var comment = jQuery('#comment').val();
jQuery.ajax({
method: "POST",
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: 'action=nmp_process_ajax&comment=' + comment,
success: function (res) {
count = res;
if (count > 10) {
alert("Sorry You Can't Put Code Here.");
return false;
}else{
jQuery("form").submit();
}
}
});
return false;
})
Plus also its a good idea to put return type to you ajax request. Let me know if this works.
另外,将返回类型放到ajax请求中也是一个好主意。如果可以的话,请告诉我。
#1
3
Finally, I just figured it out by myself.
最后,我自己算出来了。
Just put async: false
in ajax call. And now it is working fine. Plus create an empty variable and store Boolean values in it and then after ajax call return that variable.
在ajax调用中输入async: false。现在它运行得很好。加上创建一个空变量并在其中存储布尔值,然后在ajax调用之后返回该变量。
Here is my previous code:
以下是我之前的代码:
require 'nmp_process.php';
add_action('wp_ajax_nmp_process_ajax', 'nmp_process_func');
add_action('wp_ajax_nopriv_nmp_process_ajax', 'nmp_process_func');
add_action('wp_head', 'no_markup');
function no_markup() {
?>
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery('form').submit(function (e) {
var comment = jQuery('#comment').val();
jQuery.ajax({
method: "POST",
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: 'action=nmp_process_ajax&comment=' + comment,
success: function (res) {
count = res;
if (count > 10) {
alert("Sorry You Can't Put Code Here.");
return false;
}
}
});
return false;
});
});
</script>
<?php
}
And the issue that i resolved is,
我解决的问题是,
New code
新代码
var returnval = false;
jQuery.ajax({
method: "POST",
url: '<?php echo admin_url('admin-ajax.php'); ?>',
async: false, // Add this
data: 'action=nmp_process_ajax&comment=' + comment,
Why i use it
为什么我使用它
Async:False
will hold the execution of rest code. Once you get response of ajax, only then, rest of the code will execute.
Async:False将保存rest代码的执行。一旦获得了ajax的响应,就只需要执行其余的代码。
And Then simply store Boolean in variable like this ,
然后把布尔值存储在这样的变量中,
success: function (res) {
count = res;
if (count > 10) {
alert("Sorry You Can't Put Code Here.");
returnval = false;
} else {
returnval = true;
}
}
});
// Prevent Default Submission Form
return returnval; });
That's it.
就是这样。
Thanks for the answers by the way.
谢谢你的回答。
#2
2
Try doing a ajax call with a click event and if the fields are valid you submit the form:
尝试使用单击事件进行ajax调用,如果字段有效,则提交表单:
jQuery(document).ready(function () {
jQuery("input[type=submit]").click(function (e) {
var form = $(this).closest('form');
e.preventDefault();
var comment = jQuery('#comment').val();
jQuery.ajax({
method: "POST",
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: {'action':'nmp_process_ajax','comment':comment},
success: function (res) {
var count = parseInt(res);
if (count > 10) {
alert("Sorry You Can't Put Code Here.");
} else {
form.submit();
}
}
});
});
});
note : you call need to call that function in php and return only the count!
注意:您调用需要在php中调用该函数,并且只返回计数!
#3
1
Instead of submitting the form bind the submit button to a click event.
将submit按钮绑定到单击事件,而不是提交表单。
jQuery("input[type=submit]").on("click",function(){
//ajax call here
var comment = jQuery('#comment').val();
jQuery.ajax({
method: "POST",
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: 'action=nmp_process_ajax&comment=' + comment,
success: function (res) {
count = res;
if (count > 10) {
alert("Sorry You Can't Put Code Here.");
return false;
}else{
jQuery("form").submit();
}
}
});
return false;
})
Plus also its a good idea to put return type to you ajax request. Let me know if this works.
另外,将返回类型放到ajax请求中也是一个好主意。如果可以的话,请告诉我。