JQuery / Ajax从PHP函数接受True / False
function check_jobref_availability(){ if ($job_reference_already_exists == 0) { $output = TRUE; } else { $output = FALSE; } echo $output; }
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;
}
这是我的PHP代码(其余代码实际上查询数据库)并返回0或1.根据我需要返回true或false。
var newjobrefrence = $('#newjobrefrence').val().trim(); $.ajax( { type: "POST", 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中看到响应..
我头上留下了一些头发,因为我整个周末一直把它拉到头上。
保存我的发型会非常感谢任何帮助。
var $true = false; $.ajax( { type: "POST", 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 : "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; } }