【问题标题】:run ajax under jquery on CakePHP在 CakePHP 中运行 ajax 和 jquery
【发布时间】:2014-01-07 18:00:15
【问题描述】:

我正在尝试使用 jquery 从 ajax 运行我的 question.php。 这段代码完美运行

<script>
$(':radio').click(function() {
  if($(':radio:checked').length === 1) {
    var val = $(this).val();
    alert(val);
  }
});
</script>

但是当而不是

警报(val);

我正在放 ajax 代码,这里是相同的示例,但有变化

<script>
$(':radio').click(function() {
  if($(':radio:checked').length === 1) {
    var val = $(this).val();
    $.ajax({
        type: "POST",
        url: "questions.php",
           //data: {"a":val}
        });
  }
});
</script>

ajax 不运行 questions.php

questions.php位于webroot目录

编辑: 看起来它返回了我的所有页面,但我的表格没有发生任何变化 questions.php负责向mysql表中插入数据

questions.php

<?php
    $selected_button = $_POST['a'];
    $conn = mysql_connect('localhost','root','');
      if(!$conn )
       {
         die('Could not connect: ' . mysql_error());
       }

   $sql = "INSERT INTO answers (member_id, question_id, answer) VALUES (1, 2,3)";

   mysql_select_db('ipad',$conn);
   $result = mysql_query($sql, $conn);
   if(!$result)
     {
        return false;
     }
   return true;

   mysql_close($conn);
 ?> 

问题已解决 问题在于指向文件

<script>
$(':radio').click(function () {
        if ($(':radio:checked').length === 1) {
            var val = $(this).val();
            $.ajax({
                type: "POST",
                url: "../../../questions.php",
                data: {"a": val}
            })
                .error(function (msg) {
                    alert(msg);
                })
                .done(function (msg) {
                    alert(msg);
                });
        }
    });
</script>

【问题讨论】:

  • questions.php 或 question.php ?
  • 你遇到了什么错误,试试 firebug 来捕捉错误
  • ofcourse questions.php ,我在这篇文章中犯了错误,我的siede上的所有东西都有questions.php

标签: php jquery ajax cakephp


【解决方案1】:

试试下面的代码,url 需要正确。检查 cakephp 路由正确的格式是什么,controller/action

  $(':radio').click(function () {
            if ($(':radio:checked').length === 1) {
                var val = $(this).val();
                $.ajax({
                    type: "POST",
                    url: "questions.php",
                    data: {"a": val}
                })
                    .error(function (msg) {
                        alert(msg);
                    })
                    .done(function (msg) {
                        alert(msg);
                    });
            }
        });

我的测试代码

<script src="jqueryPATH"></script>
<script>
    $(document).ready(function(){
        $('#hello').on('click', function () {
            $.ajax({
                type: "POST",
                url: "questions.php",
                data: {"a": 1}
            })
                .error(function (msg) {
                    alert(msg);
                })
                .done(function (msg) {
                    alert(msg);
                });
        });
    });

</script>
<a href="#" id="hello">hello</a>

questions.php

<?php
$selected_button = $_POST['a'];
echo $selected_button;

【讨论】:

  • 我更新了我的信息。在你输入代码后,我收到了我的漏洞页面上下文
  • 你在 questions.php 上添加了 php 标签吗? @KordianWikliński
  • 我不明白。我的 questions.php 文件中有开始和结束标记
  • 现在我知道哪里出错了,我的路由有问题。 ajax 正在调用 localhost/konkurs/members/start/2/questions.php 的 localhost/questions.php 你知道如何指向 webroot 吗?我试过 "questions.php ?> 但它返回的是 windows 位置路径
【解决方案2】:

您需要将要运行的代码放入控制器中的函数中,然后使用 url 字段调用该函数。因此,例如,如果您将代码放在 questions_controller.php 的测试函数中,它将是

网址:'/问题/测试'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多