0

I have the following ajax code sending data to php via post:

          checked_id = 10;
          $.ajax({
               url: "/delete_document",
               type: "POST",
               data: {id:checked_id}, // a
               success: function(data, textStatus, jqXHR){
                    alert(data);
                    location.href='/delete_document';
               },
               error: function (jqXHR, textStatus, errorThrown){
                    //alert('Error!')
               }
          });

it prepares the data successfully, and the correct value is alerted (10).

Then i should fetch this value in the php file:

$selected_id = $_POST['id'];
var_dump($selected_id);

but var_dump gives me string(0) "".

I know that when the code doesn't work, it's always me the one that's doing it wrong. But I can really not see the mistake here.

Lazarus Rising
  • 2,177
  • 4
  • 23
  • 50
  • Have you tried `var_dump($_POST)` and seeing what is sent through? – Ben Jan 07 '16 at 13:57
  • @Saty It is a routing thing, so that is not a problem. the file delete_document is recognized as /delete_document. I tried it by echoing some random string. – Lazarus Rising Jan 07 '16 at 14:02
  • @Saty why? He may be using url rewriting. Anyway, he gets **string(0) ""** as `var_dump` result which means, that PHP code is being parsed. – Jakub Matczak Jan 07 '16 at 14:03
  • @amygrimaldi did you checked in Firebug (or similar) that `id` is indeed properly sent to server? – Jakub Matczak Jan 07 '16 at 14:04
  • @BenPearlKahan I tried that too, it gives me NULL . So maybe the value is not being send or received at all. As a side note, using isset($_POST['id']) and an if(){} else{} indicates that the value is not set – Lazarus Rising Jan 07 '16 at 14:06
  • @dragoste I tried using firebug. Had to remove the redirect line from ajax code, or i didn't have the time to see the results. But what I got was this: http://postimg.org/image/kua9l6l2h/ If I keep the redirect, once i go to the delete page, the console section is empty – Lazarus Rising Jan 07 '16 at 14:14
  • 1
    So it is working fine. I see what is the problem. After you redirect to `'/delete_document'` it is not a POST call anymore and this script is called second time, without POST data this time. – Jakub Matczak Jan 07 '16 at 14:19
  • Why are you redirecting to delete_document after posting to it? – epascarello Jan 07 '16 at 14:26
  • @epascarello I am redirecting because once the id is send, I need to perform other tasks depending on this id. What would you suggest doing instead? – Lazarus Rising Jan 07 '16 at 14:28
  • Why are you not just posting a form their directly?? The first post you will have the if sent up, when you redirect there is no post and there will be no id. The post and get request are not linked in anyway. – epascarello Jan 07 '16 at 14:30
  • @epascarello due to other parts of the code whole, this cannot be done through a form. – Lazarus Rising Jan 07 '16 at 14:46
  • There is a disconnect in your workflow and I am not sure why you can use a form. When you post the form with Ajax, you should have the id. When you redirect the page, you are not going to have the id. I have a feeling that is the issue you have and I am not sure if you are not realizing that. What happens on the second step that can not be done on the first. What is the initial post doing? Why can you not just post the page? – epascarello Jan 07 '16 at 14:50
  • @epascarello The id is not taken from a form at all. I have a table with many rows and one single delete link. I get the id from the selected row when clicking the delete link with jquery and have to send it to a code that will perform the delete and other tasks. – Lazarus Rising Jan 07 '16 at 14:57
  • Problem is you are not passing that id when you do the redirect. Do you understand that? Is that where the value is not available? If you remove the redirect, is there an issue (other than the fact your second part is not running)? – epascarello Jan 07 '16 at 14:58

1 Answers1

2

The thing is that what you eventually see after this code has completed is not a result of POST ajax call, but it is the result of redirection in line

location.href='/delete_document';

And this makes a second call to /delete_document URL which is this time simple GET request without the POST data that you expect it to have.

Edit.

Instead of making an ajax call and redirecting to that page you want to move to /delete_document with POST data. You can't do that with redirecting so you should make a form and submit it in JavaScript.

Here's an example of what you're trying to do: JavaScript post request like a form submit

Community
  • 1
  • 1
Jakub Matczak
  • 14,327
  • 5
  • 42
  • 55
  • But he wrote that he got 10 as alert output. – Ravi Jan 07 '16 at 14:23
  • Yes, before redirect. – Jakub Matczak Jan 07 '16 at 14:24
  • If var_dump($selected_id); is present in that file then how can he get 10 as alert output? – Ravi Jan 07 '16 at 14:24
  • @RaviHirani I think the output was during the posting, before redirecting. – Lazarus Rising Jan 07 '16 at 14:24
  • @dragoste Thanks, for pointing this out. But if it is not too much asking, how can i make it so that I get the values in the /delete_document? I have to redirect because there is where i should get the id and do many other things. (using GET and sending the value via url is not an option) – Lazarus Rising Jan 07 '16 at 14:26
  • @dragoste: I think he has a grid in a page. now he want to delete specific row and after successful delete he wants to reload the whole page. – Ravi Jan 07 '16 at 14:40
  • @RaviHirani That is right. I have a table and want to delete rows from it. the id is id of the checked row. – Lazarus Rising Jan 07 '16 at 14:47
  • @amygrimaldi: Do this thing. alert(data); return false; and print your answer in your question. – Ravi Jan 07 '16 at 14:49
  • @amygrimaldi then I don't really see the problem. You have correct data while you're calling ajax. Just after you have deleted this item during ajax call (and did other tasks if needed), refresh the page (redirect to current url) instead of trying to redirect to deletion script again. – Jakub Matczak Jan 07 '16 at 14:58
  • @dragoste Even though I still performed the delete as I had planned, it was indeed the redirection 'erasing' the posted value, so you were really helpful. I redirected via php (from delete.php to another page) after getting the posted data and it worked like a charm. Also, thank you for suggesting Firebug, it will come really handy next time I don't think things through carefully ;) – Lazarus Rising Jan 07 '16 at 15:25