蛋糕PHP数据库查询
创建的领域独特的验证。它会在保存之前检查该字段的值。如果存在,它会告诉用户该值已经存在。
这就是我不希望它是唯一的。所以,即使值存在,用户也应该能够添加内容,但应该通知该值存在。 – 2012-02-27 16:58:20
我不明白。如果该值已经存在,并且您警告用户,会发生什么情况。他们仍然可以再次添加该值吗? – 2012-02-28 02:34:35
是的,他们应该能够添加。即使价值存在。只是它会告诉他们价值已经存在。 – 2012-02-28 05:01:03
假设: 表:帖子 型号:帖子 控制器:帖子 和领域有关,你需要前生的通知是POST_TITLE
现在做这样的事情
鉴于:
设置post_title的文本字段的ID为PostPostTitle
$('#PostPostTitle').bind({
blur:function(){
var newTitle = $(this).val();
if(newTitle){
$.ajax({
url:'Posts/checkTitle/',
type:'POST',
data:{title:newValue},
dataType:'JSON',
success:function(data){
if(data.ok==1){
//dont show any error and let the user submit
}else {
//show error notification in desired format
}
}
})
}
}
});
在您的控制器
现在做与名字checkTitle动作
控制器代码会是这样
public function checkTitle(){
//fetch the vlaue of title from appropriate key of the controller class
//variable $this->params (in cake v<2) or from $this->reuest (cake v2.x+)
$this->layout = 'ajax';//or json or null what ever you prefer
$this->autoRender = false;
//assumig that you have fetched value of newTitle in $newTitle from requestVar
//make a database query to post table
$response = array('ok'=>0);
$data = $this->Post->find('count',array('conditions'=>array('post_title'=>$newTitle)));
if($data==0) {
$response['ok']=1;
}
echo json_encode($response);
exit;
}
这是你想要做什么? http://bakery.cakephp.org/articles/instatutorial/2011/08/14/check_username_availability_in_cakephp_using_jquery – blueberryfields 2012-02-27 03:35:14
Yaa是这样的。我想要的是,当我在文本框中输入文本时,应该使用数据库中特定列的值检查输入的文本,并且如果该值存在,则只是回显该值存在。 你提供的链接似乎很有用,让我试试它,并会告诉你它是否有效。 谢谢, – 2012-02-27 03:45:41