douqianzha6213 2012-07-04 08:02
浏览 54

通过jquery ajax调用函数

It's been 2 days now and still I can't get this problem to work. So basically I am trying to call a php function through jquery ajax but nothing is working, I am not sure what is the problem is... here are my codes

the ajax

$(document).ready(function() {
    $("#idCheck1").click(function(){
        $.ajax({
            url:'../../../../Controller/PostsController.php', 
            data: {action: 'update_checkbox'},
            success:function(result){
//                $("#dsa").html(result);
            } 
        });
    });
});

the view

<?php echo $this->Form->create("Posts", array("action" => "update_checkbox", "id" => "checkingBox")) ?>
                    <td>
                        <?php
                        echo $this->Form->input('Post.' . $i . '.id', array("type" => "hidden", "label" => false, "value" => $sum['posts']['id']))
                        ?>
                        <?php
                        echo $this->Form->input('Post.' . $i . '.done', array("type" => "checkbox", "label" => false, "value" => "1", "id" => "idCheck1"))
                        ?>
                    </td>
                </tr>
                <?php
                $i++;
            }
            ?>

        </table>
        <?php echo $this->Form->end(); ?>

the controller

 public function update_checkbox() {
        //    debug($this->data);
        $var = $this->Post->saveCheckBox($this->data);
        $this->set("result", $var);
    }

the model

public function saveCheckBox($checkbox) {
        debug($checkbox);
        $this->saveAll($checkbox['Post']);
    }
  • 写回答

1条回答 默认 最新

  • doudilin1225 2012-07-04 08:27
    关注

    The url

    url:'../../../../Controller/PostsController.php'
    

    looks wrong. Since this is an AJAX request that goes through the browser you can't use relative paths that try to go upwards in the folder hierarchy, as browsers url's don't work that way. You should be making that request so that it is passed through a web server, i.e. the url should look like one of the following:

    url:'http://localhost/Controller/PostsController.php'
    

    or

    url:'/Controller/PostsController.php'
    

    The first option is an absolute url, but this also makes the code a bit less flexible (suppose you change the domain from localhost to something else). The second option is a relative URL, but one that is relative to the domain root of your web server (i.e. in the example it would still resolve to localhost/Controller...).

    In both cases based on what you've posted, your PHP file should live in a Controller/ folder in the document root of your site. The structure of your code however suggests that you are using a framework of some kind (e.g. Zend, Symfony or CodeIgniter)? If that is the case it would be helpful if you post information on what framework you're using as well as that might change the answer.

    UPDATE

    In the case of cakePHP, you should access the controller through the front controller, meaning the URL should look like this:

    url:'/posts'
    

    UPDATE 2

    For the jquery side, a complete ajax request example could look like this:

    $(".idCheck").click(
       function(){ 
          var idVal = $(this).val();
    
          $.ajax({ 
             url:'/posts/update_checkbox', 
             data: {id: idVal }, 
             type: 'POST', 
             success:function(result){ 
                $("#dsa").html(result); 
             } 
          }); 
      });
    

    Note that the URL already contains the update_checkbox action (i.e. the complete url to the action you want to execute), and the data object contains the value of the clicked element that you want to send to the server. If you want to send a complete form you could also use $('#myformselector').serialize() to convert all inputs in the form to a object suitable for the data property of the ajax request.

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line