0

Good day.

I have a question I couldn't solve it, I'm using Cakephp and $.ajax. The data from $.ajax can be passed in to my database. But the problem is I cant redirect to another page when successful.

I had tried the if($save){ echo something} and is working, just the $this->redirect part.

The Cakephp code as below:

public function testingadd() {
        $this->layout=null;

    $name = $_GET['name'];
    $email = $_GET['email'];
    $phone = $_GET['phone'];

    $this->Newlead->create();


    $this->Newlead->set("name",$name);
    $this->Newlead->set("email",$email);
    $this->Newlead->set("phone",$phone);


    $save = $this->Newlead->save();

    if($save)
    {
        $this->redirect('/Newlead/thankyou');
    }

 }

Ajax code as below:

$("#btn-submit").click(function ()
    {

    var obj = new Object();
    obj.n = $("#inputName").val();
    obj.e = $("#inputEmail").val();
    obj.c = $("#inputMobile").val();

        $.ajax({
            type: 'POST',
            url: '/Newleads/testingadd.json',
            data: {
                'name' : obj.n,
                'email' :  obj.e,
                'phone' :  obj.c
            },
            dataType: "jsonp",
            timeout:1000,
            jsonp:'jsonp'
        });
});

Is there any more code need to be added to any files in Cakephp such as routes.php, PagesController.php or any? I'm still new to this. Please help.

Jenz
  • 8,280
  • 7
  • 44
  • 77
TooJuniorToCode
  • 566
  • 1
  • 5
  • 15
  • 2
    you need to send response back from php to ajax and add a success block in ajax then redirct through ajax – Rakesh Sharma Apr 24 '14 at 04:48
  • @RakeshSharma Response back from php to ajax ? For my 'success: function(data, status, xhr) { //response = data setTimeout(redirect,100); }' 'function redirect() { //alert('Thank you. We have received your details.'); window.location = 'url/thankyou.html'; }' Not working as well, the error is **Uncaught SyntaxError: Unexpected token < URL/thankyou.html:1** – TooJuniorToCode Apr 24 '14 at 04:58
  • so you have a syntex error try to check console why you added a more function in success block – Rakesh Sharma Apr 24 '14 at 04:59
  • related question http://stackoverflow.com/questions/12384606/cakephp-redirect-method-not-redirecting – Fazal Rasel Apr 24 '14 at 06:09
  • @RakeshSharma you mean console.log ? But I can add the data to my database. – TooJuniorToCode Apr 24 '14 at 07:03
  • Always end script after sending a HTTP header (location); – Ryan May 02 '14 at 02:25
  • @RPM Hello RPM, I will keep in mind on the exit() after the HTTP header next time :). Thanks alot. I learned something. – TooJuniorToCode May 02 '14 at 02:36

2 Answers2

0

Redirect on the client via JavaScript, depending on your server response:

Code on Server (controller)

public function testingadd() 
{
    [...]

    if($save)
    {
        return json_encode(array('saveSuccess' => true));
    }
}

Code on Client

$.ajax({
    [...],
    dataType: 'json',
    success: function(data) {   

        if(data.saveSuccess === 'true')
        {
            // the redirect
            window.location = '/Newlead/thankyou';
        }

    },
    error: function()
    {
        alert('an error occured');
    }            
});
Simon
  • 4,157
  • 2
  • 46
  • 87
  • Hi Simon, I've tried but still not working as well... my page alert me the error... if I change the dataType to Json, I will get this : "XMLHttpRequest cannot load http://URL/Newleads/testingadd.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. " If I put dataType: jsonp, this will shows up "Uncaught SyntaxError: Unexpected token : testingadd.json:1 " This have taken me 2 days already, btw thanks for the reply Simon. Appreciated – TooJuniorToCode Apr 24 '14 at 07:01
  • At first, make sure, that your url in `$.ajax` is correct. Alter your `url: "/hostname/controller/action"` e.g. `url: "http://localhost/Newleads/testingadd"` and try request again. – Simon Apr 24 '14 at 07:11
  • Simon, "XHR finished loading: POST" this came up after my $_GET at Controller page couldn't be use, and I change it to $_POST. Database still normal, can add all the input. Just the redirect part ><. Thank you for your time Simon. Appreciate your solution. :) – TooJuniorToCode Apr 24 '14 at 07:51
0

Not sure what the problem is.

But I've solved it.

$.ajax({
[...]
dataType:'jsonp', //have to be Jsonp because I'm using different domain.
error:function(){
  redirect(url);
}

It's a bit awkward to do it like this. But, I'm frustrated about the error I can't find.

Marty
  • 146
  • 1
  • 13
TooJuniorToCode
  • 566
  • 1
  • 5
  • 15