Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am working on an HTML form whose data would be saved on a failed submit, or page refresh.

We are using a PHP Session to store this data, and we post the elements back when needed.

The issue is that it's not working. We need the form data to be preserved on a submit with errors, or page refresh. Currently on a failed submit, or page refresh, there is no data being stored in the session.

I'm fairly new to PHP, and most of this code is not mine, so go easy on me.

The PHP Sumbit code being used is:

Software: PHPMailer - PHP email class                                    
Version: 5.0.2                                                          
Contact: via sourceforge.net support pages (also www.codeworxtech.com)  
Info: http://phpmailer.sourceforge.net                               
Support: http://sourceforge.net/projects/phpmailer/  

SESSION:

<?php

session_name("fancyform");
session_start();

$str='';
if($_SESSION['errStr'])
{
$str='<div class="error">'.$_SESSION['errStr'].'</div>';
unset($_SESSION['errStr']);
}

$success='';
if($_SESSION['sent'])
{
$success='<div class="message-sent"><p>Message was sent successfully. Thank you!  </p></div>';

$css='<style type="text/css">#contact-form{display:none;}</style>';

unset($_SESSION['sent']);
}
?>

FORM:

<form id="contact-form" name="contact-form" method="post" action="submit.php">
<p><input type="text" name="name" id="name" class="validate[required]"     placeholder="Enter your first and last name here" value="<?=$_SESSION['post']['name']?>" /></p>
<p><input type="text" name="email" id="email" class="validate[required,custom[email]]" placeholder="Enter your email address here" value="<?=$_SESSION['post']['email']?>" /></p>
<p><input type="text" name="phone" id="phone" placeholder="Enter your phone number here" value="<?=$_SESSION['post']['phone']?>" /></p>
<p>
<select name="subject" id="subject">
<option>What event service are you primarily interested in?</option>
<option>Event Creation</option>
<option>Project Management</option>
<option>Promotion</option>
</select>
</p>
<textarea name="message" id="message" class="validate[required]" placeholder="Please     include some details about your project or event..."><?=$_SESSION['post']['message']?>    </textarea>
<input type="submit" value="" />
</form>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
226 views
Welcome To Ask or Share your Answers For Others

1 Answer

You are outputting $_SESSION['post']['form_element'] to your template but in the above PHP code there is no mention of setting that data. For this to work, you would have to loop through the $_POST array and assign each key pair to $_SESSION['post']

At that point you should be able to access the previously submitted form data from the session just as you have coded above.

add this to your submit.php:

session_start();    
foreach ($_POST AS $key => $value) {
 $_SESSION['post'][$key] = $value;
} 

This will move all the data from $_POST to $_SESSION['post'] for future use in the template, and should be all you need to get it working.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...