2012-03-14 162 views
0

显示获取匹配的记录,我要检查,如果他/她的数据是使用电子邮件地址数据库表。如果它在那里,它应该获取匹配记录(姓名,地址,城市,州,邮政编码)并将其显示在表单中。如果电子邮件地址不在数据库表中,应该允许用户填写表格的其余部分。CakePHP的:使用电子邮件地址和当用户输入自己的电子邮件地址的形式

我使用CakePHP和MySQL这个项目。这可以使用AJAX完成吗?做到以上的最好方法是什么?

谢谢。

+0

我只是想知道,如果AJAX和CakePHP单独suffient为了这个目的? – vaanipala 2012-03-14 06:23:11

+0

是的,Ajax和cakephp就足够了。下面我已经向你展示了如何(取决于你的蛋糕版本)。 – swiecki 2012-03-14 15:26:50

+0

好的,我将首先做一些AJAX修订。我会试试看,让你知道。 – vaanipala 2012-03-15 03:44:32

回答

1

最好的办法确实使用AJAX。我绑定到电子邮件字段的类型事件定义一个函数,然后把它拿到你的控制器动作,如果有任何,则填补了这一数据到使用Javascript相应的字段专门返回用相应的数据JSON对象。

我在下面使用JQuery做到了这一点。

在你看来:

笔记:controller/action是应该在控制器中调用操作,也#emailbox被认为是电子邮件形式的ID。 #otherbox是其他表单元素的id的占位符。

<script type="text/javascript">           
    $(document).ready(function() {  
    $('input#emailbox').keyup(function() { 

     // could do other stuff here, like show load bar or clear old results 
     //run the ajax 
     getPage(); 
    }); 
}); 


function getPage() { 

    //generate the parameter for the php script 
    var data = {email: $("#emailbox").val()}; 
    $.ajax({ 
     url: '/controller/action', 
     type: 'GET',   
     data: data, 
     dataType: 'json',  
     cache: false, 
     success: function (data) { 

      //add the content retrieved from ajax into whatever forms you like 
      $("#otherbox").val(data.[User][information][here]); 
      $("#otherbox").val(data.[User][information][here]); 
      $("#otherbox").val(data.[User][information][here]); 
      $("#otherbox").val(data.[User][information][here]); 
     }  
    }); 
}         
</script> 

需要注意的是Chrome浏览器开发工具可以帮助调试你要回来,你能弄清楚如何访问相关数据每个字段的JSON对象。

在你的控制器:

public function searchForDetails() { 
     if($this->request->is('ajax')) { 
      $this->autoRender = false; 
//Get the email parameter data- this is one way to do it, you could also do it plenty of other ways 
$email = $this->request->query['email']; 
//Get user data- I have given an example of how to do so: 
$userdata = $this->User->find('first', array(
    'conditions' => array(
     'User.email' => $email, 
     'recursive' => -1)); 
return(json_encode($userdata)); 
      } 
    } 

这是你可以去它的一种方式。您所要做的工作是使用chrome调试JSON对象(使用命令shift j来访问开发工具)并找出对象内部数组的布局,以便设置各个表单的值。我还没有测试过这种方法的效率,如果你正在开发Facebook,你可能不应该这样做。

相关问题