Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 根据ajax回调返回的数据停止表单加载_Php_Javascript_Jquery_Ajax - Fatal编程技术网

Php 根据ajax回调返回的数据停止表单加载

Php 根据ajax回调返回的数据停止表单加载,php,javascript,jquery,ajax,Php,Javascript,Jquery,Ajax,您好,我正在使用一个ajaxpost函数,在这个函数中,我发送数据,然后从PHP回调返回一些数据。根据返回的数据,我做出决定,要么继续前进,要么允许用户留在页面上进行更改 if (validateStep(step)) { if(step==1) { var data = document.getElementById('hiddenContact').value; $.post('

您好,我正在使用一个ajaxpost函数,在这个函数中,我发送数据,然后从PHP回调返回一些数据。根据返回的数据,我做出决定,要么继续前进,要么允许用户留在页面上进行更改

        if (validateStep(step))
        {
        if(step==1)
        { 
            var data = document.getElementById('hiddenContact').value;
            $.post('/app/controller/action', {'data':data}, function(returndata){if(returndata.match('not Unique'))alert('Contact source already exists'); else if(returndata.match('not posted')){alert("Not posted to the database");return false;}});
        }
        step = step + 1;
        form[0].action = '/app/controller/index/step:'+step;
        document.getElementById('step').value = step;
        form[0].submit();
        }
在这里,当返回数据与“not post”匹配时,我试图停止应用程序的运行,并抛出一个警报,然后抛出一个return false以停止操作。但我无法阻止应用程序进入下一步。虽然返回false,但操作将继续到下一步,但当我对增加步骤、设置操作和提交的最后4行进行注释时,它将保留在页面上,并引发警报


有人知道我应该怎么做才能停止提交进程吗???

AJAX调用是异步的。返回的“false”位于成功回调上,而不是显示在那里的处理程序上。因此,最后四个步骤总是在AJAX调用到达服务器之前执行。您可以通过更改ajax响应回调来解决此问题,如下所示:

if (validateStep(step))
{
    var next_step = function() {
        step = step + 1;
        form[0].action = '/app/controller/index/step:'+step;
        document.getElementById('step').value = step;
        form[0].submit();
    }

    if(step==1)
    { 
        var data = document.getElementById('hiddenContact').value;
        $.post('/app/controller/action', { 'data':data }, function(returndata) {
            if (returndata.match('not Unique')) alert('Contact source already exists');
            else if (returndata.match('not posted')) alert("Not posted to the database");
            else next_step();
        });
    }
    else next_step();
}

AJAX调用是异步的。返回的“false”位于成功回调上,而不是显示在那里的处理程序上。因此,最后四个步骤总是在AJAX调用到达服务器之前执行。您可以通过更改ajax响应回调来解决此问题,如下所示:

if (validateStep(step))
{
    var next_step = function() {
        step = step + 1;
        form[0].action = '/app/controller/index/step:'+step;
        document.getElementById('step').value = step;
        form[0].submit();
    }

    if(step==1)
    { 
        var data = document.getElementById('hiddenContact').value;
        $.post('/app/controller/action', { 'data':data }, function(returndata) {
            if (returndata.match('not Unique')) alert('Contact source already exists');
            else if (returndata.match('not posted')) alert("Not posted to the database");
            else next_step();
        });
    }
    else next_step();
}

继续下一步的过程不在
IF
语句的范围内,将始终运行

你不能这样做:

    if (validateStep(step))
    {
        if(step==1)
        { 
            var data = document.getElementById('hiddenContact').value;
            $.post('/app/controller/action', {'data':data}, function(returndata){
                if(returndata.match('not Unique')) {
                    alert('Contact source already exists');

                    step = step + 1;
                    form[0].action = '/app/controller/index/step:'+step;
                    document.getElementById('step').value = step;
                    form[0].submit();

                } else if (returndata.match('not posted')){
                    alert("Not posted to the database");
                    return false;
                }
            });
        }
    }

继续下一步的过程不在
IF
语句的范围内,将始终运行

你不能这样做:

    if (validateStep(step))
    {
        if(step==1)
        { 
            var data = document.getElementById('hiddenContact').value;
            $.post('/app/controller/action', {'data':data}, function(returndata){
                if(returndata.match('not Unique')) {
                    alert('Contact source already exists');

                    step = step + 1;
                    form[0].action = '/app/controller/index/step:'+step;
                    document.getElementById('step').value = step;
                    form[0].submit();

                } else if (returndata.match('not posted')){
                    alert("Not posted to the database");
                    return false;
                }
            });
        }
    }

这假设您只在不想继续时返回false(不再需要)。因此,“非唯一”将继续,而“未发布”则不会。这假设您只在不想继续时返回false(不再需要)。所以“非唯一”继续进行,而“未发布”则没有。你好,李,我正在尝试停止“未发布”案例的处理。因此,如果我调用next_-step函数,它无论如何都会执行提交部分,不是吗???@macha,
next_-step()
会执行提交过程,但请注意,在我发布的代码中,如果
if
语句,它位于
else
子句中。因此,只有在前两个条件匹配时,它才会发布。如果reutrn不是唯一的,您是否也希望它不发布?我发布的代码假设在这些错误条件中的任何一种情况下,表单提交都将被阻止。Hello Lee,我正在尝试停止“未发布”案例的处理。因此,如果我调用next_-step函数,它无论如何都会执行提交部分,不是吗???@macha,
next_-step()
会执行提交过程,但请注意,在我发布的代码中,如果
if
语句,它位于
else
子句中。因此,只有在前两个条件匹配时,它才会发布。如果reutrn不是唯一的,您是否也希望它不发布?我发布的代码假设在这些错误条件下,表单提交都会被阻止。