php返回将包含文件的内容发送到javascript

时间:2016-01-13 10:50:05

标签: javascript php jquery

我遇到了从PHP获取返回值到JSON的问题。

如果成功或失败,警报将与include '../core/init.php';内的javascript文件内容一起显示。但我只需要成功或在警报中失败。请帮忙

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

主页:

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

提醒窗口:

<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"

3 个答案:

答案 0 :(得分:0)

在echo之后使用exit,如下所示:

    $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;
    }

答案 1 :(得分:0)

添加dataType:&#39; JSON&#39;你的AJAX JS代码:

$.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);
} 

答案 2 :(得分:0)

JavaScript更改确保您的帖子参数具有值

<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更改

<?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);