View Full Version : Form with PHP validation help
ullsa01
03-17-2004, 01:31 AM
Hello everyone,
I am new to PHP, but I am trying to get it to work for me. Please give me a hand!
First I created a big form with many fields, and tried to have PHP validate just some of them, that failed. I think it wants to validate all. So I created a small form to validate all fields and start from there, but when I get to the submit part, it tells me that I failed to enter information on all the fields. Of course I did fill out the form. What did I miss? Please help me out with this. Here is the end of the code:
// Contact Message
if ($_POST['comments'])
{
$comments = "comments - " . $_POST['comments'];
$message .= "$comments\n";
}
else
{
$error .= "Please enter your comments<br>\n";
}
// If there is no error this part of the code sends the email
if ($error == "")
{
echo "Thank you for using my online form.
<br>
<br>Te mandaremos una copia de tus comentarios.<br><br>
<br><br>Thank you, Gracias.";
// This Part of the code sends the email to the guest
mail ("info@mi-pueblo.com", "Website email from www.mi-pueblo.com", $message,
"From: $Email\r\n"
."Reply-To: $Email\r\n"
."X-Mailer: PHP/" . phpversion());
// This part of the code sends the email to the owner of Site
mail ("myemail@domain.com", "Website email from www.mi-pueblo.com", $message,
"From: $Email\r\n"
."Reply-To: $Email\r\n"
."X-Mailer: PHP/" . phpversion());
// If there is an error this part of the code displays what the error is
}
else
{
print "I'm sorry, but the form cannot be sent until you correct the following errors:<br>\n";
print "$error<br>\n";
print "<br>\n";
print "<br>\n";
print "Please use your \"Back\" button to return to the form to correct the omissions. Thank you.<br>\n";
}
?>
Nightfire
03-17-2004, 01:49 AM
Can you edit your post please and surround your code with the tags? Is it possible to have a look at your form too?
ullsa01
03-17-2004, 02:04 AM
Thank you for that quick response.
I added the at the beginning of the PHP and at end of PHP code
Here is the form.
<body>
<form action="form.php" name="Contact" method="post">
<table border="1" style="border-collapse: collapse" width="50%" id="table1" bordercolor="#7C93B6" align="Center">
<tr>
<td colspan="2" bgcolor="#7C93B6">
<p align="center"><b><u><font face="Verdana" size="2" color="#FFFFFF">
Tus comentarios o Sugerencias</font></u></b></td>
</tr>
<tr>
<td width="13%"><font size="2" face="Verdana">Nombre:</font></td>
<td width="85%"> <input type="text" name="name" size="33" style="border: 1px solid #C0C0C0"></td>
</tr>
<tr>
<td width="13%"><font size="2" face="Verdana">E-Mail</font></td>
<td width="85%"> <input type="text" name="uemail" size="33" style="border: 1px solid #C0C0C0"></td>
</tr>
<tr>
<td width="13%"><font size="2" face="Verdana">Tema:</font></td>
<td width="85%"> <select size="1" name="subject" style="border: 1px solid #C0C0C0">
<option selected>Seleccione uno</option>
<option value="Comments?">Sugerencias?</option>
<option value="Misc.?">Problemas con Website.?</option>
<option value="Website Problems?">Ayuda Technica?</option>
<option value="Questions?">Preguntas?</option>
</select></td>
</tr>
<tr>
<td width="13%" valign="top"><font size="2" face="Verdana">Mensage:</font></td>
<td width="85%"> <textarea rows="12" name="message" cols="46" style="border: 1px solid
#C0C0C0"></textarea></td>
</tr>
<tr>
<td colspan="2">
<p align="center">
<input type="submit" value="Enviar Mensaje" name="send" style="border: 1px solid #000000">
<input type="reset" value="Empezar Nuevo" name="clear" style="border: 1px solid #000000"></td>
</tr>
<tr>
<td colspan="2" bgcolor="#6666FF"> </td>
</tr>
</table>
</form>
</body>
</html>
Nightfire
03-17-2004, 02:12 AM
In your php code, you're seeing if the form field comments is filled, but in your form you don't have a comments field, so I'm assuming it's the message textarea you're checking? If it is, rename the textarea name from message to comments
ullsa01
03-17-2004, 02:29 AM
Good call, I made the change, but still the error is
:
I'm sorry, but the form cannot be sent until you correct the following errors:
You did not enter your name
You did not enter your email address
Please pick a subject from list
Please enter your comments
Please use your "Back" button to return to the form to correct the omissions. Thank you.
ullsa01
03-17-2004, 02:31 AM
I appreciate your help. In your previous reply you said "Can you edit your post please and surround your code with the tags?" I am not sure I understood that, can you give me an example? I put the code after the post in brackets.
Thank you
Nightfire
03-17-2004, 02:31 AM
I've got no idea why that's showing. From the code you've shown, it doesn't even show that text anywhere, or anything to do with those form fields.
Nightfire
03-17-2004, 02:34 AM
Yeah, what I meant was, when you post code, it makes it much easier to read and see errors when there's colour highlighting. The tags make this magic happen. EG:
<?php
if($_POST['comment']){
// code etc
makes it look like
<?php
if($_POST['comment']){
// code etc
ullsa01
03-17-2004, 02:40 AM
The fields do exist, here is the entire PHP code.
<?php
$message = "Thank you for sending your feedback.\n";
// Contact Name
if ($_POST['username'])
{
$user = "Name - " . $_POST['username'];
$message .= "$user\n";
}
else
{
$error .= "You did not enter your name<br>\n";
}
// Email Address
if ($_POST['email'])
{
$email = "email - " . $_POST['email'];
$message .= "$email\n";
}
else
{
$error .= "You did not enter your email address<br>\n";
}
// Message Subject
if ($_POST['subject'])
{
$subject = "subject - " . $_POST['subject'];
$message .= "$subject\n";
}
else
{
$error .= "Please pick a subject from list<br>\n";
}
// Contact Message
if ($_POST['comments'])
{
$comments = "comments - " . $_POST['comments'];
$message .= "$comments\n";
}
else
{
$error .= "Please enter your comments<br>\n";
}
// If there is no error this part of the code sends the email
if ($error == "")
{
echo "Thank you for using my online form.
<br>
<br>Te mandaremos una copia de tus comentarios.<br><br>
<br><br>Thank you, Gracias.";
// This Part of the code sends the email to the user
mail ("info@mi-pueblo.com", "Website email from www.mi-pueblo.com", $message,
"From: $Email\r\n"
."Reply-To: $Email\r\n"
."X-Mailer: PHP/" . phpversion());
// This part of the code sends the email to the owner
mail ("info@mi-pueblo.com", "Website email from www.mi-pueblo.com", $message,
"From: $Email\r\n"
."Reply-To: $Email\r\n"
."X-Mailer: PHP/" . phpversion());
// If there is an error this part of the code displays what the error is
}
else
{
print "I'm sorry, but the form cannot be sent until you correct the following errors:<br>\n";
print "$error<br>\n";
print "<br>\n";
print "<br>\n";
print "Please use your \"Back\" button to return to the form to correct the omissions. Thank you.<br>\n";
}
?>
Nightfire
03-17-2004, 02:47 AM
Seem to be having a naming problem with your php and form. You're checking for fields called username and email, when those two are called name and uemail in the html form. I'm not sure why you're getting errors for the other fields though. If you put
print_r($_POST);
what does that print out at the top of your page?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.