Can't retain attributes in the form.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
leex18
Forum Newbie
Posts: 1
Joined: Fri Apr 16, 2004 1:04 pm

Can't retain attributes in the form.

Post by leex18 »

I have a problem with my php coding and some how i can't retain my attributes when i submit the wrong infomation into form and the only thing it retains is the name form whereas the rest is blank. Please any help would be much appreciated.

The following codes are:

<html>
<head>
<title>Sign My GuestBook</title>
</head>

<body background="" bgcolor="9999FF" text="000000">
<h1>Feel free to sign our Guest Book</h1>

<?php

$db = mysql_connect("localhost", "", "");
mysql_select_db("guestbook", $db);




if ($submit) {

$errmsg1="";
$errmsg2="";
$errmsg3="";

$name1 .= "$name";
$location1 .= "$location";
$email1 .= "$email";
$url1 .= "$url";
$comments1 .= "$comments";

if (empty($name))
{
$errmsg1 .="<li>you have to put in a name, at least!\n";
printf($errmsg1);
}


if (empty($email) || !eregi("^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,3}$",$email))
{
$errmsg2 .="<li>$email doesn't look like a valid email address\n";
printf($errmsg2);
}


if (!empty($url) && !eregi("^http://[A-Za-z0-9\%\?\_\:\~\/\.-]+$",$url))
{
$errmsg3 .="<li>$url doesn't look like a valid URL\n";
printf($errmsg3);
}

if ( !empty($errmsg1) || !empty($errmsg2) || !empty($errmsg3) )
{
?>
<form method="post" action="<?php echo $PHP_SELF?>">

<table border=0>

<tr>
<td align="right">Name:</td>
<td><input type= "text" name= "name" value="<?php echo "$name1" ?>" size= "40"><br /></td>
</tr>

<tr>
<td align="right">Location:</td>
<td><input type= "text" name= "location" value="<?php "$location1" ?>" size= "40"><br /></td>
</tr>

<tr>
<td align="right">E-mail:</td>
<td><input type= "text" name= "email" value="<?php "$email1" ?>" size= "40"><br /></td>
</tr>

<tr>
<td align="right">URL:</td>
<td><input type= "text" name= "url" value="<?php "$url1" ?>" size= "40"><br /></td>
</tr>

<tr>
<td align="right" valign="top">Comments:</td>
<td><textarea wrap=virtual rows=5 cols=45 name="comments" value= "<?php "$comments1" ?>" ></textarea><br /></td>
</tr>

<tr>
<td>
<input type="Submit" name="submit" value="Submit"></td>
<td><input type=reset value="Reset form">
</td>
</tr>

</table>
</form>

<?php
exit();
}





if (empty($errmsg1) && empty($errmsg2) && empty($errmsg3))
{
$sql = "INSERT INTO guestbook (name,location,email,url,comments)
VALUES ('$name','$location','$email','$url','$comments')";
$result = mysql_query($sql);
echo "Thank you! Information entered.\n";
}

} else{
//?>

printf("" <form method="post" action="<?php echo $PHP_SELF?>">

<table border=0>

<tr>
<td align="right">Name:</td>
<td><input type= "text" name= "name" size= "40"><br /></td>
</tr>

<tr>
<td align="right">Location:</td>
<td><input type= "text" name= "location" size= "40"><br /></td>
</tr>

<tr>
<td align="right">E-mail:</td>
<td><input type= "text" name= "email" size= "40"><br /></td>
</tr>

<tr>
<td align="right">URL:</td>
<td><input type= "text" name= "url" value= "http://" size= "40"><br /></td>
</tr>

<tr>
<td align="right" valign="top">Comments:</td>
<td><textarea wrap=virtual rows=5 cols=45 name="comments" ></textarea><br /></td>
</tr>

<tr>
<td>
<input type="Submit" name="submit" value="Submit"></td>
<td><input type=reset value="Reset form">
</td>
</tr>

</table>
</form>


//<?php
}
?>


</body>
</html>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

value="<?php "$location1" ?>"

you need to echo the value:
value="<?php echo $location1; ?>"

(same for all those other similar lines too ;))
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

or value="<?=$location1?>"
(just learned that!
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

value="<?=$location1?>"

with no ;?

it's not value="<?=$location1;?>"

interesting. this will come in handy for many of my scripts.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

i havent had any problems with or without ;
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Just a reminder that <?=$var?> only works if short_open_tag is On ;)
Post Reply