Jump to content

Remembering $_POST data


Love2c0de

Recommended Posts

Good morning,

 

I have a simple 1 page website which I'm working on and I'd like to be able to fill the input/textarea field(s) with any $_POST data should they get re-directed back to the form.

 

I've tried adding:

 

<input type="text" value="<?php if(isset($_POST['name'])){ print($_POST['name']); } ?>" />

 

but it doesn't seem to be working. I'm using header("Location: ../index.php#contact_form"); within my action script if any of the required fields are empty.

 

Here is my full code:

 

<form id="contact_form" method="post" action="core/process_form.php">
    <p><label for="name">Name:</label><input type="text" name="name" id="name" />* <span class="error">Cannot be empty!</span></p>
    <p><label for="number">Number:</label><input type="text" name="number" id="number" /><span>( Optional )</span></p>
    <p><label for="email">Email:</label><input type="text" name="email" id="email" />* <span class="error">Cannot be empty!</span></p>
    <p class="textarea"><label for="comment">Comment:</label><textarea name="comment" id="comment" cols="35" rows="10"></textarea></p>
    <p class="input"><input type="submit" value="Send" onclick="return process_form()" /><input type="reset" value="Clear" /></p>
</form>

 

Action Script

 

//get post data

echo "<pre>";
print_r($_POST);
echo "</pre>";

foreach($_POST as $k => &$v)
{
    $v = trim($v);
}

if($_POST['name'] == "" || $_POST['email'] == "")
{
    header("Location: ../index.php#contact_form");
    die();
}

 

Thank you in advance for any info!
 

Regards,

 

L2c.

Link to comment
https://forums.phpfreaks.com/topic/281418-remembering-_post-data/
Share on other sites

POST data is not saved during a redirect. You have to display the form immediately if you want to make it "sticky" (as it's commonly called). No redirections.

 

[edit] Yeah, or you can take cheap ways out and, eg, stuff the data in the session, but I hate that.

Thank you requinix,

 

Is it bad practice to use the session for the data or is it something which is accepted to use?

 

When you say 'sticky', if I was to do the form processing within the same file, could I create that affect and would it be a more efficient way to do it?

 

Thank for reply.

 

Kind regards,

 

L2c.

  On 8/21/2013 at 2:36 AM, Love2c0de said:

Is it bad practice to use the session for the data or is it something which is accepted to use?

It's as acceptable as it is easy to do, that's really what it comes down to. Mind you the user can't be browsing more than one of these forms at once, and there'll be a magical behavior where the form is filled out for them when they might not expect it, but most of the time that's okay. (There are workarounds but they have issues too.)

 

Essentially you pick a place in $_SESSION and stuff $_POST in there.

$_SESSION["that one form over there"] = $_POST;
Then your form looks to the session to pre-populate the form before using its defaults. It's called "sticky" because the values "stick" between visits to the form.

 

  On 8/21/2013 at 2:36 AM, Love2c0de said:

When you say 'sticky', if I was to do the form processing within the same file, could I create that affect and would it be a more efficient way to do it?

That's the way I prefer to do it. Means you don't pollute the session with what is really just temporary information. The drawback is that users get one of those "resubmit form data" warnings if the come back to the form and simply try to refresh the page, but that's exactly what the warning is supposed to be for anyways.

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.