-1

I have three different parts to my page. The first is a button, the second is a JS function (AJAX request) and the last one is a PHP script that is called by the AJAX request.

The PHP script is supposed to call a web service to process the data. Here is the content of the PHP script:

<?php 
    if(isset($_POST['forminfos'])){  

        $client = new SoapClient('URL');
        $client->Function();
        header('Location: localhost/page.php');
    }
?>

The page is not redirected but when I check the console I can see that there is a GET with the localhost/page.php content.

enter image description here

//on button sumbit
var data=$('#forminfos').serializeArray();
$.ajax({
    url: '../func.php',  
    data:data,
    type: 'POST', 
    dataType: 'json', 
    success: function () { 
                 
    },
    error: function(){
                 
    }
});

I expected the page with the AJAX request to be redirected to 'localhost/page.php' but it is not.

Henders
  • 1,195
  • 1
  • 21
  • 27
E.Sophia
  • 49
  • 1
  • 12
  • 5
    there isn't enough code or information here. The ***less*** the good people know, the ***more time*** it takes to provide you with a solution. Which in turn, you're asking us to take a blind shot at an invisible target. – Funk Forty Niner May 27 '16 at 13:09
  • The code is 1 single line which is header('Location: URL'); in a php page but it is not working when I check the console I find the page's content in get – E.Sophia May 27 '16 at 13:10
  • comment #1 still applies. If you're not going to post full code, then check for errors or wait for a clairvoyant. I have no time for guesswork. Good luck, *bye*. – Funk Forty Niner May 27 '16 at 13:11
  • You are just downvoting my question, I am trying to edit the questiona dn insert more code any way thanks – E.Sophia May 27 '16 at 13:25
  • Kind of making it personal we are all here to learn and to exchange ideas not making it personal – E.Sophia May 27 '16 at 13:26
  • 1
    and you're asking yourself why? *lol* that's like someone wanting to get their carburator changed in an engine but didn't bother taking the car in the garage. You gave us next to NOTHING to work with. So... you GET nothing. – Funk Forty Niner May 27 '16 at 13:26
  • 1
    Downvotes are not personal. The question of comments + downvotes has been discussed ad nauseum on Meta. Many folks just choose to DV and move on. Many offer advice. Many try to light the path for newbies. – Jay Blanchard May 27 '16 at 13:27
  • I got your point, maybe I was just so focused on what I think and forgot that I was asking for others help. Thank you if you still can help – E.Sophia May 27 '16 at 13:37
  • Which script calls the `header()` function? Is that the only thing in the php file or are there others? If there is other parts to that script, can you put it into your question too, please? – Henders May 27 '16 at 13:39
  • _"when I check the console I can see that there is a GET with the localhost/page.php content"_ - Could you paste that whole thing? – Henders May 27 '16 at 13:46
  • the only thing I try to do on php is process the data in web service and the header location, I am editing the question with a screen shot of the console – E.Sophia May 27 '16 at 13:48
  • Can you post the script that contains `header('Location: localhost/page.php');`, please? – Henders May 27 '16 at 13:54
  • Are you sure that `$_POST['forminfos']` is actually set? I would add an else clause onto your if statement just to check. Something like this: `if(isset($_POST['forminfos'])){ $client = new SoapClient('URL'); $client->Function(); header('Location: localhost/page.php'); } else { print_r($_POST); }` Then we can look at what is being posted. I'd guess you don't have a variable in there called `forminfos` – Henders May 27 '16 at 14:11
  • Yes, in this case $_POST['forminfos'] is set. – E.Sophia May 27 '16 at 14:25
  • Hmm. Could you tell me what the output of `print_r($_POST)` is? – Henders May 27 '16 at 14:28
  • And you are sure that there are no errors and that you have error logging turned on? [See how to do that here](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – Henders May 27 '16 at 14:29
  • Yes @Henders, thank you for your precious time I just removed the header to JS in the $.ajax I appended done and used window location replace and it is working now. – E.Sophia May 27 '16 at 14:39

1 Answers1

2

The problem is that the file you are doing the redirect in is being called by AJAX. That means that although you are redirecting that request page, the actual page you made the ajax request from is not being redirected. The end of your ajax request results in the script you called being redirect not the page it is requested on.

If you only goal is to get to your destination in the header (localhost/page.php) then you could wait for the AJAX request to finish and do:

window.location.href = "localhost/page.php"

This uses javascript instead to redirect the page on the successful completion of your AJAX request.


Further reading:

Basic JS docs about window

Difference between window location and window replace

Community
  • 1
  • 1
Henders
  • 1,195
  • 1
  • 21
  • 27