weixin_33676492 2016-11-27 16:51 采纳率: 0%
浏览 16

通过jQuery和PHP进行AJAX发布

This is my first time using AJAX. I'm Trying to write data from an HTML form to a phpmyadmin database. I'm using jQuery for the AJAX call. 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. No Javascript errors in the browser. 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);
?>
  • 写回答

1条回答 默认 最新

  • python小菜 2016-11-27 16:57
    关注

    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 :

    $.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.

    NOTE : Add try{ }catch(){} in your PHP code to debug the instructions and show the response in alert as shown in the above code.

    Hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。