如何使用javascript访问模型方法 [英] How to access a model method with javascript

查看:79
本文介绍了如何使用javascript访问模型方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请观看以下CakePHP代码

Please have a look at the below CakePHP code

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 (Controller)

Flip2Controller.php (Controller)

<?php
class Flip2Controller extends AppController {

    public function index()
    {
    }       
}
?>

index.ctp (View)

<?php
//echo $this->Html->css(array('bootstrap', 'mark', 'style'));
echo $this->Html->script(array('timer','swfobject','bootstrap.min.js'));
?>
<style>
#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;
}

</style>

<div id="hideall">
    <?php //echo $this->Html->image('progress.gif', array('alt' => 'Wait', 'style' => 'text-align:center; padding-top:200px;'));?>
</div>

<!--<div class="wrapper" style="border: 1px solid red; width: 100%;">-->
    <div class="section group" style="margin-top: 50px;">
        <div class="col span_3_of_3">
            <h3 style="margin:0px; font-size:22px;">Play word game: </h3>
        </div>
    </div>


    <div class="">
        <div>
            <div>
                <span class="remainWords"><?php //echo count($words);?></span> oxxxxxxxxxxxxxxxf <?php //echo $totalWords;?>
            </div>

            <div>
                <?php
                echo $this->Html->image("comic_edit.png",
                                        array(
                                            "alt" => "Pareto List",
                                            "id" => "paretoList",
                                            'url' => "javascript:;"
                                        )
                                    );
                ?>
            </div>
        </div>
    </div>




    <div class="container"><div class="row">






<?php



    foreach($worddeck as $worcard)
    {
    ?>
        <div class="xy col-lg-3 col-md-4 col-sm-6 img-rounded" id="card1" style="width:250px; height:200px; background-color:grey; heiht:170px; margin: 10px 10px;">
            <div id="enside1" >
                <h1 data-pos="<?php //echo ; ?>" ><?php echo $worcard['unique_wordsforcards']['en_word']; $enSpell = $worcard['unique_wordsforcards']['en_word']; ?></h1>       
            </div>

            <div id="ptside1" style="display:none;">

            <?php echo $phonemer[$enSpell]; ?>
                <p><?php echo $worcard['unique_wordsforcards']['hint']; ?></p>
            </div>
            <div id="cntrol1">
                <button type="button" id="2" class="a btn btn-success mr5 btn-lg">Acertei</button>
                <button type="button" id="2" class="e btn btn-danger mr5 btn-lg">Errei</button>
            </div>
        </div>
    <?php
    }
?>





    </div></div>


    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
<script type="text/javascript">
$(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);
  });

});


</script>

现在,我需要做的是,这。当用户点击 Acertei 按钮时,我需要执行 correctAnswer 函数。我对PHP和CakePHP非常新,所以我真的很困惑,当点击按钮时如何做到这一点。

Now, what I need to do is, this. When the user click on the Acertei button, I need to execute the function correctAnswer. I am very new to PHP and CakePHP so I am really confused about how to do this when a button is clicked. Any advice please?

推荐答案

您有

<div id="cntrol1">
    <button type="button" id="2" class="a btn btn-success mr5 btn-lg">Acertei</button>
    <button type="button" id="2" class="e btn btn-danger mr5 btn-lg">Errei</button>
</div>

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

You should use different IDs for each button.

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

You can call the correctAnswer function with ajax:

将按钮更改为

<button type="button" id="2" data-word="<?php $worcard['unique_wordsforcards']['en_word'] ?>" class="a btn btn-success mr5 btn-lg">Acertei</button>

然后在 $(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');
          });

我不知道用户部分是如何工作的,你应该在会话中有这个,而不是发送到函数。我硬编码用户89,并为你添加一个方法发送单词。

I am not sure how the user part works. You should probably have that in the session, and not sent to the function. I hardcoded user 89 and added a way for you to send the word.

这篇关于如何使用javascript访问模型方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆