[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 652
  • Last Modified:

PHP Get textbox value after submit

Hi,

After submitting my search form, I want to retain the value of the search in the textfield.

Also, just to let you know, at the end of the search where you click next/previous button, the querystring contains the search words. I'm using PHP.

Any help appreciated, thanks.
0
Fezi
Asked:
Fezi
1 Solution
 
amiguraCommented:
replace search varible

if($_POST['search varible']){$search_varible=htmlspecialchars($_POST['search_varible'], ENT_QUOTES);}
else{$search_varible=htmlspecialchars($_GET['search_varible'], ENT_QUOTES);}

Open in new window


search box

<input name="q" type="text" size="20" value="<?php echo $'search varible; ?>">

Open in new window

0
 
ukerandiCommented:

<form action="<?php echo $_SERVER['$PHP_SELF'];?>" method="GET">
<input name="search" type="text" size="25" value="<?php echo $_GET['search']; ?>">

<input type="submit" value="submit">
</form>
0
 
Ray PaseurCommented:
Here is the general design pattern.  All search requests should be GET method requests.  
http://www.laprbass.com/RAY_temp_fezi_GET.php

From the look of this and your other question I answered, it looks to me like you would really enjoy this book.  Very readable with excellent examples and a code library that you can download and modify for your own uses!
http://www.sitepoint.com/books/phpmysql4/

PHP also has some good online tutorials.  See especially "Dealing With Forms" on this page.
http://php.net/tut.php

Good luck with your project, ~Ray
<?php // RAY_temp_fezi_GET.php
error_reporting(E_ALL);
session_start();

// INITIALIZATION
$_SESSION["q"] = NULL;

// IF THERE GET REQUEST COPY IT AND SAVE IT
if (!empty($_GET["q"])) $_SESSION["q"] = $_GET["q"];

// SHOW WHAT WE ARE SEARCHING FOR
echo "<pre>";
if ( $_SESSION["q"]) echo "WE ARE SEARCHING FOR <b>{$_SESSION["q"]}</b> NOW.";
if (!$_SESSION["q"]) echo "WE ARE NOT SEARCHING NOW.";


// CREATE THE FORM USING HEREDOC NOTATION
$form = <<<ENDFORM
<form>
ENTER A SEARCH TERM HERE:
<input name="q" value="{$_SESSION["q"]}" />
<input type="submit" />
</form>
ENDFORM;

echo $form;

Open in new window

0
 
FeziAuthor Commented:
Thank you. Perfect Solution
0
 
Ray PaseurCommented:
Thanks for the points - it's a great question, ~Ray
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now