Page 1 of 1

Can't retain attributes in the form.

Posted: Fri Apr 16, 2004 1:04 pm
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>

Posted: Fri Apr 16, 2004 1:11 pm
by markl999
value="<?php "$location1" ?>"

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

(same for all those other similar lines too ;))

Posted: Fri Apr 16, 2004 1:15 pm
by magicrobotmonkey
or value="<?=$location1?>"
(just learned that!

Posted: Fri Apr 16, 2004 1:40 pm
by johnperkins21
value="<?=$location1?>"

with no ;?

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

interesting. this will come in handy for many of my scripts.

Posted: Fri Apr 16, 2004 1:57 pm
by magicrobotmonkey
i havent had any problems with or without ;

Posted: Fri Apr 16, 2004 2:23 pm
by markl999
Just a reminder that <?=$var?> only works if short_open_tag is On ;)