如何使用javascript访问模型方法

请看下面的CakePHP代码

Flip2.php (型号)

<?php class Flip2 extends AppModel { var $name = 'Flip2'; public $useTable = false; //Increment the correct_answer field of the specific user public function correctAnswer($userID=89, $word) { $setQuery = "UPDATE `users_words` SET `correctanswer` = `correctanswer`+1 WHERE `userid`=$userID && `wordid`='$word' "; query($setQuery); } } 

Flip2Controller.php (控制器)

  

index.ctp (查看)

 Html->css(array('bootstrap', 'mark', 'style')); echo $this->Html->script(array('timer','swfobject','bootstrap.min.js')); ?>  #hideall { display: none; opacity: 0.7; position: fixed; height: 100%; width: 100%; top: 0; left: 0; background: #000; border: 1px solid #cecece; z-index: 1; vertical-align:middle; text-align:center; } .removeCardflip{ transition: rotateY(0deg); -webkit-transition: rotateY(0deg); transition-duration: 0s; } /* SECTIONS */ .section { clear: both; padding: 0 10px 0 10px; margin: 0px; }  
Html->image('progress.gif', array('alt' => 'Wait', 'style' => 'text-align:center; padding-top:200px;'));?>
<!--
-->

Play word game:

oxxxxxxxxxxxxxxxf
Html->image("comic_edit.png", array( "alt" => "Pareto List", "id" => "paretoList", 'url' => "javascript:;" ) ); ?>
<h1 data-pos="" >

$(document).ready(function(){ $( ".btn-danger" ).click(function(){ console.log("Red Button"); var toclose = $(this).parent().parent(); $.ajax({ url: "../img/media.jpg", }).done(function() { console.log( "The act has been done"); toclose.toggle(); }); }); $( ".btn-success" ).click(function(){ console.log("Red Button"); var toclose = $(this).parent().parent(); $.ajax({ url: "../img/media.jpg", }).done(function() { console.log( "The act has been done"); toclose.toggle(); }); }); $( ".xy" ).click(function(){ $(this).find("#enside1").toggle(); $(this).find("#ptside1").toggle(); console.log(this); }); });

现在,我需要做的是,这个。 当用户单击Acertei按钮时,我需要执行函数correctAnswer 。 我是PHP和CakePHP的新手,所以当单击一个按钮时,我真的很困惑。 有什么建议吗?

你有

 

您应该为每个按钮使用不同的ID。

您可以使用ajax调用correctAnswer函数:

将按钮更改为

  

然后在$(document).ready()添加以下代码

  $(document).ready(function(){ $(".btn-success").click(function(){ var word = $(this).data('word'); $.post('/flip2/correct.json', { word: word }) .done(function(data) { alert('Saved'); }); 

我不确定用户部分是如何工作的。 您应该在会话中拥有该function,而不是发送给该function。 我硬编码了用户89,并添加了一种发送单词的方法。

使用适当的模型方法

使用updateAll可以更好地编写模型中的函数correctAnswer

 public function correctAnswer($userId, $word) { return $this->updateAll( array('correctanswer' => 'correctanswer + 1'), array( 'userid' => $userId, 'wordid' => $word ) ); } 

以这种方式编写的输入( $userId$word )将被适当地转义,并且不易受sql注入的影响。

创建控制器操作

Web应用程序的门户是控制器操作,创建一个简单的函数,调用模型方法,并将其写入输出json :

 public function correct() { $postData = $this->request->data; $word = $this->request->data['word']; $userId = $this->Auth->user('id'); $result = false; if ($userId && $word) { $result = $this->Flip2->correctAnswer($userId, $word); } $this->set('_serialize', array('result')); $this->set('result', $result); } 

注意

  • 它只能通过邮寄请求工作。
  • Auth组件(会话)用于获取当前用户ID,它不是用户定义的输入。
  • 定义此函数,使其作为json响应。
  • 无需使用上述代码创建视图文件。

请务必设置您的应用以处理json请求 :

添加Router :: parseExtensions(’json’)之后; 在您的路由文件中,当使用.json扩展名完成请求时,CakePHP将自动切换视图类,或者Accept头是application / json。

调用控制器动作

所需的js将是以下forms:

 $(".btn-success").click(function(){ var word = ...; $.post( '/flip2/correct.json', {word: word}, function (data) { console.log(data); // {result: bool} } ); }); 

注意:

  • url以.json结尾,这是一种简单而强大的方式来激发CakePHP以json的forms响应。
  • 这是一个post请求。
  • 响应将是控制器操作中_serialize定义的forms。