Page 1 of 1

Accessing hidden variables from a preceeding webpage

Posted: Wed Dec 02, 2009 8:37 pm
by DrPL
Hi,
I have absolutely no idea where to put this, as it encompasses php and javascript, but I think the former is more applicable as I am having trouble accessing hidden variables passed
to the next webpage.

What I am trying to do is this: I have a webpage that manipulates some data using javascript, and these data are then passed immediately (without user prompting) to the next page, where a php page picks up the hidden values.

What I have so far is:

transfer_file.php

Code: Select all

 
<html>
 <body>
 
<script type="text/javascript">
        jsvar=10;
        document.form1.data.value = jsvar;
 /* Just a test - alter the hidden value */
    </script>
 
<form method="POST" name="form1" action="transfer_file2.php">
<input type='hidden' name='data' value=''>
</form>
 
function sendData()
{
//alert(document.form1.data.value);
document.form1.submit();
}
 
<?php
header("Location: transfer_file2.php"); // immediately forward the user to the next page
?>
</body></html>
The next page is transfer_file2.php

Code: Select all

<?php
$myvar = $_POST['data'];
    echo "myvar: ".$myvar;
 
?>
Naturally, there are problems! On the transfer_file2 page, I get:
Notice: Undefined index: data in C:\wamp\www\transfer_file2.php on line 2
myvar:

Can someone please help suggest a fix?

Thanks!

Paul

Re: Accessing hidden variables from a preceeding webpage

Posted: Thu Dec 03, 2009 2:45 am
by sergio-pro
Hi

You are trying to redirect using header location _immediately_ after displaying a page.
You can't do this.
In php script you can do (in this context) only one of two things:
1. Show page content (html)
2. immediately redirect to another page - but not showing anything on current page.

All you need to do is client side redirect (through form submission).
like this:

Code: Select all

 
<html>
 <body>
 
<form method="POST" name="form1" action="transfer_file2.php">
<input type='hidden' name='data' value=''>
</form>
 
<script type="text/javascript">
  document.form1.data.value = 10;
 
function sendData()
{
   document.form1.submit();
}
 
sendData();
 
</script>
 
</body></html>
 
 
 

Re: Accessing hidden variables from a preceeding webpage

Posted: Thu Dec 03, 2009 9:19 am
by DrPL
Thanks, that worked. I don't know how I can have missed that... :(