-2

I have one question.. I want to pass javascript variable to php script, on the same page as javascript is located... I have second code:

<script type="text/javascript">
    function getID(i)
    {
        var table = document.getElementById("tblPersons");
        var row = table.rows[i];    
        alert(row.id);

        //window.location.href = window.location+"?id="+row.id;

        $.ajax({
        type: 'POST',
        url: 'index.php',
        data: {'variable': row.id},
        });
    }
</script>

$selected_row = $_POST['variable'];
echo $selected_row;
                            }

but if I try to var_dump $_post['variable'], I got null in echo...

so can anyone help with my problem?

vladimir
  • 695
  • 3
  • 15
  • 34

1 Answers1

0

See Reference: Why does the PHP (or other server side) code in my Javascript not work?

The code is executed like this:

  1. server executes any PHP (including your var_dump, which echoes nothing)
  2. servers sends resulting HTML/Javascript code to browser
  3. browser parses HTML and executes Javascript
  4. Javascript sends AJAX request to server
  5. server receives request and executes PHP code, this time outputting something
  6. Javascript receives the response of the AJAX call (no visible output yet)
  7. you do nothing with the received result

Watch the Network tab of your browser debugging tool of choice (Firebug for Firefox, Web Inspector for Chrome/Safari) to see what's really happening.

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889