weixin_33736649 2017-11-02 10:00 采纳率: 0%
浏览 36

在Ajax中测试PHP响应

I'm making a simple registration mechanism that stores username and password into a MySQL database. How it works:

  1. User enters his data into textboxes.
  2. The values get fetched and validation via JQuery script.
  3. If the validation is passed, an Ajax call is made to a PHP file that then checks if the Username has already been taken. If it's taken, it does echo '0'. If not, it does echo '1' and inserts the values into the database.
  4. Jquery then checks the response and does an appropriate action.

    $.ajax({
        type: "POST",
        url: "register.php",
        data: {usn:us, pwd: pw},
        cache: false,
        success: function(result){
            console.log(result);
            if (result=='1'){
                window.location = "next.php";
            }else{
                errorMessage= "Username taken. <br>";
                $("#regerrormsg").html(errorMessage);
            }
        }
        });
    

PHP:

include 'dbconnect.php';
    $us = $_POST["usn"];
    $pw = $_POST["pwd"];

    $tryus = $conn->query("SELECT Username from users WHERE Username='" . $us . "'");
    if ($tryus->num_rows == 0){
        echo '1';
        $conn->query("INSERT INTO users (ID, Username, Password) VALUES (DEFAULT, '" . $us . "', '" . $pw . "')");  
        session_start();
        $_SESSION["user"] = $us;
    }else{
        echo '0';
    }

Now, here's the weird thing. This pretty much works, except for one part. The query does indeed check if the username already exist and it does echo the appropriate value ('0' or '1'). I've tested this by logging the output in the javascript file. I've also checked my database and it never stores a duplicate user. But, for some weird reason, the window.location = "next.php"; is never executed, even when the value of the output is '1'. It always goes to the 'else' block. Any idea why this is happening? Thanks.

  • 写回答

2条回答 默认 最新

  • bug^君 2017-11-02 10:04
    关注

    The best way to check backend responses is to use a REST Api Client. Postman is the most popular but I prefer Insomnia. Try it out, in my opinion it's a must have for front-end development.

    评论
  • weixin_33725515 2017-11-02 12:13
    关注

    Please change below code:

    if ($tryus->num_rows == 0){
        echo '1';
        $conn->query("INSERT INTO users (ID, Username, Password) VALUES (DEFAULT, '" . $us . "', '" . $pw . "')");  
        session_start();
        $_SESSION["user"] = $us;
    }else{
        echo '0';
    }
    

    to this:

    if ($tryus->num_rows == 0){
        $conn->query("INSERT INTO users (ID, Username, Password) VALUES (DEFAULT, '" . $us . "', '" . $pw . "')");
        echo '1';
    }else{
        echo '0';
    }
    exit();
    

    Create session in variable in next.php file.

    Change window.location = "next.php"; to document.location.href="http://your-site-url.com/next.php";

    评论

报告相同问题?

悬赏问题

  • ¥15 pom文件依赖管理,未找到依赖
  • ¥15 现在后端返回给我一堆下载地址,都是一个视频切片后的,如何在uniapp安卓环境下将这些分片的视频下周并且合并成原始视频并下载到本地。
  • ¥15 Unity导出2D项目运行时图片变成马赛克
  • ¥15 关于communitytoolkit.mvvm的生成器得到的代码看起来没有被使用的问题
  • ¥15 matlab中此类型的变量不支持使用点进行索引
  • ¥15 咨询第六届工业互联网数据创新大赛原始数据
  • ¥15 Pycharm无法自动补全,识别第三方库函数接收的参数!
  • ¥15 STM32U575 pwm和DMA输出的波形少一段
  • ¥30 android百度地图SDK海量点显示标题
  • ¥15 windows导入environment.yml运行conda env create -f environment_win.yml命令报错