By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,919 Members | 1,625 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,919 IT Pros & Developers. It's quick & easy.

how can I keep text in text boxes when form is rewritten?

P: 3
I'm a beginner with php. I'm trying to learn it so I can make quizzes and tutorials for my English as a Second Language site.

I'm trying to make a form that does the following:

(1) the user answers a question by typing in a text box
(2) a response is then written below the form, saying whether the answer is right or wrong and, perhaps making a comment of some kind
(3) the user's answer remains visible in same text box as it was originally typed in

I've managed to do the first two, but despite many attempts and much searching for code examples, I've failed to find a way of getting the answer to reappear in the text box when the page is rewritten.

This is my code:

<body>
<p> Please write your name in the box</p>
<form action= "<?php echo $PHP_SELF;?>" method="post" />
<input type=text name=name value="">
<input type=submit>
</form>
<?php
$name=$_POST['name'];
if (!empty($name)){
echo "thank you ";
echo $name;
}
?>
</body>

As I said, I want the user's name to reappear in the form as well as appearing in the sentence below the form.

Any advice will be much appreciated.

regards to all from
flunn
Apr 17 '07 #1
Share this Question
Share on Google+
3 Replies


ak1dnar
Expert 100+
P: 1,584
[PHP]<body>
<p> Please write your name in the box</p>
<form action= "<?php echo $PHP_SELF;?>" method="post" />
<input type=text name=name value="<?php echo $name; ?>">
<input type=submit>
</form>
<?php
$name=$_POST['name'];
if (!empty($name)){
echo "thank you ";
echo $name;
}
?>
</body>[/PHP]
Apr 17 '07 #2

P: 3
Many thanks to ajaxrand for replying to my question.The idea of solving the problem by using

value= "<?php echo $name; ?>"

seemed very good because it was straightforward and comprehensible.

However, for me at least, it doesn't work. After making the change in my code, I find the user's name still disappears after I click on the "submit" button.

I should have said, perhaps, that I'm using Mac OS 10.4 and developing locally with MAMP.

further suggestions from ajaxrand or anyone else will be most welcome

regards to all from

flunn
Apr 17 '07 #3

P: 3
The problem has been solved by "kenrbnsn" who replied to my question when I posted it in another forum. He says the best way to do what I want to do is by writing a function. Here is the final code:[HTML]<body>

<?php
function form_value($fld,$txtarea=false) {
if (!isset($_POST[$fld])) return ('');
$retval = htmlentities(trim(stripslashes($_POST[$fld])),ENT_QUOTES);
if (!$txtarea) return(' value="' . $retval . '"');
else return($retval);
}

?>
<p> Please write your name in the box</p>
<form action= "<?php echo $PHP_SELF;?>" method="post" />
<input type="text" name="name" <?php echo form_value('name') ?>>
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['name']))
echo "thank you " . stripslashes($_POST['name']);
?>
</body>[/HTML]

I'm afraid I don't understand this code, and I don't like using code I don't understand, but, still, I'm very happy to have the immediate problem solved.

regards to all from
flunn
Apr 17 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.