0
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。基于此,我需要返回 true 或 false。

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;和错误的;但我在我的 jquery 中看不到响应..

我头上的头发很少,因为我整个周末都在拉头发。

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

4

2 回答 2

0

尝试一次,并检查您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;
    }

}
于 2013-11-04T10:50:22.263 回答
0
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;
于 2013-11-04T11:13:12.237 回答