How do you handle a PHP session update on a form submit?
Hey Guys, I have this form that when I submit will put into a session variable the items in the form. The form has a self target so after clicking submit, it just refreshes the page.
But I need the page 开发者_开发问答to be refreshed with the newly added session variable items to be echo'd back out. Currently I would need to click refresh again for it to echo out because the session was updated on the 1st session and not yet available until the next page reload.
So how can I achieve what I want? I hope this makes sense...
thanks!
It sounds like you're just wanting to be able to know if the form was submitted?
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
session_start();
$_SESSION['field1'] = $_POST['field1'];
echo "field1 = " . $_SESSION['field1'];
} else {
// show form here
}
?>
Session data is actually stored on the server so you don't need to refresh the page to access the data in the session.
try like this. i just created an example.
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<?php
if (isset($_POST['action']) && ($_POST['action'] == 'Engaged')) {
$_SESSION["try"] = 2;
}
echo $_SESSION["try"]; // print after saving or doing the action
?>
<form action="" method="post" name="record">
<tr>
<td>
<input type="submit" value="Engaged" name="action" />
</td>
</tr>
</form>
</body>
</html>
精彩评论