Jump to content

Does POST data clear between pages?


DWilliams

Recommended Posts

For example, say I'm making a simple registration form. My initial input form submits to the same page via POST. After submitting, my page does all the usual input validation.

 

Now, instead of a "correct and resubmit", what if I only want to warn the user that what they entered may not be correct and give them a "yes I'm sure" button? In my error section, can I have a form like the following:

 

<form action="mypage.php" method="post">
<input type="hidden" name="override" value="1" />
<input type="submit" value="Yes I'm sure" />
</form>

 

When the user clicks the button on that form, will the $_POST array I have access to (with name, email, etc etc) currently be available to my script after THIS form is submitted or do I have to manually re-add each value to the form like so:

 

<form action="mypage.php" method="post">
<input type="hidden" name="override" value="1" />
<input type="hidden" name="username" value="{$_POST['username']}" />
<input type="hidden" name="email" value="{$_POST['email']}" />
// And so on...

<input type="submit" value="Yes I'm sure" />
</form>

Link to comment
https://forums.phpfreaks.com/topic/200180-does-post-data-clear-between-pages/
Share on other sites

manually re-enter

 

Hmm that's unfortunate. I guess I could make some function like

 

function PostToHiddenField($postarray)
{
  foreach(array_keys($postarray) as $item)
    echo "<input type=\"hidden\" name=\"$item\" value=\"{$postarray[$item]}\" />";
}

 

I'm sure there's some built-in way to handle it with PHP though...

I like to keep my code as browser-independent as possible

 

Cookies are supported by most (if not all) browsers. Your function will do fine although I would recommend generalizing it more, like:

 

function htmlInput($array, $type = 'hidden') {
  $html = '';
  foreach($array as $name => $value)
    $html .= "<input type=\"$type\" name=\"$name\" value=\"$value\" />\r\n";
  return $html;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.