JQuery/Ajax 接受来自 PHP 函数的 True/False


JQuery/Ajax accepting True/False from PHP function

function check_jobref_availability(){
    if ($job_reference_already_exists == 0) { 
        $output = TRUE;
    } else {
        $output = FALSE;
    }
    echo $output;
}

功能齐全

function check_jobref_availability(){       
    $output = 1;       
    $jobrefference = $this->uri->segment(3); //mysql_real_escape_string($_REQUEST['ptitle']);
    $strSQL = "SELECT count(*) FROM projects where ptitle = '" . $jobrefference . "' and companyid = 1";
    $job_reference_already_exists = get_singlecolumn($strSQL);        
    //if it is zero means. no refrence was found and it is good to go to add the new job reference.
    if ($job_reference_already_exists == 0) { 
        $output = 1; // NOT FOUND SO GO AHED 
    } else {
        $output = 0; // MEANS ALREAYD EXISTS give an alert
    }
    echo $output;

}

这是我的PHP代码(其余代码实际上查询数据库)并返回0或1。基于此,我需要返回真或假。

var newjobrefrence = $('#newjobrefrence').val().trim();
$.ajax(
{
    type: "POST",
    url: "<?php echo base_url(); ?>postproject/check_jobref_availability/" + newjobrefrence,        
    data: {ptitle: newjobrefrence},
    success: function(response){
        if (response.d == "false") {
            jobrefrencealreadyexists = 1;
            alert("Job ref found");
        }else{
           jobrefrencealreadyexists = 0;
           alert("Not found");
        }
    },
    error: function(xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    }
});

这是我的jquery/ajax调用。我是jquery/ajax的新手。

它需要做的就是调用check_jobref_availability,并根据返回值发出警报。

我尝试了不同的组合,例如返回Jason(我也不能100%确定是否正确),或者返回"True"或"False"和简单的true;和false;但我在我的jquery中看不到响应。

我头上的头发所剩无几,因为我整个周末都在拉它们。

任何帮助将不胜感激,以节省我的发型。

var $true = false;
$.ajax(
{
    type: "POST",
    url: "<?php echo base_url(); ?>postproject/check_jobref_availability/" + newjobrefrence,        
    data: {ptitle: newjobrefrence},
    success: function(response){
        if (response.d == "false") {
            jobrefrencealreadyexists = 1;
            alert("Job ref found");
            $true = true;
        }else{
           jobrefrencealreadyexists = 0;
           alert("Not found");
        }
    },
    error: function(xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    },
    async: false
});
  if($true) return true;
  return false;

尝试一次,并检查您的firebug's net panel以获取 ajax 调用的响应:

function check_jobref_availability(){
if ($job_reference_already_exists == 0) { 
    $output = 1;
} else {
    $output = 0;
}
echo $output;
}
success: function(response){
    if (response == 1) {
        jobrefrencealreadyexists = 1;
        alert("Job ref found");
    }else{
       jobrefrencealreadyexists = 0;
       alert("Not found");
    }
}
如果

它没有帮助,请尝试直接点击浏览器中的此链接以查看控制器的输出(如果其输出echo(ing)正确):

base_url()/postproject/check_jobref_availability/[any_job_refrence_here]

更新

var newjobrefrence = $('#newjobrefrence').val().trim();
$.ajax({
    type        : "POST",
    url         : "<?php echo base_url(); ?>postproject/check_jobref_availability/", //removed the uri segment       
    data        : { newjobrefrence : newjobrefrence },
    success     : function(response){
        if (response == "1") {
            jobrefrencealreadyexists = 1;
            alert("Job ref found");
        }else{
           jobrefrencealreadyexists = 0;
           alert("Not found");
        }
    },
    error       : function(xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    }
});
function check_jobref_availability(){
    if( $this->input->post(null) ){
        $id = $this->input->post('newjobrefrence'); #fetch from the post array and not the uri segment
        //your query here with $job_reference_already_exists
        /*if ($job_reference_already_exists == 0) { 
            $output = 1;
        } else {
            $output = 0;
        }*/
        echo $job_reference_already_exists;
    }
}