yii Jquery Ajax
纠结一下午了,求高手相助,刚接触yii,这有什么问题吗
$.ajax({
type:'post',
url:"/www/index.php?r=home/CkUser",//这个地址应该怎么写
data:{"name":val},
success:function(msg){
alert("正确");
$("#nameinfo").html(msg);
},
}
);
我想传值到actionCkUser()里
class HomeController extends Controller{
public function actionCkUser(){
//todo
$name=Yii::app()->request->getParam('name');
echo $name.'bb';
$model=new User;
if($name)
{
$model->username=$name;
$user = User::model() -> find("username = '{$this -> username}'");
$result='';
if($user)
{
$result='用户已存在';
}
else
{
$result='ok';
}
return $result;
}
$this->render('CkUser',array(
'model'=>$model));
}
}
回复讨论(解决方案)
?r=home/CkUser
$.post('?r=home/CkUser',{'name':val'},function(msg){ //数据处理 });
好像还是不行
好像还是不行
那你现在是什么问题,这样传值是没错的。不行就贴出错误信息。
后来这样写
var data="name="+val;
//alert(data);error:function(){alert('错误');}
$.ajax({
type:'post',
url:"http://localhost/message/www/index.php?r=home/CkUser",
data:data,
success:function(msg){
alert('msg');
$("#nameinfo").html(msg);
},
}
);当输入框没有填值时失去焦点,alert('msg')会提示,输入值以后再失去焦点就不提示了,我这个是验证用户名是否存在的
既然你已经使用了YII,那么何不用它的表单规则呢,封装起来,不管你数据库里哪个字段都是可以验证是否重复,这也是YII的好处啊。
关键是我们这边让自己写,没办法啊,这两天看yii看得头都大了
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn