Mike Lewis
Programmer
I have just got back to PHP after an absence of several years, and am having a little difficulty.
This is what I want to do:
1. Display a form, which includes a text box. Have the user fill in the text box with a value, and hit Submit.
2. The form calls a routine that processes the entered value in some way.
3. The procedure displays its results, on the same page as the form.
This is what I have got so far (highly simplified, with all extraneous code eliminated). (The page is named postcode.php.)
That works perfectly ...
... except for one small detail. When the user clicks the Submit button, the page is redisplayed. The input box is now empty. That's what you would expect. But I'd really like it to continue to show the value that the user entered, so that the user can edit it if he made a mistake when typing it.
So I thought I would simply store the value in a session variable, and use that value as the value attribute of the input box the next time round. I amended the code as follows:
But that doesn't quite work.
This is what I am seeing:
1. The user enters A and hits Submit.
2. The page displays the results for A, but the input box is now empty.
3. The use enters B and hits Submit.
4. The page displays the results for B, but the input box now contains A.
In other words, the input box always shows the value from the last query but one, not the immediately previous query. I can't understand why that is happening or what I can do to fix it. Any help would be appreciated.
For what it's worth, I'm using PHP 5.2.6, under a local Apache server on a Windows PC.
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads
This is what I want to do:
1. Display a form, which includes a text box. Have the user fill in the text box with a value, and hit Submit.
2. The form calls a routine that processes the entered value in some way.
3. The procedure displays its results, on the same page as the form.
This is what I have got so far (highly simplified, with all extraneous code eliminated). (The page is named postcode.php.)
PHP:
<?php session_start(); ?>
..
..
<?php
// display the form
$lookup_form =
'<form action = "postcode.php" method="post">
<input type = "text" name = "postcode" />
<input type = "submit" name = "submit_button" value = "Submit" />
</form>' ;
echo $lookup_form ;
if(isset($_POST['submit_button']))
{
if (isset($_POST['postcode']))
{
// beautify the value that the user entered
$postcode = trim(strtoupper($_POST['postcode']));
// code goes here to process the value; stores the result in $result
// ...
// ...
// display the result
echo $result;
}
else
{
echo '<p>Please enter your postcode.</p>';
}
}
?>
That works perfectly ...
... except for one small detail. When the user clicks the Submit button, the page is redisplayed. The input box is now empty. That's what you would expect. But I'd really like it to continue to show the value that the user entered, so that the user can edit it if he made a mistake when typing it.
So I thought I would simply store the value in a session variable, and use that value as the value attribute of the input box the next time round. I amended the code as follows:
PHP:
<?php session_start(); ?>
..
..
<?php
// display the form
$lookup_form =
'<form action = "postcode.php" method="post">
<input type = "text" name = "postcode" value = "' [b]. $_SESSION['prev_postcode'] .[/b] '" />
<input type = "submit" name = "submit_button" value = "Submit" />
</form>' ;
echo $lookup_form ;
if(isset($_POST['submit_button']))
{
if (isset($_POST['postcode']))
{
// beautify the value that the user entered
$postcode = trim(strtoupper($_POST['postcode']));
[b] // store it in a session variable for next time
$_SESSION['prev_postcode'] = $postcode ;[/b]
// code goes here to process the value; stores the result in $result
// ...
// ...
// display the results
echo $result;
}
else
{
echo '<p>Please enter your postcode.</p>';
}
}
?>
But that doesn't quite work.
This is what I am seeing:
1. The user enters A and hits Submit.
2. The page displays the results for A, but the input box is now empty.
3. The use enters B and hits Submit.
4. The page displays the results for B, but the input box now contains A.
In other words, the input box always shows the value from the last query but one, not the immediately previous query. I can't understand why that is happening or what I can do to fix it. Any help would be appreciated.
For what it's worth, I'm using PHP 5.2.6, under a local Apache server on a Windows PC.
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads