2

I have my ajax code that I'm trying to post from the values of textbox's. I want to send the info to my php page to where it does a query.

    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>

My PHP code

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">';
}
}

Does anyone know why when I click on and type in each of the textbox's, it says " You must fill in all the blanks. " even though none of them are equal to '' (blank). And then, when I refresh, it sends me back to the index.php (signs my session out).

Phiter
  • 14,570
  • 14
  • 50
  • 84

3 Answers3

1

Using && instead of & will cause the conditional to work properly.

See this answer for more details on why.

Also, you didn't define response in your ajax's error function, it will throw errors.

Community
  • 1
  • 1
Phiter
  • 14,570
  • 14
  • 50
  • 84
  • while && vs & is something that the OP should be doing, it seems the "logic" would actually be correct – Jaromanda X Dec 13 '16 at 23:57
  • Yes, the logic is correct. I debugged his code and the reason why they didn't work became clear. – Phiter Dec 13 '16 at 23:58
  • @PhiterFernandes I'm now using this : ` if(member_username != "" && member_email != "" && member_cpukey != "")` but it's still saying the message "You must fill in all the blanks." – BenZa Music Dec 14 '16 at 16:18
0

first of all , you need to put double "&" to mean the "and" in JavaScript so

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

so let's suppose now that the request had been well sent to the server side , the request that you're setting there will not work cause you are having a big syntax error , you can not say in SQL INSERT INTO users SET usernam the correct form is to write it as this :

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

and another remark is that "header('Location: admin_members.php');" is a PHP code so it will not be interpreted on your JS code so for that i suggest you to use the window.location.href = "admin_members.php"; i hope it helped.

PacMan
  • 1,358
  • 2
  • 12
  • 25
  • In mysql, you can use SET for INSERTS. It's an mysql extension. [Check this](http://stackoverflow.com/questions/861722/mysql-insert-into-table-values-vs-insert-into-table-set). – Phiter Dec 14 '16 at 10:04
  • `"INSERT INTO users VALUES('$username','$default_password','$email','$cpukey')" ` how is this specifying the column to enter it into? – BenZa Music Dec 14 '16 at 16:22
  • it will do the insert asynchronously to the order of your fields in the database – PacMan Dec 14 '16 at 20:53
0

I found out the reason it was not working correctly.

I was calling my my database file too late in my php file. My new working code is :

<?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">';
    }
}

}}

?>