...

View Full Version : Forming Help



wincode
03-28-2010, 03:37 PM
Hello. I'm trying to make a really simple contact form.
I have name and a content box and with a submit button. When the user presses submit, the form action goes to /page.php?done=1 and then the form gets sent. But I am getting a lot of empty fields. How can I make it so if the user doesn't fill one of the fields, it give them an error and they have to go back to fill it.
Here is my code:


<?php if($_GET['done'] == 1)
{
echo "<center>Thank you for your message. We will have a new site shortly.</center>";}

//BLAH BLAH SEND CODES

else
{
?>
<center>

<form id="contact" name="contact" method="post" action="?done=1">
<br/>Name: <input type="text" name="name"/>
<br/><br/>What would you like on the new site?
<br/><textarea name="content" cols="75" rows="4"></textarea>
<br/><input type="SUBMIT" value="Submit"/>
</center>
<? } ?>

Mezzra
03-28-2010, 03:53 PM
You could use a


if (empty($variable)) {
die ("Please go back and enter a value in variable name")
}

masterofollies
03-28-2010, 04:31 PM
<?php if($_GET['done'] == 1)
{
extract($_POST);
if (EMPTY($name) || EMPTY($content)) {
echo 'You left a field empty. Please fill it in.';
}
else {
echo "<center>Thank you for your message. We will have a new site shortly.</center>";}

//BLAH BLAH SEND CODES
}
else
{
?>
<center>

<form id="contact" name="contact" method="post" action="?done=1">
<br/>Name: <input type="text" name="name"/>
<br/><br/>What would you like on the new site?
<br/><textarea name="content" cols="75" rows="4"></textarea>
<br/><input type="SUBMIT" value="Submit"/>
</center>
<? } ?>

_Aerospace_Eng_
03-28-2010, 05:17 PM
Why the need for extract for two fields? Extract can dangerous some times. Also with the way it is setup now someone can just go to ?done=1 and attempt to process the form. I would check to see if the form was submitted through a post like so

<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$content = $_POST['content'];
if (trim($name) == '' || trim($content) == '')
{
echo 'You left a field empty. Please fill it in.';
}
else
{
echo "<p style=\"text-align:center\" Thank you for your message. We will have a new site shortly.</p>";
}
//BLAH BLAH SEND CODES
}
else
{
?>

<form id="contact" style="text-align:center" name="contact" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<br/>
<label for="name">Name:</label>
<input type="text" id="name" name="name"/>
<br/>
<br/>
<label for="content">What would you like on the new site?</label> <br/>
<textarea name="content" id="content" cols="75" rows="4"></textarea>
<br/>
<input type="submit" name="submit" value="Submit"/>
<?php
}
?>

I used htmlentities on the $_SERVER['PHP_SELF'] in order to help prevent xss injection. Also the <center> tag is deprecated. CSS should be used in its place.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum