为什么我的ajax代码实际上没有发送?

时间:2016-12-13 23:42:25

标签: javascript php ajax

我有我的ajax代码,我试图从文本框的值发布。我想将信息发送到我的php页面进行查询。

    function addUser() {
        myOutput = document.getElementById('add_user_result');
        var member_username = $('#username_name').val();
        var member_email    = $('#email_name').val();
        var member_cpukey   = $('#cpukey_name').val();
    
        if(member_username != "" & member_email != "" & member_cpukey != "") {
            $.ajax({
                type: "POST",
                url: 'includes/ajax_data_add_member.php',
                data: { username: member_username, email: member_email, cpukey: member_cpukey },
                success: function(response) {
                    $("#add_user_result").show();
                    $('#add_user_result').fadeOut(3000).html(response);
                    header('Location: admin_members.php');
                },
                error: function() {
                    $("#add_user_result").show();
                    $('#add_user_result').fadeOut(3000).html(response);
                    header('Location: admin_members.php');
                }
            });
        } else {
            $("#add_user_result").show();
            $('#add_user_result').fadeOut(3000);
            myOutput.innerHTML = "<font style='color: red;'>You must fill in all the blanks.</font>";
        }
        return false;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
  <div class="col-lg-12">
    <div class="p-20">
      <div class="card-box" style="background: #eeeeee; padding: 10px; border: 1px solid #E3E3E3; border-radius: 0px;">
        <div class="form-inline">
          <div class="form-group" style="padding-left: 4%;">
            <div class="form-group m-r-12">
              <label class="col-sm-12 control-label">Add User</label>
            </div>
            <input type="text" name="username_name" id="username_name" placeholder="Username" class="form-control" required/>
            <input type="text" name="email_name" id="email_name" placeholder="Email" class="form-control" required/>
            <input type="text" name="cpukey_name" id="cpukey_name" placeholder="CPUKey" class="form-control" required/>
            <button onclick="addUser()" class="btn btn-success"><i class="fa fa-user-plus"></i> Add User</button>
            <select class="form-control" id="selection_value" name="selection_value">
              <option value="account_credits">Account Credits</option>
              <option value="free_gifted_credits">Free Gifted Credits</option>
              <option value="time">Member Server Days</option>
            </select>
            <input type="text" name="add_to_all_value" id="add_to_all_value" onkeypress="return isNumberKey(event)" placeholder="Value to add to current" class="form-control" required/>
            <button id="button1" onclick="add_to_all()" class="btn btn-primary"><i class="fa fa-user-plus"></i> Add To All Users</button>
          </div>
        </div>
      </div>
      <div id="add_user_result" class="add_user_result"></div>
    </div>
  </div>
</div>

我的PHP代码

if(!empty($_POST) && isset($_POST)) {
$username           = mysqli_real_escape_string($con, $_POST['username']);
$email              = mysqli_real_escape_string($con, $_POST['email']);
$cpukey             = mysqli_real_escape_string($con, $_POST['cpukey']);
$default_password   = grab_default_user_password();

$insert_query = mysqli_query($con, "INSERT INTO users SET username = '$username', password = '$default_password',  email = '$email', cpukey='$cpukey'");
if($insert_query) {
    echo '<font style="color: green;">Successfully Inserted user <b>'.$username.'</b></font>';
    echo '<META HTTP-EQUIV="Refresh" CONTENT="3; URL=admin_members.php">';
}
else {
    echo '<font style="color: red;">Failed to insert user <b>'.$username.'</b></font>';
    echo '<META HTTP-EQUIV="Refresh" CONTENT="3; URL=admin_members.php">';
}
}

有人知道为什么当我点击并输入每个文本框时,它会说“你必须填写所有空白。”即使它们都不等于''(空白)。然后,当我刷新时,它会将我发送回index.php(签署我的会话)。

3 个答案:

答案 0 :(得分:1)

使用&&代替&会导致条件正常工作。

有关原因的详细信息,请参阅this answer

另外,你没有在你的ajax的错误函数中定义response,它会抛出错误。

答案 1 :(得分:0)

首先,你需要把双“&amp;”在JavaScript中表示“和”

if(member_username != "" & member_email != "" & member_cpukey != "") {

所以让我们假设请求已经很好地发送到服务器端,你在那里设置的请求将无法工作,因为你的语法错误很大,你不能在SQL INSERT INTO users SET usernam中说 正确的形式是将其写成:

"INSERT INTO users VALUES('$username','$default_password','$email','$cpukey')" 

另一个评论是"header('Location: admin_members.php');"是一个PHP代码,所以它不会在你的JS代码上被解释,因此我建议你使用window.location.href = "admin_members.php";我希望它有所帮助。

答案 2 :(得分:0)

我发现它无法正常工作的原因。

我在我的php文件中调用我的数据库文件太晚了。 我的新工作代码是:

<?php
session_start();
include_once('../configuration/db.php');

function user_session_timeout_manual() {
global $con;
    $grab_user_timeout_sql = mysqli_query($con, "SELECT user_session_timeout FROM sitesettings");
    $row = mysqli_fetch_array($grab_user_timeout_sql);
    return $row['user_session_timeout'];
}

function grab_default_user_password() {
global $con;
    $grab_default_user_password_sql = mysqli_query($con, "SELECT default_user_insert_password FROM sitesettings");
    $row = mysqli_fetch_array($grab_default_user_password_sql);
    return $row['default_user_insert_password'];
}

if(!isset($_SESSION['username']) || time() - $_SESSION['login_time'] > user_session_timeout_manual()) {
    unset($_SESSION['login']);
    unset($_SESSION['username']);
    session_destroy();
    header("Location: index.php");
}

else {
    $_SESSION['login_time'] = time();

if(!isset($_SESSION['login']) || $_SESSION['login'] !== true) {
    unset($_SESSION['login']);
    unset($_SESSION['username']);
    session_destroy();
    header('Location: index.php');
}
else {

if(!empty($_POST) && isset($_POST)) {
    $username_grab          = mysqli_real_escape_string($con, $_POST['username']);
    $email_grab             = mysqli_real_escape_string($con, $_POST['email']);
    $cpukey_grab            = mysqli_real_escape_string($con, $_POST['cpukey']);
    $default_password_grab  = grab_default_user_password();

    $insert_query = mysqli_query($con, "INSERT INTO users SET username = '$username_grab', password = '$default_password_grab',  email = '$email_grab', cpukey='$cpukey_grab'");
    if($insert_query) {
        echo '<font style="color: green;">Successfully Inserted user <b>'.$username_grab.'</b></font>';
        echo '<META HTTP-EQUIV="Refresh" CONTENT="3; URL=admin_members.php">';
    }
    else {
        echo '<font style="color: red;">Failed to insert user <b>'.$username_grab.'</b></font>';
        echo '<META HTTP-EQUIV="Refresh" CONTENT="3; URL=admin_members.php">';
    }
}

}}

?>