PDA

View Full Version : Validate a dropdown menu


gigiloumill
05-26-2011, 02:07 AM
Guys, I have this script to send a form to an e-mail. It's working ok, but I would like to know if someone can show me how to get script to validate a dropdown menu. "$reason" is for the dropdown menu. Unfortunately I can't get it to validate the menu. Here's the code. Thanks

<?php

// Where to redirect after form is processed.
$url = '';

$to = "";

$name = $_REQUEST["name"];
$e_mail = $_REQUEST["e_mail"];
$reason = $_REQUEST["reason"];
$comments = $_REQUEST["comments"];


$mail = new PHPMailer();$mail = new PHPMailer();
$mail->CharSet ="utf-8"; // You can adjust the Charset according to your language
$mail->IsSMTP();
$mail->Host = "";
$mail->From=""; //REMEMBER, this MUST be same as your authorization email address above.
$mail->FromName="";
$mail->SMTPAuth = true;
$mail->Username = "";
$mail->Password = "";

$mail->AddAddress($to);
$mail->Subject = "";
$mail->Body = "Name : " .$name . "\n" . "E_mail : " . $e_mail . "\n" . "Reason : " . $reason . "\n" . "Comments : " . $comments. "\n";



function is_valid_name($name) {
return ( $name);
}

if (!is_valid_name($name)) {
echo 'Sorry, you must enter your name. Please go back and enter your name';
exit;
}

function is_valid_email($e_mail) {
return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si', $e_mail);
}

if (!is_valid_email($e_mail)) {
echo 'Sorry, you must enter an e-mail. Please go back and enter an e-mail';
exit;
}

function is_valid_reason($reason) {
return ( $reason);
}

if (!is_valid_reason($reason)) {
echo 'Sorry, you must choose a reason for contacting us. Please go back and enter your reason';
exit;
}

function is_valid_comments($comments) {
return ( $comments);
}

if (!is_valid_comments($comments)) {
echo 'Sorry, you must tell us your comments. Please go back and enter your comments';
exit;
}
include_once $_SERVER['DOCUMENT_ROOT'] . 'securimage/securimage.php';
$securimage = new Securimage();

if ($securimage->check($_POST['captcha_code']) == false) {
// the code was incorrect
// you should handle the error so that the form processor doesn't continue

// or you can use the following code if there is no validation or you do not know how
echo "The security code entered was incorrect.<br /><br />";
echo "Please go <a href='javascript:history.go(-1)'>back</a> and try again.";
exit;
}
if(!$mail->Send())
{
echo "Error sending: " . $mail->ErrorInfo;
}
else
{
echo "Thank you for submitting.";
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
}


?>

gigiloumill
05-26-2011, 01:12 PM
I'm sorry I should have explained it better. The form is working correctly, however, it picks the first item on the menu. The first item says "choose a reason". I don't want it to say this every time the user fails to choose a reason. What I need is to tell the user to choose a reason. Thanks

m1a2x3x7
05-26-2011, 06:22 PM
Your function isn't testing anything against $reason...

Try something like this:


function is_valid_reason($var)
{
if($var == 'choose a reason')
{
echo 'Sorry, you must choose a reason for contacting us. Please go back and enter your reason';
}
}

$reason = 'choose a reason';

is_valid_reason($reason);

gigiloumill
05-26-2011, 08:08 PM
Thaks Chris, it seems to be working, however, it asks to validate the "comments" as well. This is what's echoing "Sorry, you must choose a reason for contacting us. Please go back and enter your reasonSorry, you must tell us your comments. Please go back and enter your comments" and I can't figure out why. Any other suggestions? thanks, again.

m1a2x3x7
05-26-2011, 08:20 PM
You have the same issue as you did earlier. Your function isn't testing anything.


function is_valid_comments($comments) {
return ( $comments);
}


All this is going to do is return $comments. Nothing is telling it to check if it's holding anything.

You need to do the same thing you are doing for the is_valid_reason.

gigiloumill
05-26-2011, 08:34 PM
It seems to be working the way I originally had it, but, without the "reason" http://about.ilouie.com/contact.php

m1a2x3x7
05-27-2011, 01:12 PM
I just tested the form. What doesn't seem to be working correctly?

I did notice in your HTML you have no values set for reason.


<select name="reason" id="reason">
<option>-choose a reason-</option>
<option>General feedback about ilouie</option>
<option>Sending a link to an article</option>
<option>Sending a link to an entrepreneur</option>
<option>Publisher inquiry</option>
<option>Media inquiry</option>
</select>
You need something like this:


<select name="reason" id="reason">
<option value="choose a reason">-choose a reason-</option>
<option value="General feedback about ilouie">General feedback about ilouie</option>
<option value="Sending a link to an article">Sending a link to an article</option>
<option value="Sending a link to an entrepreneur">Sending a link to an entrepreneur</option>
<option value="Publisher inquiry">Publisher inquiry</option>
<option value="Media inquiry">Media inquiry</option>
</select>

davidj
05-27-2011, 01:33 PM
Is there a reason you are creating 2 objects which are the same?

example code from above

$mail = new PHPMailer();$mail = new PHPMailer();

gigiloumill
05-27-2011, 02:04 PM
Thanks guys, and David, I didn't see that. Thank you for pointing it out. Chris, I would add values.