简体   繁体   English

通过jQuery和PHP进行AJAX发布

[英]AJAX Post via jQuery and PHP

This is my first time using AJAX. 这是我第一次使用AJAX。 I'm Trying to write data from an HTML form to a phpmyadmin database. 我正在尝试将数据从HTML表单写入phpmyadmin数据库。 I'm using jQuery for the AJAX call. 我正在使用jQuery进行AJAX调用。 I suspect there's something wrong in the jQuery because I've included a Javascript alert with echo at the top of the registerUser.php file and it won't appear when I attempt to run this code, so the PHP is not even running. 我怀疑jQuery中有问题,因为我在registerUser.php文件的顶部包含了带有echo的Javascript alert ,当我尝试运行此代码时它不会出现,因此PHP甚至没有运行。 No Javascript errors in the browser. 浏览器中没有Javascript错误。 I appreciate your insight. 感谢您的见解。

function registerUserViaAjax() {
  $.ajax({
    type: "POST",
    url: "registerUser.php",
    data: {
      registerUsername: $('#registerUsername').val(),         
      registerPassword: $('#registerPassword').val(), 
      registerPassword: $('#registerEmail').val() 
    } 
  })
}
<label>Username</label>
<input class="w3-input" type="text" id="registerUsername">

<label>Password</label>
<input class="w3-input" type="password" id="registerPassword">

<label>Confirm Password</label>
<input class="w3-input" type="password" id="registerConfirmPassword">

<label>Email Address</label>
<input class="w3-input" type="text" id="registerEmail">

<button class="w3-btn w3-green" style="margin-top:3%;margin-bottom:3%" id="registerButton" onclick="registerUserViaAjax()">Go</button>
<?php
    $host = 'localhost';
    $user = 'root';
    $pw  = '';
    $db = 'fall2167';

    $dbc = mysqli_connect($host, $user, $pw, $db)
        or die('LOCAL CONNECT ERROR: '. mysqli_connect_error());

    $uname = mysqli_real_escape_string($dbc, $_POST['registerUsername']);
    $pword = mysqli_real_escape_string($dbc, $_POST['registerPassword']);
    $pword = password_hash($pword, PASSWORD_DEFAULT);

    $email = mysqli_real_escape_string($dbc, $_POST['registerEmail']);

    $check = mysqli_query($dbc, "select id from hw6 where uname = '$uname'")
        or die('confirm6 read error: ' . mysqli_error($dbc));

    if (mysqli_num_rows($check) != 0)
    {
        echo "<script> usernameTaken(); </script>";
        exit;
    }

    $query = "insert into users(uname, pword, email)" . "values('$uname','$pword','$email')";
    $result = mysqli_query($dbc, $query)
        or die('DB Write Error: ' . mysqli_error($dbc));

    echo "<script> openSuccessMessage(); </script>";
    mysqli_close($dbc);
?>

You should use done and fail callbacks to make sure what happen and what is the response you're getting back from the server side : 您应该使用donefail回调,以确保发生了什么以及从服务器端得到的响应是什么:

$.ajax({
  type: "POST",
  url: "registerUser.php",
  data: {registerUsername: $('#registerUsername').val(), registerPassword: $('#registerPassword').val(), registerPassword: $('#registerEmail').val() },
})
  .done(function(response) {
   alert( response );

   //call you function 'openSuccessMessage' here
   openSuccessMessage(); 
})
  .fail(function(response) {
  alert( response );
});

Your openSuccessMessage should be called in the callback and not inside the PHP code. 您的openSuccessMessage应该在回调中而不是在PHP代码中调用。

NOTE : Add try{ }catch(){} in your PHP code to debug the instructions and show the response in alert as shown in the above code. 注意:如上述代码所示,在PHP代码中添加try{ }catch(){}以调试指令并在警报中显示响应。

Hope this helps. 希望这可以帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM