简体   繁体   English

如何从PHP提取返回数据以提供给我的AJAX POST调用函数?

[英]How do I pull in the return data from PHP to give to my AJAX POST call function?

Okay, this one is a bit confusing. 好的,这有点令人困惑。

I have a form that submits to a database. 我有一个提交到数据库的表格。 I use the AJAX POST function to do so. 我使用AJAX POST函数来做到这一点。 Within AJAX the success function needs to have an if statement that says "If the data returned by the PHP file is true clear out the form, if its false leave intact" .. I've been trying to fix this for a while now and I am unable to. 在AJAX中,成功函数需要有一个if语句,该语句表示“如果PHP文件返回的数据是正确的,则清除表格,如果错误保留完好无损” ..我已经尝试了一段时间,现在,我做不到 I know that from my PHP file I can echo something correctly. 我知道从我的PHP文件中,我可以正确地回声。 But if I return FALSE then the AJAX success function doesn't take that. 但是,如果我返回FALSE,则AJAX成功函数不会接受该功能。

Here is my AJAX POST call code: 这是我的AJAX POST呼叫代码:

$.post("addCustomer.php",
                    {...DATA...} ,
                    function(data){
                        if (data){
                            $("#myForm").trigger("reset");
                        }
                    });

Here is my PHP file: 这是我的PHP文件:

public function createMember(...DATA...){
        $mysql = new Mysql();
        $cuResult = $mysql->addNewCustomer(...DATA...);

                    if ($cuResult){
                        return true;
                    } else {
                        return false;
                    }

            }

As you can tell, I removed some unnecessary info. 如您所知,我删除了一些不必要的信息。 Everything works, except my IF statement in my POST call... 一切正常,除了POST调用中的IF语句...

I might be doing it wrong. 我可能做错了。 What's the right way to do this? 什么是正确的方法?

I would suggest to return an json string with the necessary information. 我建议返回带有必要信息的json字符串。

header('Content-Type', 'application/json');
echo json_encode(['success' => createMember(...)]);
exit();

and in js you can simply use something like this 在js中,您可以简单地使用类似这样的内容

$.post("addCustomer.php",
    {...DATA...} ,
    function(data){
        if (data.success){
            $("#myForm").trigger("reset");
           }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当它们不是来自HTML表单时,如何将PHP POST和FILES数据发送到AJAX URL调用? - How do I send PHP POST and FILES data to an AJAX URL call when they are not from an HTML form? 如何从ajax调用返回数据到php - how to return data from ajax call to php 如何在Django中通过ajax调用从外部脚本中提取数据? - How do I pull data from an external script with an ajax call in Django? 如何使用return函数之外的Ajax POST调用中返回的数据? - How can I use data returned in an Ajax POST call outside the return function? 我正在尝试使用 ajax POST 方法从 html 页面将数据发送到我的 PHP 页面,但它给出错误,注意:未定义索引: - I am trying to send data to my PHP page from html page using ajax POST method but it give error ,Notice: Undefined index: 我正在尝试通过AJAX调用返回创建一个结果数组。 我怎么做? - I'm trying to create an array of results from my return on an AJAX call. How do I do that? 如何从PHP的AJAX帖子返回数据? - How to return data from an AJAX post from PHP? 在我的情况下,如何在 AJAX 回调 function 之外访问从 ajax 调用返回的数据? - How can I access data returned from ajax call outside AJAX callback function in my case? 如何通过AJAX将数据从AJAX PHP结果传递到单独的PHP函数? - How do I pass data from an AJAX PHP result to a seperate PHP function through AJAX? 如何获取Ajax返回函数并阻止文档下载onclick - how do I get my Ajax to return a function and prevent my document from downloading onclick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM