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

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.