1

I have looked at many posts here and elswhere on this error without success of resolving my error. In console the form data is being sent via json as expected to my php processing page, the php process is not returning any errors so I'm confident the process is completing. I have checked the way the result is formatted and cannot see anything wrong there, although I'm no expert at ajax/json so I could be wrong, Here is my code

Jquery code

<script type="text/javascript">

$(document).ready(function() {

    $("#admin_login_but").click(function()  {

    if($("#admin_name").val()=="" || $("#admin_password").val()=="" || $("#admin_email").val()=="") {

    $(".adminloginError").html("Please enter Admin Name, Admin Email and Admin Password");

        return false;   

    }else{


    $(".adminloginError").html('<img src="image/ajax-loader.gif" width="16" height="16" alt=""/>');
    var adminName=$("#admin_name").val();
    var adminPassword=$("#admin_password").val();
    var adminEmail=$("#admin_email").val(); 

$.post("includes/admin_login.inc.php",{admin_name:adminName,admin_password:adminPassword, admin_email:adminEmail},function(json)   {
    if(json.result === "success") {
        $(".adminloginError").html( "Welcome "+adminName+"!");

        setTimeout(function(){
              window.location= "admin_secure.php"; 
        },2000);                          

    }else{
        $(".adminloginError").html(json.message);
    }
});
    }

Here is my php processing code

<?php
include_once 'functions.php';
include_once 'db_connect.php';

header("Content-Type: application/json");   //this will tell the browser to send a json object back to client not text/html (as default)

//convert variable (array) into a JSON object

function result($var){
    echo json_encode($var);
    exit();
}

sec_session_start();

error_reporting(E_ALL); ini_set('display_errors', 1);


//check if surname is empty 
  if (isset($_POST["admin_login_but"])) {

//check if first_name is empty  
  if (empty($_POST["admin_name"])) {

        $response = array('result'=>'fail', 'message' => 'Missing Admin Name');
        result($response);

  }else{

// if not empty sanitize first_name input
    $admin_name = filter_input(INPUT_POST, 'admin_name', FILTER_SANITIZE_STRING);

  }


 //Check if email is empty and santize and validate email
      if (empty($_POST['admin_email'])) {

        $response = array('result'=>'fail', 'message' => 'Missing Admin Email');
        result($response);

      }else{

        $admin_email = filter_var($_POST['admin_email'], FILTER_SANITIZE_EMAIL);
}

        if (!filter_var($admin_email, FILTER_VALIDATE_EMAIL)) {

        $response = array('result'=>'fail', 'message' => 'The Email is not in a Valid Email Format!');
        result($response);  

        } 



 //check if register password input is empty
     if (empty($_POST["admin_password"])) {

        $response = array('result'=>'fail', 'message' => 'Missing Admin Password');
        result($response); 

  } else {
//Sanitize the data passed in 'password'
    $admin_password = filter_input(INPUT_POST, 'admin_password', FILTER_SANITIZE_STRING);
}

    //validate the data passed in 'password'
    if (!preg_match("/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/", $admin_password)) {

        $response = array('result'=>'fail', 'message' => 'Password is in the Wrong Format!');
        result($response);

        }  


//query database
$results = mysqli_query($mysqli, "SELECT * FROM admin WHERE name = '$admin_name' AND email = '$admin_email' AND hash = '$admin_password'");

// Check if SQL query had erors 
if(!$results){

        $response = array('result'=>'fail', 'message' => 'sql error: ' . mysqli_error($mysqli));
        result($response);
}

// If query was successfull and there are rows do this:
if (mysqli_num_rows($results)>0){ 

    $_GET['name'] = $admin_name;

    $response = array('result'=>'success', 'message' => 'User is Authenticated');
    result($response);

} else {

    $response = array('result'=>'fail', 'message' => 'User Authentication Failed');
    result($response);

}
}

?>

I cannot figure out how to resolve this. Can anyone help please

4
  • possible duplicate of JSON Parse Error: unexpected end of data at line 1 column 1 of the JSON data Commented May 4, 2015 at 14:54
  • I had already looked at this and it focuses on the jquery script, I do not believe my problem is with the jquery but how the php is returning the json and the question you mentioned does not deal with that, like I said searched all over for a solution and I only ever post here as a last resort Commented May 4, 2015 at 15:08
  • did you check that you don't have a empty line /newline at the beginning or end of your script ? (if so then it creates httpheaders instead of your json ) Commented May 4, 2015 at 15:22
  • Hi, I took out any line spaces between the top of the page until the processing starts and took out the line spaces after the process finishes so no spaces then before the php closing tag on your suggestion, Still the same error Commented May 4, 2015 at 15:28

1 Answer 1

0

I just figured it out, I hadn't put an else option at the end of my php for the initial post check

if(!isset($_POST)) {

//all the processing code


}else{
    $response = array('result'=>'success', 'message' => 'Post is Empty');
    result($response);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.