Jump to content

[SOLVED] how to keep text in text boxes after form is rewritten


flunn

Recommended Posts

I'm a beginner with php. I'm trying to learn it so I can make interactive 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 the original text box

 

I've managed to write some code that does the first two things, 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

 

Link to comment
Share on other sites

hi, from what i gather you can:

make the value of the input field name=$name i.e.

 

<body>

<p> Please write your name in the box</p>

<form action= "<?php echo $PHP_SELF;?>" method="post" />

<input type=text name=name value="<? echo "$name"; ?>">

<input type=submit>

</form>

Link to comment
Share on other sites

You have to use the value in the $_POST array in your form. I usually create a function to do this, since most forms have more than one field:

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

 

Ken

Link to comment
Share on other sites

Many thanks to saint959, kenrbnsn, and boo_iolly for their suggestions.

 

Unfortunately, however, I haven't had any success with any of them. When I make the recommended changes in my code, I get exactly the same results as i was getting before.

 

still hoping though

 

flunn

Link to comment
Share on other sites

Great!! With that extra "echo" Ken's code works perfectly.

Thank you Ken! Your help is much appreciated.

 

And of course I appreciate the other replies to my question too. I've been given a very favorable impression of this forum which I only began using this morning.

 

As to boo_lolly's question about how I integrated the code he sent. Yes, I should have said in my previous post that I didn't really know where to put it; so i just stuck it in at the end as follows.

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN">
<html>

<head>
	<title></title>
</head>
<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;
echo "<input type=\"text\" name=\"field1\" value=\"". ((isset($_POST['field1'])) ? ($_POST['field1']) : ("")) ."\">\n";
}
?>
</body>
</html>

 

Happy as I am about getting my immediate problem solved, I realize I've still got a problem on my hands. I'm a long way from being able to *understand* Ken's code and even farther perhaps from being able to understand boo_lolly's. And I really don't think it's a good idea in the long run to use code you don't understand.

 

But what is the best way to get that understanding? Maybe I'll make another post to this thread later on asking for advice on *that* matter.

 

regards to all

 

flunn

Link to comment
Share on other sites

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN">
<html>

<head>
	<title></title>
</head>
<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 ((isset($_POST['name'])) ? ($_POST['name']) : ("")); ?>">
<input type=submit>
</form>
<?php 
$name=$_POST['name'];
if (!empty($name)){
echo "thank you ";
echo $name;
}
?>
</body>
</html>

 

try that.

Link to comment
Share on other sites

Earlier I said I was going to make another posting in this thread about the problem of how to avoid using code I don't understand — or of how, for example, to come to understand the code Ken has given me (and which I am quite likely going to be using *a lot* in the future).

 

As I've said, I'm a beginner with php. Almost all my small knowledge of it comes from using online stuff, tutorials and articles. That's a good way to get an understanding of the really *basic* stuff, but I don't think it's a very good way to solve the specific problems that beginners are bound to run into as soon as they try to apply their basic knowledge to writing php code for their own websites.

 

I'm not too sure that books are likely to be a big help with these specific, practical problems either: A couple of weeks ago I bought Larry Ullman's "PHP for the World Wide Web." I haven't had time yet to study it carefully, but I've done quite a bit of browsing and I'm pretty sure that nowhere in its pages would I find a solution to the "keeping-text-in-text-boxes" problem—or to lots and lots of other "little" problems of a similar kind.

 

Forums seem to be much the best idea when you're faced with a specific practical problem — but it seems that by using them you *are* likely to end up using code you don't understand. The ideal thing, of course, would be to have a sort of "personal php trainer." I mean, I'm pretty sure I could learn a lot very quickly if I could get Ken to go over his code with me line by line, patiently answering my dumb questions and clearing up my confusions. But I can't afford to pay him or anyone else what they would deserve for doing that and I wouldn't feel right about trying to get that kind of help from anyone for free.

 

Any advice about how a beginner like me might best go about learning php efficiently and freely would be much appreciated. (I think such advice might be of quite general interest: There must be quite a few people out there in a similar position.)

 

regards to all from

 

flunn

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.