0

I have the following code:

<?php

    $id = $_GET['id'];

?>
<html>
    <head>
        <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    </head>
    <body>
    <script>
    var id = $.urlParam('id');

    var id = <?php echo $id; ?>;

    $.post("api.php", { id: id });

    $.ajax({                                      
      url: 'api.php',                        
      data: "",                        
      dataType: 'json',                      
      success: function(data)          
      {
        var path = data[0];         
        alert(path);
      } 
    }); 
    </script>
    </body>
</html>

I have a variable in the URL called id (for example http://www.url.com?id=1) and I'm trying to send it to a page called api.php where I have the database query. Then I'm trying to get the corresponding value of this id (in my case path) and return it. However it seems that the variable id is not being passed to api.php. I have hardcoded the id variable in api.php ($id = 1), the query is working fine and after that I'm able to get the value from the database, but when in api.php I put the following:

<?php

  if($_POST) {
     $id = $_POST['id'];
  }

?>

Then it's not working. I'm not sure if the $.post() method is not working or for some reason I'm not able to get the value of the id variable in the PHP script.

Can somebody help me to solve this out?

Regards, Ivan

5
  • You define id twice in JS. If you look at ajax request is the value there? Commented Jul 23, 2015 at 20:49
  • $.ajax() does a get request, not post. that means $_POST will NOT be set when you do your .ajax() call. Commented Jul 23, 2015 at 20:51
  • can't use $.urlParam('id') if you haven't included that plugin function in page. Will throw exceptions and break your code. Also you have back to back ajax requests...the second with no data and a GET and the first one has no done callback Commented Jul 23, 2015 at 20:51
  • You missed the .ajax option type : 'post' Commented Jul 23, 2015 at 20:55
  • @ToniMichelCaubet not to mention any data... Commented Jul 23, 2015 at 20:57

2 Answers 2

3

First of all, your $.post() call has no callback, so even though the POST request is made, there is no code to execute when it's successful.

Your second, $.ajax() call does not specify a type parameter (or any data for the call), and as a result will use GET. Inside your api.php file, you're checking the $_POST super-global, which of course does not contain the value for id, since you're using GET, not POST.

You should update your $.ajax() call as follows:

$.ajax({                                      
    url: 'api.php',                        
    data: { 'id': <?php echo $_GET['id'] ?> },                        
    dataType: 'json',   
    type: 'post'               
}).done(function(data) {  
    var path = data[0];         
    alert(path);
});

(You don't need the first $.post() call, I think you have confused the usage of this function).

Also, you'll need to actually output something from api.php in order to access it in JS, for example:

<?php
if( isset($_POST['id']) )
{
    echo $_POST['id'];
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

@khan Huh? I'm fully aware that $.post() has a callback, but the OP did not specify one in his code... That explains why he doesn't see anything happening when the AJAX request is done.
Sorry i missed your while reading the first line of your answer. Upvoting it.
0

Here's where I think you may be confused.

$.post() is a shortcut method for using the full $.ajax. You use one or the other, not both together.

To use $.post you need a callback for the data returned

Try removing $.ajax and doing

$.post("api.php", { id: id })
    .done(function(response){
        alert(response);
    });

Now in your php you don't show any output to the page.

So try:

<?php

  if(isset($_POST['id']) ) {
     $id = $_POST['id'];
     echo $id;
  }

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.