Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

html - Storing form data with PHP Session

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...