0

I'm trying to send post request with ajax but it doesn't work and I'm so lost. Please I need some help...

index.php:

<button type="button" id="ajaxBtn">Test</button>

main.js:

$('#ajaxBtn').click(function() {
  $.ajax({
    url: test.php,
    type: "POST",
    data: {any : "any"},
    success: function() {
      window.location.href = "./test.php";
    }
  });
});

When I click on the button I'm redirected to test.php but:

var_dump($_POST) // array(0) { } 

PS: I don't want to use GET method.

Thanks for help guys.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Delbo ar
  • 362
  • 2
  • 4
  • 12
  • 1
    in your test.php, do `var_dump($_POST['any']);` which is the variable you defined as your post variable in your AJAX function. also add a "," after your data curly bracket, like so: `data: {any : "any"},`. Further more, the data variable needs to be set with ":" and not "=" in this case. – Martin Aug 24 '18 at 12:28
  • I can't see how a typographical error should merit an upvote. – Funk Forty Niner Aug 24 '18 at 12:28
  • try data: "test=1&test2=2", – unixmiah Aug 24 '18 at 12:29
  • @unixmiah Still not working Edit: If it was a DATA problem, the success fonction would not working, no ? – Delbo ar Aug 24 '18 at 12:34
  • its not the data issue but still you don't get the data, use collon : instead of equal in data, or you can do like data: `"any=any",` – Ahmed Sunny Aug 24 '18 at 12:40
  • My script is with : and I have the , guys. Btw $_POST give all posts – Delbo ar Aug 24 '18 at 12:41
  • try with string, and check in network tab, what response you getting, – Ahmed Sunny Aug 24 '18 at 12:44
  • 1
    remove windows.location and then check, its working but redirecting after success to same page as url – Ahmed Sunny Aug 24 '18 at 12:47
  • 1
    `window.location.href = "./test.php";` is executed AFTER your ajax request is finished. So it is logical that it does not display any POST value (because it is a new request). What exactly do you want to accomplish? If you want to redirect WITH the values, AJAX might not be your goto. – jrswgtr Aug 24 '18 at 12:48
  • This is probably what you want: https://stackoverflow.com/a/133997/8108407 – jrswgtr Aug 24 '18 at 12:56

2 Answers2

2

The redirection happens when after the ajax has been executed. The data is sent to your test.php and then the browser is redirected to test.php. If you want to see the result from your ajax call do not redirect, but instead display the results. You can do for example:

success: function(data) {
  console.log(data);
  // here the result from test.php will be logged in the browser console
}

Alternatively you can put the data on your page when it is returned in the success call. $('#resultContainer').text(data);

Also as pointer by others:
1. URL is a string so put it inside of quotes url: "test.php"
2. JS object notation uses colon (:)

Dharman
  • 30,962
  • 25
  • 85
  • 135
-2

Have you inserted jQuery in your code.

or you can try this method on your click event.

$.post('url', { field1: "hello", field2 : "world"}, 
    function(returnedData){
         console.log(returnedData);
});
Parag Yelonde
  • 34
  • 1
  • 12