【问题标题】:JQuery/Ajax accepting True/False from PHP functionJQuery/Ajax 从 PHP 函数接受 True/False
【发布时间】:2013-11-04 10:38:28
【问题描述】:
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 中看不到响应..

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

非常感谢任何帮助以保存我的发型。

【问题讨论】:

  • 您是否检查了对您的发布请求的响应正文? (使用萤火虫或类似的东西)。
  • 你能打开 /postproject/check_jobref_availability/ 看看它有什么反应吗?也许它的路由问题
  • 检查响应更容易...一个简单的 console.log(result) 也可以提供帮助。对我来说,您似乎在测试“TRUE”==“true”,JavaScript 区分大小写

标签: php codeigniter jquery boolean


【解决方案1】:
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;

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我的 PHP 正在根据我作为工作参考提供的内容返回 0 和 1。但反应总是正确的。任何建议。
    • 我的意思是如果做警报(“响应:”+响应);成功函数中的第一件事总是返回 true
    • 在您的问题中发布您的完整功能check_jobref_availability()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    相关资源
    最近更新 更多