How to store a session value in the textbox when the page is loaded?
I am having a search text box. In that box, I want to store the previous search value when the page is loaded next time. For that i created session for the textbox value when the search button is cl开发者_如何学JAVAicked. But i dont know how to replace the session value in the textbox when the page is next loaded. Help me. Any help will be highly appreciated.
<?php
session_start(); // must be before any other output
$search_term = '';
if (!empty($_SESSION['last_search_term']))
{
// Check for last search term and update the search_term var
// Escaped from @Eli's suggestion - Thanks!
$search_term = htmlspecialchars($_SESSION['last_search_term'], ENT_QUOTES);
}
?>
<form method="get" action="page.php">
<input type="text" name="query" value="<?php print $search_term; ?>" />
<input type="submit" value="Search" />
</form>
<?php
if (!empty($_GET['query']))
{
// Form submitted
// Any necessary search logic here
$_SESSION['last_search_term'] = $_GET['query'];
}
?>
As stated in the comments, session_start() must be placed before any other output or you will be presented an error. The last PHP block could be above the form. It's up to you and your particular workflow.
<input type='text' style='' name='' id='' class='' value='<?php echo(htmlspecialchars($_SESSION["Whatever"], ENT_QUOTES)); ?>' />
精彩评论