View previous topic :: View next topic |
Author |
Message |
shawn.berg -
Joined: 27 May 2004 Posts: 16 Location: Minnesota
|
Posted: Wed Jun 09, 2004 5:35 am Post subject: Retain form info... |
|
|
OK I've searched and searched...I have a form that when a user enters data into form fields, it writes it to a MySQL table.
Now, as it stands, when the "Submit" button is pressed and a visitor overlooks a form field (Address, City, State etc..) an err message appears at the top of the form stating (Address Required, City Required, State Required, etc..) but resets ALL the fields to blank; forcing the visitor to retype EVERY field.
Is there a code snippet that would keep the already entered data in the form fields when the visitor gets the err messages so they don't need to retype the WHOLE form?
I thank you ahead of time.
Sincerely,
Shawn.Berg |
|
Back to top |
|
 |
iNaNimAtE -
Joined: 05 Nov 2003 Posts: 2381 Location: Everywhere you're not.
|
Posted: Wed Jun 09, 2004 5:41 am Post subject: |
|
|
Yes. If you have the form and processor using the same page, then use this:
Code: | <input type=\"text\" name=\"address\" value=\"$_POST['address']\"> |
Remember, it must be a php file. _________________ Bienvenidos! |
|
Back to top |
 |
 |
shawn.berg -
Joined: 27 May 2004 Posts: 16 Location: Minnesota
|
Posted: Wed Jun 09, 2004 6:06 am Post subject: OK..maybe I'm a little tired |
|
|
Sorry for the basic question, but here is my PHP code (yes it is a .php file). Unfortunately, I did not write the form dynamically in PHP but the form does process itself:
Code: |
<HTML>
<HEAD>
<TITLE>Add New Client</TITLE>
<!-- CSS -->
<LINK href="/css/shell4.css" type="text/css" rel="stylesheet">
</HEAD>
<BODY>
<IMG src="/images/logo.gif" class="logo" width="300" height="80">
<SPAN class="user"><FORM name="form" action="<?php echo($PHP_SELF); ?>" method="post">
<TABLE border="0">
<TR>
<TD>Company:</TD>
<TD><input type="text" name="company" size="35" maxlength="35"></TD>
<TD>Position:</TD>
<TD><input type="text" name="position" size="35" maxlength="35"></TD>
</TR>
<TR>
<TD>First Name:</TD>
<TD><input type="text" name="firstname" size="35" maxlength="35"></TD>
<TD>Last Name:</TD>
<TD><input type="text" name="lastname" size="35" maxlength="35"></TD>
</TR>
<TR>
<TD>Address:</TD>
<TD><input type="text" name="addressa" size="35" maxlength="35"></TD>
<TD>Address 2:</TD>
<TD><input type="text" name="addressb" size="35" maxlength="35"></TD>
</TR>
<TR>
<TD>City:</TD>
<TD><input type="text" name="city" size="35" maxlength="35"></TD>
<TD>State:</TD>
<TD><input type="text" name="state" size="2" maxlength="2"> Zip:<input type="text" name="zip" size="5" maxlength="5"></TD>
</TR>
<TR>
<TD>Daytime #:</TD>
<TD><input type="text" name="daytime" size="35" maxlength="12"></TD>
<TD>Evening #:</TD>
<TD><input type="text" name="evening" size="35" maxlength="12"></TD>
</TR>
<TR>
<TD>User Name:</TD>
<TD><input type="text" name="username" size="35" maxlength="35"></TD>
<TD>Password:</TD>
<TD><input type="text" name="password" size="35" maxlength="16"></TD>
</TR>
<TR>
<TD></TD>
<TD><input type="submit" name="sent" value="Sign 'em up cowboy!"></TD>
</TR>
</FORM></SPAN></TABLE>
<?php
error_reporting(E_PARSE | E_ERROR);
if ($sent)
{ $valid=true;
if (!$company)
{ $errmsg.="Company Name Required <br>"; }
if (!$position)
{ $errmsg.="Position Title Required <br>"; $valid=false; }
if (!$firstname)
{ $errmsg.="First Name Required <br>"; $valid=false; }
if (!$lastname)
{ $errmsg.="Last Name Required <br>"; $valid=false; }
if (!$addressa)
{ $errmsg.="Address Required <br>"; $valid=false; }
if (!$city)
{ $errmsg.="City Required <br>"; $valid=false; }
if (!$state)
{ $errmsg.="State Required <br>"; $valid=false; }
if (!$zip)
{ $errmsg.="Zipcode Required <br>"; $valid=false; }
if (!$daytime)
{ $errmsg.="Daytime # Required <br>"; $valid=false; }
if (!$evening)
{ $errmsg.="Evening # Required <br>"; $valid=false; }
if (!$username)
{ $errmsg.="User Name Required <br>"; $valid=false; }
if (!$password)
{ $errmsg.="Password Required <br>"; $valid=false; }
}
if ($valid!=true)
{ echo($errmsg.$form); }
else {
#connect to MySQL
$conn = @mysql_connect("localhost","username","password")
or die("Could not connect to MySQL");
#select a database
$db = @mysql_select_db("clients",$conn)
or die("Could not select database");
#create the SQL query
$sql = "insert into user (company,position,first_name,last_name,addressa,addressb,city,state,
zip,daytime,evening,user_name,password)
values (\"$company\",\"$position\",\"$firstname\",\"$lastname\",\"$addressa\",\"$addressb\",
\"$city\",\"$state\",\"$zip\",\"$daytime\",\"$evening\",\"$username\",password(\"$password\") )";
#execute the query
$result = @mysql_query($sql,$conn)
or die("Could not execute query");
if($result)
{ echo("New user $username added"); }
}
?>
</BODY></HTML>
|
I think I'm a little tired, but how would I include that code snippet in my form? Thank you so much for your help!!!!! GROVEL GROVEL GROVEL!!!!
Sincerely,
shawn.berg |
|
Back to top |
|
 |
roganty -
Joined: 08 Jun 2004 Posts: 357 Location: Bristol, UK
|
Posted: Wed Jun 09, 2004 10:51 am Post subject: |
|
|
shawn.berg wrote: | I think I'm a little tired, but how would I include that code snippet in my form? |
heres how you might do it:
Code: | <input type="text" name="firstname" size="35" maxlength="35" value="<?php if( isset($_POST['firstname']) echo($_POST['firstname']) ?>"> |
basically, if 'firstname' is set, then echo it, otherwise, it doesnt echo anything and so creates a blank string in the value attribute.
personally i would use print instead of echo. but thats just me! |
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|