-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?

4
  • 7
    You have $_POST['value'] and $_POST['variable'], which one is correect? Commented Aug 13, 2013 at 11:57
  • emmm, no mater... I just deleted if statement and also got null in var_dump Commented Aug 13, 2013 at 12:00
  • Where exactly are you checking for the output? Are you expecting it to be there right in the same page underneath the script? Commented Aug 13, 2013 at 12:04
  • @deceze, yes have javascript and php code on the same page Commented Aug 13, 2013 at 12:05

1 Answer 1

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.