drxt70655 2013-11-04 10:38
浏览 59

JQuery / Ajax从PHP函数接受True / False

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

complete function

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;

}

This is my PHP Code (rest of the code actually queries the DB) and returns 0 or 1. Based on which i need to return true or 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);
    }
});

And this is my jquery/ajax call. I am new to jquery/ajax.

All it needs to do is call check_jobref_availability, and based on the return value do an alert.

I have tried different combinations like returning Jason (I am not 100% sure if was doing that correctly either), or returning "True" or "False" and simple true; and false; but I can not see the response in my jquery..

I have got few hair left on my head as I have been pulling them all weekend on this.

Any help would be much appreciated to save my hair style.

  • 写回答

2条回答 默认 最新

  • dourang8305 2013-11-04 10:50
    关注

    Try this once, and check your firebug's net panel for the ajax call's response:

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

    If it doesnot helps, try hitting this link in your browser directly to see the output of your controller if its echo(ing) correct output:

    base_url()/postproject/check_jobref_availability/[any_job_refrence_here]
    

    Update

    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;
        }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)