473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
3 1749
ak1dnar
1,584 Expert 1GB
[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
flunn
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
flunn
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

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

Similar topics

1
by: Daniel Hill | last post by:
OK, I have very, VERY basic background knowledge of VB6 and have now upgraded to VB.NET and now I'm struggling to bring up the forms I want. What I am looking to do is to have a click a command...
5
by: Hung Huynh | last post by:
Hello, I have 2 separate web sites on 2 different boxes www.xyz.com on box 1 www2.xyz.com on box 2 Users log into box 1 via regular ASP/Database authentication, and I keep a session...
19
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the...
7
by: Gertjan van Heijst | last post by:
Hi, I really hope someone can help me because I've already spend 2 days on this problem and I'm not getting anywhere. I think the problem is that I don't really understand how text boxes store...
7
by: Andrew McKendrick | last post by:
Hi, I've noticed a bug in VB.NET (latest .NET Framework)... - I have a TabControl on a form with several tabs. - Each tab contains text boxes that are bound to fields in a data source...
6
by: BurntWithTime | last post by:
Hello everyone, Please can anyone tell me if it is possible for a VB.NET application to check a database and then create textboxes on a form that correspond to those records on the database? For...
7
by: ljungers | last post by:
Have Form-1 with 3 text boxes and 1 command button. With any of the 3 boxes filled out and button is clicked, a Macro is performed that Opens a Query that has a WHERE clause that uses the 3 test...
4
by: Andrew Meador - ASCPA, MCSE, MCP+I, Network+, A+ | last post by:
I have created a report. This report needs to display records between two dates entered by the user. I put two text boxes on the report so I can enter the start and end date - I set them to use an...
11
by: jwessner | last post by:
I have a form (Form1) which contains basic Project data and a subform listing the personnel assigned to the Project as a continuous form. Selecting a person on that project and clicking on a command...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.