Catherine
Catherine

Reputation: 45

Call a function through 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']);
    }

Upvotes: 0

Views: 1247

Answers (1)

Jens Wegar
Jens Wegar

Reputation: 4872

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.

Upvotes: 2

Related Questions