flunn Posted April 17, 2007 Share Posted April 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
saint959 Posted April 17, 2007 Share Posted April 17, 2007 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> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 17, 2007 Share Posted April 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
soycharliente Posted April 17, 2007 Share Posted April 17, 2007 @ken: Ironic? I didn't make this post and you helped me! Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 17, 2007 Share Posted April 17, 2007 ken's way works i'm sure. this is how i do it: <?php echo "<input type=\"text\" name=\"field1\" value=\"". ((isset($_POST['field1'])) ? ($_POST['field1']) : ("")) ."\">\n"; ?> Quote Link to comment Share on other sites More sharing options...
flunn Posted April 17, 2007 Author Share Posted April 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 17, 2007 Share Posted April 17, 2007 Please post the code that isn't working between tags. Ken Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 17, 2007 Share Posted April 17, 2007 Ok, I found the small error in my code. I left out an "echo". Here's the corrected line: <input type="text" name="name" <?php echo form_value('name') ?>> Ken Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 17, 2007 Share Posted April 17, 2007 post your code. i would like to know how you integrated my example in your script. Quote Link to comment Share on other sites More sharing options...
flunn Posted April 17, 2007 Author Share Posted April 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 17, 2007 Share Posted April 17, 2007 <!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. Quote Link to comment Share on other sites More sharing options...
flunn Posted April 17, 2007 Author Share Posted April 17, 2007 I tried it, boo_lolly, but I'm afraid it still doesn't work. The text still fails to reappear in its original position when the "submit" button is clicked. but thanks flunn Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 17, 2007 Share Posted April 17, 2007 The problem is that the solution is missing an "echo". The correct line is: <input type=text name=name value="<?php echo ((isset($_POST['name'])) ? ($_POST['name']) : ("")); ?>"> Ken Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 17, 2007 Share Posted April 17, 2007 good eye, ken. thanks for clearing that up. Quote Link to comment Share on other sites More sharing options...
flunn Posted April 17, 2007 Author Share Posted April 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.