0

I have the following php codeigniter function code which is being called by a jquery ajax function:

            $this->load->library('session');
            $this->load->helper('url');

              $ids = $_POST['i'];
              $message = $_POST['m'];
              var_dump($message);
              var_dump($ids);
              $sql = 'update replies set `response` = "'.$message.'" where id IN ('.implode( ',', $ids).')'; 
              echo $sql;
              R::exec($sql); // works normally to here


              redirect($this->uri->uri_string());

I want to refresh the page after the db insertion of 'message'. however nothing seems to happen. everything works normally including the db insertion. What am I doing wrong?

3
  • 1
    AJAX calls the resource on the server, the server processes the response and triggers a redirect, AJAX doesn't care about that unless you tell it to. You should return the URL from PHP and use the AJAX success callback to redirect with Javascript if you want to do that. Or just post to PHP normally without AJAX and let PHP do the redirect. Commented Sep 2, 2014 at 21:58
  • Thanks Scrowler, " You should return the URL from PHP and use the AJAX success callback to redirect with Javascript if you want to do that." I'm not that knowledgeable about jquery/js. Can you explain further on how to do this? Commented Sep 3, 2014 at 2:09
  • 1
    Take the example below, but put "url" into the callback function as an argument: function(url){}, then location.href = url;, then in PHP you'll just echo 'http://myurl.com';. Lastly, once you've got all of this working, do some more error checking with JS to make sure you're getting back what you expected. Commented Sep 3, 2014 at 2:34

2 Answers 2

1

You can not redirect via AJAX url. Redirect is possible using callback function in three ways done, fail and always.

Example:

$.ajax({
        url: '/path/to/file',
        type: 'default GET (Other values: POST)',
        dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
        data: {param1: 'value1'},
    })
    .done(function(url) { // echo url in "/path/to/file" url
        // redirecting here if done
        location.href = url;
    })
    .fail(function(url) { // echo url in "/path/to/file" url
        // redirecting here if failed
        location.href = url;
    })
    .always(function(url) { // echo url in "/path/to/file" url
        // redirecting here always
        location.href = url;
    });
Sign up to request clarification or add additional context in comments.

1 Comment

I believe if the user wants to use the redirect function, the value needed for location will be in the response header (not the body). There could be some advantages to responding with a redirect.
0

On your ajax success use this after showing success message

window.location="path_to redirect";

like for example i am redirecting to member controller

window.location="member";

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.