【问题标题】:Calling PHP code with Ajax [duplicate]使用 Ajax 调用 PHP 代码 [重复]
【发布时间】:2014-07-08 07:19:39
【问题描述】:

请看下面的代码

   <?php
//echo $this->Html->css(array('bootstrap', 'mark', 'style'));
echo $this->Html->script(array('timer','swfobject','bootstrap.min.js'));
//$this->requestAction('/flip2/correctAnswer')


?>
<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="acertei" class="a btn btn-success mr5 btn-lg">Acertei</button>
                <button type="button" id="Errei" 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);
  });

  $("#acertei").on("click", function() {
    $.ajax({
        url: "/flip2/correctAnswer",
        success: function(data) {
            // Do whatever you need to do with the response here.
        },
    });
});

});


</script>

这是CakePHP 页面的View$this-&gt;requestAction('/flip2/correctAnswer') 将调用Controller 中的correctAnswer() 方法。

这里如果我在页面加载时调用$this-&gt;requestAction('/flip2/correctAnswer'),这可以正常工作。我需要在单击 Acertei 按钮时调用它,因此我按照 SO 用户的建议将其添加到 Ajax 中。但是,它不起作用,该函数没有被调用。这是为什么呢?

【问题讨论】:

  • 简单.....查找Ajax
  • 要在 HTML 按钮单击时执行 PHP 代码,您可以在 iframe 中打开一个页面,将表单发布到 PHP 页面/重定向到 PHP 页面,或者对所述 PHP 页面进行 AJAX 调用。跨度>
  • @Ejay:不,代码是CakePHP,它不打开页面,它调用一个URL来更新数据库。
  • 很失望已经回答了你的问题,看到你决定基本上放弃它,只是再次问同样的问题。如果您有一个与副本不同的特定 问题 - 修剪您的代码示例以说明这一点。原样:-1
  • 我已经有了。请退出 help vampirish 行为 - 如果您想制造火箭,请询问如何使用螺丝刀首先如果您不知道如何使用。

标签: javascript php jquery ajax cakephp


【解决方案1】:

我建议首先为您的 Acertei 按钮提供一个唯一 ID,例如“acertei”(它目前与其兄弟按钮共享“2”的 ID)。然后使用 jQuery,您可以执行以下操作:

$("#acertei").on("click", function() {
    $.ajax({
        url: "/flip2/correctAnswer",
        success: function(data) {
            // Do whatever you need to do with the response here.
        },
    });
});

这会向 Acertei 按钮添加一个单击事件,该事件将启动对“flip2/correctAnswer”URL 的 AJAX 请求。

【讨论】:

  • 您好,它不起作用。我用你的示例代码更新了我的代码,请看一下。
  • 您的示例看起来不错 - 您遇到了什么错误?
  • 没有错误。该方法应该更新数据库,它没有发生。但是,如果我在页面加载时调用$this-&gt;requestAction('/flip2/correctAnswer')(请参阅注释行),则效果很好。
  • 我需要下载 Ajax API 还是什么?
  • @HashMap 您可能还想阅读ajax-and-cakephp
猜你喜欢
  • 2015-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-14
  • 1970-01-01
  • 2013-09-10
  • 1970-01-01
  • 2011-12-22
相关资源
最近更新 更多