Jump to content

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.

Edited by requinix

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.

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.

 

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