?
Solved

PHP Remain text in textbox after submit form

Posted on 2011-10-27
9
Medium Priority
?
3,470 Views
Last Modified: 2012-05-12
Hi,

I'm in the middle of developing an application, After submitting a search form, how do you retain the text in the textbox after submitting the form.

Also, I have a previous/next button at the end of the search list if there are over e.g 7 searches, and the querystring displays the search word, i'm pretty sure it's just a simple problem. I'm also using PHP.

Any help appreciated. Thanks.
0
Comment
Question by:Fezi
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 7

Expert Comment

by:amigura
ID: 37037507
0
 
LVL 13

Expert Comment

by:Andrew Derse
ID: 37037552
You can retain the text by putting the value back into the form field...

Here's the code:
<?php

if (isset($_POST['name'])) {
$name = $_POST['name'];	
}

if ($name !='') {
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<p>Thanks <?php print $name;?>!  We appreciate your business!</p>
<form action="<?php $_SESSION['PHP_SELF']?>" method="POST">
<input type="text" name="name" id="name" value="<?php print $name;?>" />
<input type="Submit" name="submit" id="submit" value="Submit" />
</form>

<?php } else { ?>

<form action="<?php $_SESSION['PHP_SELF']?>" method="POST">
<input type="text" name="name" id="name" value="" />
<input type="Submit" name="submit" id="submit" value="Submit" />
</form>
<?php }	?>
</body>
</html>

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37038060
You can save variables in the session or in the data base.  You can retrieve these variables and use them to prepopulate the fields of a form.  You cannot prepopulate a form element with the attribute type=file, but all text elements, selectors, checkboxes, submit values, and radio buttons can be prepopulated.

If the action script name (action attribute) in the <form> tag is empty, the script will make a GET or POST request to its own URL.  That means that a statement like this might or might not work correctly:

<form action="<?php $_SESSION['PHP_SELF']?>" method="POST">

A safe statement that probably means the same thing would be like this:

<form method="post">
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Expert Comment

by:MattJellings
ID: 37038070
Hi,

Nukit is on the right track however I personally wouldn't use an If-Else and duplicate the form that way.

Simple put your form on screen as you would normally and then use the following as an example (following on a bit from Nukit's example above)::

<input type="text" name="name" id="name" value="<?php if ($name) { print $name; } ?>" />

Open in new window


Also, when printing $name as above (so that it's part of the value="" parameter) it's probably a good idea to run it through the htmlspecialchars() functions in case anyone has included quotes etc in their submission.

Hope that makes sense.

Cheers

Matt
0
 
LVL 13

Expert Comment

by:Andrew Derse
ID: 37038084
The only reason I included the if else was to display the additional greeting "Hello soandso..."  It was left over from a different project I create a while ago...
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 2000 total points
ID: 37038155
Run this script on my server to see the moving parts.  Note that it keeps the GET request arguments in url intact.  This would be lost if you used PHP_SELF and would be retained if you used REQUEST_URI.  But you do not need either of them if you (correctly) package your form and action script into a single script file.
http://www.laprbass.com/RAY_temp_fezi.php?foo=bar
<?php // RAY_temp_fezi.php
error_reporting(E_ALL);
session_start();

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

// IF THERE HAS BEEN POSTED DATA COPY IT AND SAVE IT
if (!empty($_POST["thing"])) $_SESSION["thing"] = $_POST["thing"];

// SHOW THE SUPERGLOBALS
echo "<pre>";
echo "GET ";     var_dump($_GET);
echo "POST ";    var_dump($_POST);
echo "SESSION "; var_dump($_SESSION);


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

echo $form;

Open in new window

0
 

Accepted Solution

by:
Fezi earned 0 total points
ID: 37038210
I've done it

function searchform() {
            $searchform1 = $_POST['searchterms'];
            $getsearch = $_GET['q'];

            if ($searchform1 != "") {
                echo $searchform1;
            }

            if ($getsearch != "") {
                echo $getsearch;
            }
        }


and then

Search: <input type="text" name="searchterms" value="<?php searchform();?>" />
0
 

Author Closing Comment

by:Fezi
ID: 37061222
thanks
0
 
LVL 4

Expert Comment

by:MattJellings
ID: 37039332
Hi Fezi,

Glad you found your solution.  Just one suggestion I you wish to do things this way is to use return rather than echo in your function.  This will give you a bit more flexibility as you can assign the returned value to a variable for use elsewhere should you need to. I would also check to see if the _POST and/or GET variables have been set first. I've also changed the structure of the if-else statement slightly so a blank string is returned if both searchterms and getsearch are empty.  For example:

function searchform() {
  $searchform1 = isset($_POST['searchterms']) ? $_POST['searchterms'] : '';
  $getsearch = isset($_GET['q']) ? $_GET['q'] : '';

  if ($searchform1 != '') {
    return $searchform1;
  }
  elseif ($getsearch != '') {
    return $getsearch;
  }
  else {
    return '';
  }
}

Open in new window


Then..

Search: <input type="text" name="searchterms" value="<?php echo searchform(); ?>" />

Open in new window


Should you wish to use the output from searchform() later on you can do the following:

$search = searchform();

// Something here to filter the results based on $search

Open in new window


I hope that makes some sense and helps you out.  Sounds like you may already have it working how you required but above is just a suggestion to your existing code.

Thanks,

Matt

NOTE: Sorry I haven't tested the above code, just typed it straight in to EE to try and give you a couple of pointers so I apologise if anything is incorrect above.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question