JQuery的/阿贾克斯接受真/假的PHP函数 [英] 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 code(在code实际查询数据库的其余部分),并在此基础上,我需要返回true或false返回0或1。
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);
}
});
这是我的jQuery / AJAX调用。我是新来的jQuery / AJAX。
And this is my jquery/ajax call. I am new to jquery/ajax.
所有它需要做的就是调用check_jobref_availability,并根据返回值做一个警告。
All it needs to do is call check_jobref_availability, and based on the return value do an alert.
我已经试过好像回到杰森(我不是100%肯定,如果正确地做,要么),或返回真或假和简单的真不同组合;和虚假的;但我不能看到我的jQuery ..
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.
任何帮助将是pciated救我的发型多少AP $ P $。
Any help would be much appreciated to save my hair style.
推荐答案
试试这个一次,并检查您的 Firebug的净面板
AJAX调用的响应:
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");
}
}
如果它不列入帮助,请点击浏览器此链接直接看到你的控制器的输出如果回声(ING)
正确的输出:
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]
更新
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;
}
}
这篇关于JQuery的/阿贾克斯接受真/假的PHP函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!