-1

i have this simple code:

var addUser = "simply";
$.ajax({
    type: 'POST',
    url: 'userControl.php',
    data: {addUser: addUser},
    success: function(response){
       alert("success");
    }
});

this is the page that i POST to: userControl.php, the if statement not entering so i didnt post? but i got alert message "success".

<?php
session_start();
if($_SERVER['REQUEST_METHOD'] == 'POST'){ //if page was requested with POST
echo $_POST['addUser'];
}
?>

I got the alert message "success", but the data is not POST to the other page. Im also try out to write 'simply' with one apostrophe. in another page in the same project i have a same Ajax and its working, so why this one not working? any ideas? i need to do it only with ajax, submit with form will not help me. thanks

Raz Nagar
  • 1
  • 2
  • what is the `windows.location` for?? that's not the way ajax works! get rid of that line! – Jeff Oct 27 '16 at 11:16
  • use async: false, //blocks window redirect in ajax call check link http://stackoverflow.com/a/35858496/5700401 – Abhijit Jagtap Oct 27 '16 at 11:17
  • 2
    if you lose that `windows.location` and change the alert to `alert(response);` you should see `"simply"`. – Jeff Oct 27 '16 at 11:17
  • i try out: 'async: false,' Still not working. and the 'windows.location' is after the ajax blocks, i need to use this to 'redirect' to the right page. – Raz Nagar Oct 27 '16 at 11:32
  • Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server? – Jay Blanchard Oct 27 '16 at 12:03
  • thx alot for the help, but its stil not working, i tried to do data: 'addUser=' + addUser but its didnt work and the ajax didnt even alert the success. also tried **data: {'addUser': addUser},** and its didnt worked. i include the jQuery library in the project twice, but in Different pages (i try to remove the include from the second page and still not working. there is no error reported on the console.log() only error in the userControl.php page. – Raz Nagar Oct 27 '16 at 12:16
  • that is the error: **Notice: Undefined index: addUser in C:\xampp\htdocs\Project\userControl.php on line 3** i running the project in localhost – Raz Nagar Oct 27 '16 at 12:18
  • try a var_dump of $_POST, probably now you have to decode the object catched in post – GiuServ Oct 28 '16 at 08:41
  • `var_dump($_POST);` return: `array(0) { }` – Raz Nagar Oct 28 '16 at 08:57
  • so no data is coming to your page, that's why undefined index. How are you submitting data? as in question? – GiuServ Oct 28 '16 at 09:53
  • `$("#button").click( function() { //the ajax is here } );` when i click on button. i dont understand why it is not working, in a different page in the same project i have identical ajax and its working. what can be the difference? – Raz Nagar Oct 30 '16 at 08:27

1 Answers1

-1

Pass data this way data: 'addUser=' + addUser

see below code

var addUser = "simply";
$.ajax({
    type: 'POST',
    url: 'userControl.php',
    data: 'addUser=' + addUser,
    success: function(response){
       alert("success");
    }
});

and you should remove window.location because it is possible before your ajax call completes page will redirect

Jazzzzzz
  • 1,593
  • 15
  • 16