UserNew
UserNew

Reputation: 83

open another view responding to ajax request cakephp

I'm sending ajax request to post some form data into cakephp controlller and then move the user to another page to upload some images. Here is what I did :

1- in the add.ctp view of feedbacks/add controller/action , i added the javascript code. I used jquery ajax and sent the form data

2- in the add action in the feedbacks controller I wrote this code :

public function add() {

        if ($this->RequestHandler->isAjax()) {

//commenting the next 3 lines has no effect
$this->layout = 'ajax'; // Or $this->RequestHandler->ajaxLayout, Only use for HTML
                $this->autoLayout = false;
                $this->autoRender = false;

            $response = array('success' => false);

            $data = $this->request->input(); // MY QUESTION IS WITH THIS LINE
           // var_dump($data);

            $this->render('/Uploads/add');
            // exit();
        } else {...} 

Now I can see the response in firebug as html but the browser does not redirect.

Can any one tell me wt is the problem here ??

Thanks

Upvotes: 0

Views: 1733

Answers (1)

Luc Franken
Luc Franken

Reputation: 3014

Generally when you use this kind of form submits you want to work with JSON. That's the standard used implementation with jQuery we use.

Try to develop a good JSON response, http://book.cakephp.org/2.0/en/views/json-and-xml-views.html

In jQuery wait for the success to happen:

jQuery.ajax({
    url:baseUrl + '/Uploads/add',
    type:'POST',
    data: dataInModel,
    success: function(data) {
        alert('you got success!');
        console.log(data);
        //example reloading uploads:
        $('#uploads').load(baseUrl + '/Uploads/index');
    }
});

In your success function you do whatever you want. So for example you could put in a reload, redirect etc. But that's not really ajax. If you added an upload it would for example be ajax like if you would reload the list with uploads only. That's a raw example I added in the code example above.

Upvotes: 2

Related Questions