我很新,将ajax和jqquery添加到codeigniter
当我运行以下代码时我没有返回任何内容
我不知道ajax中的url是否正常工作
控制器
function test()
{
$email=$this->input->post('email');
$query=mysql_query("select * from info where email='$email'");
$row=mysql_num_rows($query);
if($row==0)
{
echo "<span style='color:green;'>Available</span>";
}
else
{
echo "<span style='color:red;'>Not Available Choose another one..</span>";
}
}
查看
<html>
<head>
<script type="text/javascript" src="http://localhost/ajaxchecker/jquery.min.js">
<script type="text/javascript">
$(document).ready(function(){
$("#email").keyup(function() {
var email = $('#email').val();
$.ajax({
type: "POST",
url: "ajaxsample/check",
data: "email="+ email ,
success: function(html){
$("#disp").html(html);
}
});
return false;
});
})
</script>
</head>
<body>
<h3>Username Check using Ajax/php.mysql</h3>
<form method="post">
Username: <input type="text" name="email" id="email" /><br /><br />
<div id="disp"></div><br />
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
</script>
</head>
<body>
提前致谢
答案 0 :(得分:1)
首先,您必须检查$(&#34; #email&#34;)。keyup(function()是否正常工作,尝试发送警报()并在电子邮件输入中写入内容以验证您是否正在输入功能。
其次,尝试检查您是否通过控制器中的Post接收电子邮件。如果你对此没有问题......
尝试将您的控制器更改为:
function test()
{
$email=$this->input->post('email');
$query=mysql_query("select * from info where email='$email'");
$row=mysql_num_rows($query);
if($row==0)
{
$text = "<span style='color:green;'>Available</span>";
}
else
{
$text = "<span style='color:red;'>Not Available Choose another one..</span>";
}
$this->output->set_output($text);
}
您对此的看法:
<script type="text/javascript" src="http://localhost/ajaxchecker/jquery.min.js">
<script type="text/javascript">
$(document).ready(function(){
$("#email").keyup(function() {
var email = $('#email').val();
$.ajax({
type: "POST",
url: "<?php echo site_url('ajaxsample/check'); ?>",
data: {email:email} ,
dataType: "html",
success: function(html){
$("#disp").html(html);
}
});
return false;
});
})
</script>
记得加载url_helper以使用site_url()和base_url()。 如果您仍有问题,请将此添加到您的ajax。
success: function(html){
$("#disp").html(html);
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
答案 1 :(得分:0)
这样做
<script type="text/javascript" src="http://localhost/ajaxchecker/jquery.min.js">
<script type="text/javascript">
$(document).ready(function(){
$("#email").keyup(function() {
var email = $('#email').val();
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>index.php/ajaxsample/test",
data: {'email':email},
success: function(html){
$("#disp").html(html);
}
});
return false;
});
})
</script>
控制器中的
function test()
{
$email=$this->input->post('email');
$query=$this->db->query("select * from info where email like '%".$email."'%");
if($query->num_rows() > 0)
{
echo "<span style='color:green;'>Available</span>";
}
else
{
echo "<span style='color:red;'>Not Available Choose another one..</span>";
}
}