看不到php变量的值

I have a php file, from where when a button is clicked save_match() runs:

function save_match() {
    ...
    $.post('url2', {elements: elements});
    ...
}

and runs another php file, which has these:

<?php
    $tok = 1024;
    if($_POST['elements'])
    {
            echo '<script>foo("'.$tok.'") </script>';  
    ...
?>
<html>
  <body>
    <script type="text/javascript">
    function foo(p1) {
        //alert(p1);
         document.write("|"+p1+"|");
        }     
    </script>
  </body>
</html>

The reason I do this, is because with echo I can't see nothing. Well not that now I see something more.

I have opened the url2 too in a new tab. However, after I press the button in url1 and I go to the tab of the second url, there is nothing changed. I refresh it, but still, nothing is changed!

What can I do? I need this for debugging.

Here is what you can do to see value of the token.

  1. Remove all html tags from your php files as they are not required.
  2. Remove the function foo. and the scripts
  3. Change your code to following.

    <?php
       if (isset($_POST['elements'] && $_POST['elements']))
          echo $tok;
          .....
    
  4. Update your javascript to:

     function save_match() {
        ...
        $.post('url2', {elements: elements}, function($token) {
             console.log($token); // will print your token in javascript console
        });
        ...
     }
    
<html>
  <body>
    <?php
      $tok = 1024;
      if(isset($_POST['elements']))
      {
          echo $tok;
      }
    ?>
  </body>
</html>

This should be working. Make sure doctype and all the rest is set and the variable elements in POST is also set. Maybe the if function just always fails because there is no elements set.

For what you are trying to do u don't really need javascript.

Example of function that waits for Asynchronous action:

function save_match(){
  var somethingToReturn, pushTo = [];
  $.post('url2.php', {saveMatch:1, simple:'column_value_here', anotherProp:'Example'}, function(phpComesBackHere){
    $.each(phpComesBackHere, function(i, o){
      pushTo.push(o.object_property);
    });
    somethingToReturn = pushTo;
  });
  while(1){
    if(somethingToReturn){
      return somethingToReturn;
    }
  }
}

On url2.php its like:

<?php
include 'secure_database_connection.php'; $db = db(); // function db is returning `new mysqli('host', 'username', 'password', 'database');`
if(isset($_POST['saveMatch'])){
  if(+$_POST['saveMatch'] === 1){
    if(isset($_POST['simple'], $_POST['anotherProp'])){
      if($qry = $db->query("SELECT * FROM table_name WHERE column={$_POST['simple']}")){
        if($qry->num_rows > 0){
          while($r = $qry->fetch_object()){
            $returnArray[] = array('object_property' => $r->your_property_here, 'anotherProp' => $r->and_another_property);               
          }
          echo json_encode($returnArray);
        }
        $qry->free();
      }
    }
    else{
      // simple and/or anotherProp hack
    }
  }
  else{
    // saveMatch hack
  }
}
elseif(isset($_POST['somethingElse'])){
  // this could be separate $.post on same page
}
$db->close();
?>