1

I have php file that contains variables:

$t1 =  $_POST['t1'];
$t2 =  $_POST['t2'];

and I need to pass these variables into the respective html form fields:

<input id="field_1" type="hidden" name="field_1" />
<input id="field_2" type="hidden" name="field_2" />

form is located in different page than php file with variables. it's url is "form.php"

I guess I need to create php post command in my php file that redirects to url "form.php" and loads $t1 and $t2 into respective form fields "field_1" and "field_2"?

Trying around but can not find right solution..Any ideas appreciated Brgds, Raivis

1
  • 1
    Are you posting to the form from the server-side code, or are you trying to supply the values to a client-side post in some way? I guess I'm not clear on what you're trying to do. Can you show some code of your attempt so far to help clarify? Commented Jul 25, 2012 at 13:58

2 Answers 2

2
$t1 =  $_POST['t1'];
$t2 =  $_POST['t2'];

<input id="field_1" type="hidden" name="field_1" value="<?php echo $t1 ?>" />
<input id="field_2" type="hidden" name="field_2" value="<?php echo $t2 ?>" />

if the form and the values does not resides in the same file, you need to pass the value from page1.php to page2.php, you can do so by sending the values through URI and fetching it from $_GET method.

assume you have the values in page1.php and want to send the values to page2.php while redirecting then you can do it this way while redirecting.

$t1 =  $_POST['t1'];
$t2 =  $_POST['t2'];
header('Location: http://yoursite.com/page2.php?t1='.$t1.'&t2='.$t2);

now when the page will be redirected to page2.php, you will have the value, you can fetch it using $_GET in page1.php like wise.

$t1 =  $_GET['t1'];
$t2 =  $_GET['t2'];

<input id="field_1" type="hidden" name="field_1" value="<?php echo $t1 ?>" />
<input id="field_2" type="hidden" name="field_2" value="<?php echo $t2 ?>" />
Sign up to request clarification or add additional context in comments.

7 Comments

That introduces an XSS vulnerability … and the question is about accessing the data after a redirect.
Location headers take an absolute URI, not a relative one. You aren't make the variables URI safe. You still have an XSS vulnerability.
not true fully, according to PHP documentation HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs i have used relative path many a times and it have worked without any problem for me, in PHP documentation there are may examples which uses relative URI.
– Depending on clients to perform error recovery isn't a good idea, even if you haven't found one that failed to do so.
after digging a bit, people say using trailing slash / for relative URI in location header does the trick. here is the supporting link, stackoverflow.com/questions/1250393/to-understand-phps-header , however thank you for correcting me, i don't deny that absolute URI should be it if you can in location header.
|
0

You can pass data around to other pages with POST and then resend the POST data through forms with hidden fields. Like a registration process with multiple steps.

But why don't use sessions? It's more clear and easier to work with.

  1. form.php, had the form and on submit will post data to form2.php
  2. form2.php receives the POST data, checks for validation and sets the session variables.
  3. now you can access this data every where in your site while using sessions.

To start and set a session variable:

session_start(); //at top of page

$_SESSION['name_of_field'] = $_POST['name_of_field']; //do checks on the post data!

To use a session varible:

session_start(); //at top of page

$my_new_value = $_SESSION['name_of_field'];

More info about sessions

2 Comments

Oooh, potential race conditions. Also watch out as you run into the EU Cookie law minefield.
Ah yes the cookie law.. The cookie would only hold the Session ID of the session on the server. If I recall correctly is that the law thing is about when storing user/track data in the cookie, that you have to ask the visitor to confirm..

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.