0

I'm facing a problem in getting the return value from PHP to JSON.

If success or failure the alert will be displayed with the content of a javascript file inside include '../core/init.php';. But I need only success or failed in alert. Please help

Requestfile:

$id = intval($_REQUEST['id']);
$empid=$_REQUEST['empid'];

include '../core/init.php';

if($user_data['empid']==$empid){
    $delete_sql = "delete from datatable where sl=$id";
    mysql_query($delete_sql);  
    echo json_encode("success");
    }
    else{
  echo json_encode("failure");
}

Main Page:

$.ajax({
url: 'returnpage.php',
type: 'post',
data: {
'id' : row.sl,
'empid':row.empid                   
},
success:function(status){
alert(status);
},
error:function(status){
alert(status);
} 

Alert window:

<style>

/*html {

    background: url(includes/background.jpg) no-repeat center center fixed; 

  -webkit-background-size: cover;

  -moz-background-size: cover;

  -o-background-size: cover;

  background-size: cover;

}*/

</style>



<script type="text/javascript" src="css/js/browsercheck.js"></script>

 <script >

var browser_name=browserinfo();

//alert(browser_name);

if(browser_name!=="Firefox"){
   window.location="non_compatible_browser.php";
}
</script>



<script type="text/javascript" src="css/js/browsercheck.js"></script>

<script >

   // alert("Application Will be down..!!\nFrom 23-Jan-2016 07:15 PM to 23-Jan-2016 08:15 PM \nPlease save the data. ")

    var browser_name=browserinfo();

    //alert(browser_name);

    if(browser_name!=="Firefox"){

        window.location="non_compatible_browser.php";

    }

    //window.location="Maintenance_page.php";

//exit();

</script>

"failure"
Mr.Kiran
  • 79
  • 1
  • 1
  • 9

3 Answers3

0

use exit after echo as below :

    $id = intval($_REQUEST['id']);
    $empid=$_REQUEST['empid'];

    include '../core/init.php';

    if($user_data['empid']==$empid){
        $delete_sql = "delete from datatable where sl=$id";
        mysql_query($delete_sql);  
        echo json_encode("success");
exit;
        }
        else{
      echo json_encode("failure");
exit;
    }
Drudge Rajen
  • 7,584
  • 4
  • 23
  • 43
  • While this may or may not help (depending on where the unwanted output comes from), an explanation why it should might be a good idea here. – Burki Jan 13 '16 at 11:00
  • @Burki if anything is echoed in the ajax function call then it will give a same whole page as response . – Drudge Rajen Jan 13 '16 at 11:03
0

Add dataType: 'JSON' to your AJAX JS code:

$.ajax({
url: 'returnpage.php',
type: 'post',
dataType: 'JSON',
data: {
'id' : row.sl,
'empid':row.empid                   
},
success:function(status){
alert(status);
},
error:function(status){
alert(status);
} 
Patchesoft
  • 317
  • 6
  • 21
0

JavaScript Changes make sure your post parameters have the values

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
$(document).ready(function(){

    $.ajax({
        url: 'returnpage.php',
        type: 'post',
        data: {
            'id' : row.sl,
            'empid':row.empid                   
        },
        dataType:'json',
        success:function(result){
            alert(result.status);
        },
        error:function(result){
            alert(result);
        } 
    });
});
</script>

PHP changes

<?php

$id = intval($_REQUEST['id']);
$empid=$_REQUEST['empid'];

$data = array();
$data['status'] = 'failure';

//include '../core/init.php';

if($user_data['empid']==$empid){
    $delete_sql = "delete from datatable where sl=$id";
    $result = mysql_query($delete_sql);

    if($result) {
        $data['status'] = 'success';
    }
}
echo json_encode($data);
Sundar
  • 4,580
  • 6
  • 35
  • 61