Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

337
Views
Ajax data type JSON not working

I am trying to insert data using AJAX JSON but it's not working. I tried without JSON and it works, but an alert box shows with some HTML code.

HTML:

<a href="#" id="sbreak" onclick="return sbreak();">Short Break</a>

AJAX:

$(document).ready(function() {
  $('#sbreak').on('click', function() {
    var name = $("SBreak").val();
    $.ajax({
      type: "POST",
      dataType: 'json',
      url: "brkrequest.php",
      data: {
        sname: name
      }
      cache: false,
      success: function(server_response) {
        if (server_response.status == '1') //if ajax_check_username.php return value "0"
        {
          alert("Inserted ");
        } else if (server_response == '0') //if it returns "1"
        {
          alert("Already Inserted");
        }
      },
    });
    return false;
  });
});

PHP: :

session_start();
date_default_timezone_set('Asia/Kolkata');
$sname=$_POST['sname'];
$sname= $_SESSION['myusername'];
$reqdate = date("Y-m-d H:i:s");
include("connection.php");
//Insert query
$query = sprintf("SELECT * FROM `breakqueue` WHERE (`sname` ='$sname')");

$result = mysql_query($query);
if(mysql_num_rows($result) > 0){     
    $data['status']= '1';//If there is a  record match Already Inserted 
}
else { // if there is no matching rows do following
    $query = mysql_query("INSERT INTO `breakqueue`(`id`, `sname`, `btype`, `reqdate`, `apdate`, `status`) VALUES ('','$sname','Sbreak','$reqdate','','Pending')");

    $data['status']= '0';//Record Insered
}

echo json_encode($data);
}
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

use it in php header('Content-Type:application/json');

and write

    success: function(server_response){ 
    console.log(typeof server_response);
...

for finding response type, if type of server_response isn't object

use it for convert it to object :

server_response = JSON.parse(server_response);

php Code:

session_start();

//Here added...
header('Content-Type:application/json');

date_default_timezone_set('Asia/Kolkata');
$sname=$_POST['sname'];
$sname= $_SESSION['myusername'];
$reqdate = date("Y-m-d H:i:s");
include("connection.php");
//Insert query
 $query = sprintf("SELECT * FROM `breakqueue` WHERE (`sname` ='$sname')");

  $result = mysql_query($query);
  if(mysql_num_rows($result) > 0){

$data['status']= '1';//If there is a  record match Already Inserted 
  }
  else{ // if there is no matching rows do following
$query = mysql_query("INSERT INTO `breakqueue`(`id`, `sname`, `btype`, `reqdate`, `apdate`, `status`) VALUES ('','$sname','Sbreak','$reqdate','','Pending')");

$data['status']= '0';//Record Insered
 }

echo json_encode($data);
}

Javascript Code:

  $(document).ready(function()
    {
    $('#sbreak').on('click', function(){
    var name = $("SBreak").val();
    $.ajax({
    type: "POST",
    dataType:'json',
    url: "brkrequest.php",
    data: {sname: name}
    cache: false,
    success: function(server_response){ 
         //TODO:REMOVE IT After seeing.  alert or console.log for seeing type
         alert(typeof server_response);

         if(typeof server_response){
           server_response = JSON.parse(server_response);
          }


        if(server_response.status == '1')//if ajax_check_username.php return value "0"
        { 
            alert("Inserted ");
        }  
        else  if(server_response == '0')//if it returns "1"
        {  
        alert("Already Inserted");
        }  
    },
    });
    return false;
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!