如何通过Ajax获取php返回值?

时间:2022-12-01 16:31:32

Im creating a validation form in Ajax and PHP. But i don't have a clue how i should get the value from PHP??

我在Ajax和PHP中创建验证表单。但我不知道我应该如何从PHP中获取值?

For example:

The validation form is in index.php And the page with the function is checkUser.php. In checkUser i have a global file included with my classes initialized. The checkUser.php look like this:

验证表单在index.php中,带有该函数的页面是checkUser.php。在checkUser中,我有一个全局文件,包含在我的类中初始化。 checkUser.php看起来像这样:

<?php

$requser = false;
require "core/rules/glb.php";

$user->checkUser($_GET['username']);

The get function comes from the Ajax call i do in the index file. But how do i know that PHP said that the username already exist så that i can make a if statement and paus the script?

get函数来自我在索引文件中执行的Ajax调用。但我怎么知道PHP说用户名已经存在,我可以制作一个if语句和paus脚本?

Im a beginner, thanks.

我是初学者,谢谢。

And sorry for my english

抱歉我的英语

                                        $.ajax({

                                            type: "GET",
                                            url: "user_add.php",
                                            data: 'username='+$("#jusername").val()+'&email='+$("#jemail").val()+'&password='+$("#jpassword").val()+'&secureSession=23265s"',
                                            success: function()
                                            {
                                                location.href='register.php';
                                            }
                                        });

2 个解决方案

#1


0  

Jus print out the data, for better help also post the ajax script

Jus打印出数据,为了更好的帮助也发布了ajax脚本

<?php

    $requser = false;
    require "core/rules/glb.php";

    print $user->checkUser($_GET['username']);

#2


0  

If you are trying to give a response to the ajax call from php, then you can do it via normal output. Just like

如果你试图回复来自php的ajax调用,那么你可以通过正常输出来做到这一点。就像

echo json_encode(array("status"=>"FAIL"));
exit();

will send a json response to the ajax call from the php script. like

将从php脚本发送一个json响应给ajax调用。喜欢

{"status":"FAIL"}

which you can parse it at the ajax callback and check the status. like

您可以在ajax回调中解析它并检查状态。喜欢

var data = JSON.parse(response);
if(data.status == "FAIL") {
     alert("Ajax call returned failed");
}

#1


0  

Jus print out the data, for better help also post the ajax script

Jus打印出数据,为了更好的帮助也发布了ajax脚本

<?php

    $requser = false;
    require "core/rules/glb.php";

    print $user->checkUser($_GET['username']);

#2


0  

If you are trying to give a response to the ajax call from php, then you can do it via normal output. Just like

如果你试图回复来自php的ajax调用,那么你可以通过正常输出来做到这一点。就像

echo json_encode(array("status"=>"FAIL"));
exit();

will send a json response to the ajax call from the php script. like

将从php脚本发送一个json响应给ajax调用。喜欢

{"status":"FAIL"}

which you can parse it at the ajax callback and check the status. like

您可以在ajax回调中解析它并检查状态。喜欢

var data = JSON.parse(response);
if(data.status == "FAIL") {
     alert("Ajax call returned failed");
}