我使用codeigniter框架,我的问题是我无法将正确的值从php传递给js。此代码用于从用户输入到数据库的可用性检查。
这里是我的控制器
class Ajax extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->output->set_output("This is an AJAX endpoint!");
}
function activityCode_availability()
{
$activityCode = trim($this->input->post("activityCode"));
$activityCode = mysql_escape_string($activityCode);
$msg = $this->Activity_model->check_activityCode($activityCode);
if($msg == 1)
{
$a= ''.$activityCode.' is already in use.';
return $a;
}
else
{
$b = 'OK';
return $b;
}
}
}这是我的看法
$activityCode = array(
'name' => 'activityCode',
'id' => 'activityCode'
);
echo form_input($activityCode);
?>
这是我的模型
function check_activityCode($activityCode)
{
$query = $this->db->query("SELECT activityCode
FROM ami_activity
WHERE activityCode = '$activityCode'");
return $query->num_rows();
}这是我的Javascript
(document).ready(function(){
$('#activityCode').change(activity_check);
});
function activity_check()
{
var activityCode = $('#activityCode').val();
var msgbox = $("#availability_status");
if(activityCode.length > 2)
{
$("#availability_status").html(' Checking availability.');
$.ajax({
type: "POST",
url: "/index.php/ajax/activityCode_availability",
data: "activityCode="+ activityCode,
success: function(msg){
$("#availability_status").ajaxComplete(function(event, request){
if(msg == 'OK')
{
$("#activityCode").removeClass("red"); // remove red color
$("#activityCode").addClass("green"); // add green color
msgbox.html(' Available ');
}
else
{
$("#activityCode").removeClass("green"); // remove green color
$("#activityCode").addClass("red"); // add red color
msgbox.html(msg);
}
});
}
});
}
else
{
$("#activityCode").addClass("red"); // add red color
$("#availability_status").html('Enter valid User Name');
}
}任何想法都非常感谢。先谢谢了。
谢谢,
果酱