Page 1 of 1
My contact form:Part 2
Posted: Wed Sep 10, 2003 5:49 am
by Chaal
Hey there,
I have got my form showing just fine now but the error printing is not working. If I click submit without entering any information in the fields the form refreshes but does not show the error messages, also if I do fill in all the details the form still refreshes.
Is there anyone who can take a quick butchers at the code?
Here is an URL to the text code:
http://www.tangerine.tk/form.txt
Next time you are in Ely I will buy ya a pint
Thanks[/url]
Posted: Wed Sep 10, 2003 8:38 am
by Stoker
I didnt look at it all in detail, and from a programming point of view it is messy and a bit unusual....
First of all it looks like you depend on register globals being on, you should use the $_REQUEST['varname'] variable instead, and use stripslashes() for the values if magic_quotes_gpc is on..
Here is a simpler sample, this one is also wildly mixing code and content
Code: Select all
<h1>My stuff</h1>
<?php
$posted = empty($_REQUEST['posted']) ? false : true; // Determine if a POST or GET was done
$err = array(); // No errors to begin with
if ($posted)
{
if (!$name = trim( strip_tags( stripslashes( $_REQUEST['name'] )))) $err[] = 'Name Required';
if (!$email = trim( strip_tags( stripslashes( $_REQUEST['email'] )))) $err[] = 'Email Required';
}
if ($posted && !$err)
{
// Success - send mail or whatever
?><p><b>Thank you</b></p><?php
}
else // Output the form
{
foreach ($err as $errmsg) echo 'Error: '.htmlspecialchars($errmsg)."<br>\n";
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST">
<h2>Bla Bla Form</h2>
<p>Name:
<input type="text" name="name" value="<?php echo htmlspecialchars($name); ?>"><br>
E-Mail:
<input type="text" name="email" value="<?php echo htmlspecialchars($email); ?>"><br>
<input type="hidden" name="posted" value="1">
<input type="submit" name="submit" value="Submit">
</p>
</form>
<?php
} // end form output
?>
Posted: Wed Sep 10, 2003 10:17 am
by Chaal
Hi Stoker,
Thats great

Thanks!
Could you tell me how I would add this code
and concatenate into this.
Code: Select all
<input type="text" name="name" value="<?php echo htmlspecialchars($name); ?>
Thanks again

Posted: Wed Sep 10, 2003 10:19 am
by twigletmac
Just replace the variable name in the echo -
Code: Select all
<input type="text" name="name" value="<?php echo htmlspecialchars($name); ?>
becomes
Code: Select all
<input type="text" name="name" value="<?php echo htmlspecialchars($_GET['title']); ?>
Mac
Posted: Wed Sep 10, 2003 10:34 am
by Chaal
Hey thanks for that
Its all a learning curve for me - I will get there (one day)
I am also getting confused with using option select drop downs, I am not 100% sure how I can validate so an error shows if they did not select yes or no and just submitted leaving the 'Please Select' untouched:
Code: Select all
<select name="fchildren" size="1" id="fchildren" class="text" tabindex="15">
<option value="Please Select">Please Select
<option value="Yes">Yes
<option value="No">No
</option>
</select>
I wish I had something to offer you guys but as you can see I am of no help
Thanks for any support.
Posted: Wed Sep 10, 2003 2:52 pm
by Stoker
Code: Select all
...
<option value="1">Yes</option>
<option value="0">No</option>
...
<?php
if (empty($_REQUEST['fchildren']))
{
// No was selected
$err[] = 'You cant select NO you son of a Gates';
}
else
{
// Yes was selected
}
?>
Posted: Wed Sep 10, 2003 2:56 pm
by Chaal
Thanks for that Stoker
If I dare ask one more question
How do I make the selected input options remain selected when the page refreshes to show items that were not enetered or selected?
At the moment when the page refreshes to show errors the 'please select' lists go back to 'please select' instead of showing what might have been selected.
Your cheques in the post by the way
Thanks