Storing form data with PHP Session

2019-09-20 22:52发布

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>

5条回答
Juvenile、少年°
2楼-- · 2019-09-20 23:36

You need to parse the post variables and add them to the session super global, right now you are referencing $_SESSION['post']['phone'] which won't work as you expect.

// Assuming same file, this is session section
if (array_key_exists('submit', $_REQUEST)) {

    if (array_key_exists('phone', $_REQUEST)) {
        $_SESSION['phone'] = $_REQUEST['phone'];
    }
}

// Form section
<?php $phone = (array_key_exists('phone', $_SESSION)) ? $_SESSION['phone'] : null;?>
<input type="text" name="phone" id="phone" placeholder="Enter your phone number here" value="<?php echo $phone ?>" />
查看更多
forever°为你锁心
3楼-- · 2019-09-20 23:44

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.

查看更多
小情绪 Triste *
4楼-- · 2019-09-20 23:44

HTTP is stateless, and data that is not submitted will not remain unless you use a client-side approach (webstorage, etc).

查看更多
霸刀☆藐视天下
5楼-- · 2019-09-20 23:46

I may be wrong, but I think you need to get the posted values from the form by using something like

if ($_POST['errStr']) {
     $_SESSION['errStr'] = $POST['errStr'];
} 

If i'm right its the way you're trying to access the variables after posting the form

If you look at the METHOD attribute of the form it set as post, so this should return the values you want to pass accross pages.

Maybe this isn't what you were asking though, i'm a little unclear what part the problem is with, i'm assuming its taking values from the form and getting them out on the next page.

If you want to do it when the page is refreshed/exited you'd probably need to use some javascript client side to try and catch it before the action happens.

Don't know if this is possible. PHP wont help you for that as its executed server-side, and the client wont submit anything (useful) to the server when they exit/reload, only the command to perform the action.

This'll probably require using javascript listeners, eg window.onclose (although apparently that doesn't work for safari or firefox 2), and within that an ajax xmlhttprequest to send the data to your server.

For failed submit (ie capture form with invalid data in?) its almost the same case as form that submit worked on. Just re-check the data on the other side when you're processing it.

查看更多
看我几分像从前
6楼-- · 2019-09-20 23:47

Either you didn't include the code for submit.php or, if you did, the problem is clear: you're never setting the session values. In order to do that, you'd use

session_start();

// etc...

$_SESSION['post']['name']  = $_POST['name'];
$_SESSION['post']['email'] = $_POST['email'];
$_SESSION['post']['phone'] = $_POST['phone'];

// etc...

on submit.php and then the form page (presumably another page?) could then check if those values have been set

.. value="<?php if (isset($_SESSION['post']['name'])) echo $_SESSION['post']['name']; ?>" ..
查看更多
登录 后发表回答