-1

I have eventually managed to implement an Ajax call in my code but I'm slightly confused how to redirect after the call has been made, an example of my script is seen below, I am developing using CodeIgniter.:

<script type="text/javascript">
  function myFunction() {
    var form_data = $('#form1').serialize();
    $.ajax({
      type: "POST",
      dataType: "text",
      url: "testproject/main/test",
      data: form_data,
      success: function(response){
        window.location("http://www.google.com");
      }
    });
    window.alert("sometext");
  }
</script>

within my test() php function can I have a redirect? if not how can I pass information back to the JS function to redirect? also as a final question how could I use a query such as this to dynamically load html into my page? (the final question is more me being inquisitive/nosey). Many thanks in advance and please exercise patience as I am a complete newbie!

EDIT: my Ajax query executes without an issue however - my code never executes:

success: function(response){
  window.location("http://www.google.com");
}

do I have to return response in php somehow?

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
TotalNewbie
  • 1,004
  • 5
  • 13
  • 25
  • 1
    first its `window` not `windows`, second `window.location="some url"` not `window.location("")`, you should have been getting a _"ReferenceError: windows is not defined"_ (as you are using `windows`) or _"Property 'location' of object [object global] is not a function"_ or similar error in your console. – Patrick Evans Apr 01 '14 at 16:53
  • ok I'm still looking for whether I code redirect from within PHP? – TotalNewbie Apr 01 '14 at 16:56
  • 1
    If you are wanting to redirect right after the post, why not just do a normal form post why use ajax? You use ajax so the user can stay on the same page and do other stuff. – Patrick Evans Apr 01 '14 at 17:00
  • @PatrickEvans Not necessarily, you may want to do validation, or post to one page and redirect to another. – Styphon Apr 01 '14 at 17:01
  • because I'm executing the ajax call outside of a form and wish to redirect after the data has been posted - I don't wish to use an additional form – TotalNewbie Apr 01 '14 at 17:02

2 Answers2

1

A quick google on how to redirect in javascript would have revealed this. Your javascript is simply written wrong. You need:

window.location.href = 'http://www.google.com';

As for redirecting inside PHP, no you cannot do that. You would need to pass it back to the javascript for that to redirect. You do this by echoing out the page you wish to go to on your PHP page and then redirecting to that.

So on your PHP page you have:

<?php echo 'myurl.html'; ?>

Then in your Ajax you have:

window.location.href = response

as response will have whatever is echo'd out on the PHP page.

Community
  • 1
  • 1
Styphon
  • 10,304
  • 9
  • 52
  • 86
  • Hi - yeah my issue is that line of code is never being reached - I know that my code is executing the ajax query but thats as far as it gets – TotalNewbie Apr 01 '14 at 16:59
0

try this:

<script type="text/javascript">
    function myFunction()
    {

        var form_data = $('#form1').serialize();

         $.ajax({
             type: "POST",
             dataType: "text",
             url: "testproject/main/test",
             data: form_data,
             success: function(response){
                 window.location.href="http://www.google.com";   
                   // or window.location.assign("http://www.google.com");
             }
          })
         alert("sometext");
   }
</script>
Arka
  • 581
  • 5
  • 18