PDA

View Full Version : Conditional Contact Form


BullDog
11-23-2011, 04:33 AM
I have always used something like this as an example to work off of:

http://www.freecontactform.com/email_form.php

What I want is a topic selection of option1, option2, other:

the option selected would determine the email address the form is submitted to.

I've looked for examples of this unsuccessfully. Is this something fairly basic to set up? I am as green as you get when it comes to PHP.

BullDog
11-23-2011, 04:45 AM
would something like this work or is there a better method?



<form name='mailForm' method='post' action='mailsend.php' enctype='text/plain'>

<select name='me' size='1'>

<option value='webmaster'>Web Master</option>

<option value='admin'>Admin</option>

</select>

<input type='submit'>

</form>



$mailto = $_POST['me'];



if($mailto == 'webmaster')

{

$email = "one@one.com";

}elseif($mailto == 'admin'){

$email = "two@two.com";

};

BullDog
11-23-2011, 08:04 AM
I started the form layout based on an example in css (no tables).

I have run into a couple issues right off the bat. the radio button alignment you'll notice. And how would you handle the message area? the css wont allow for scrolling.

Test Form (http://www.bugorama.com/test/contact_form.html)

BullDog
11-23-2011, 01:08 PM
well after hours of trying this over and over with different methods. I decided to switch the radio buttons for a combo box, making for easier alignment. And finally realized the "overflow: hidden;" is what killed the scrollbar.

My original question is still valid tho... regarding a conditional mailto.

Corrosive
11-23-2011, 01:21 PM
So is the condition for who to send the e-mail to driven by the end user? I.e. they select sales, support or customer care (as an example) in the form?

BullDog
11-23-2011, 02:10 PM
correct. the category selected will determine the address the processed form is sent to.

http://bugorama.com/test/contact.php

Corrosive
11-23-2011, 04:26 PM
OK, I'd take the 'if' statements out if the equation and and feed in the correct e-mail address using the 'select' in the form. Set it up like;


<select>
<option value="webmaster@example.com">Webmaster</option>
<option value="sales@example.com">Sales</option>
<option value="care@example.com">Customer Care</option>
</select>



That way the customer sees the options but the value passed to the $mail variable will be the full e-mail address. Then you can use it with the mailto function.

Make sense?

BullDog
11-23-2011, 04:36 PM
yes I think so... thank you. looks like my form needs some changes before I can start to figure out the php.

BullDog
11-24-2011, 12:18 AM
based on a few examples this is as far as I got. I'm not sure how to pass the combo box value into the process file. nor can I figure out how to make sure the combo box is validated. something must be chosen.

http://bugorama.com/test/contact.php


<?php

if(isset($_POST['email'])){

// Here is the email to information
$email_to = "your email address goes here";
$email_subject = "From Website 2012 Contact Form";
$email_from = "Bugorama Promotions 2012";

// Error code
function died($error){
echo "We are sorry, but there were error(s) found with the form you submitted.";
echo "These errors appear below.<br /><br />";
echo $error. "<br /><br />";
echo "Please go back and fix these errors. <br />";
die();
}

// Validation
if(!isset($_POST['category']) ||
!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['message'])){
died('We are sorry, but there appears to be a problem with the form you submitted.');
}

$category = $_POST['category'];
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.+[A-Za-z] (2,4)$/';
if(!preg_match($email_exp, $email)) {
$error_message .= 'The email address you entered does not appear to be valid.<br />';
}
$string_exp = " /^[A-Za-z.'-]+$/";
if(!preg_match($string_exp, $name)){
$error_message .= 'The name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2){
$error_message .= 'The message you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below. \n\n";

function clean_string($string){
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace(bad, "", $string);
}
$email_message .= "Name:" . clean_string($name) . "\n";
$email_message .= "Email:" . clean_string($email) . "\n";
$email_message .= "Message:" . clean_string($message) . "\n";

// Create email headers
$headers = 'From: ' .$email_From . "\r\n". 'Reply-To:' . $email. "\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- Success message goes here -->
<strong>
<font size="+1">
Thank you for contacting us. We will be in touch with you shortly.
<p>Please click <a href="index.php">HERE</a> to return to the home page.</p>
</font>
</strong>
<?php
}
?>

BullDog
11-24-2011, 12:49 AM
will this work for validation, to make sure something is selected? What about the default selection with a value of "0"?


if(!isset($_POST['category'])) {
$errorMessage .= "Please select a category.";
}

BullDog
11-24-2011, 12:50 AM
any help is appreciated as this is WAY over my head.

Thanx!

Corrosive
11-24-2011, 08:43 AM
You've got the right idea using isset but which form element(s) are you trying to validate?

edbr
11-24-2011, 08:43 AM
will this work for validation, to make sure something is selected? What about the default selection with a value of "0"?


if(!isset($_POST['category'])) {
$errorMessage .= "Please select a category.";
}


did you try testing it?

BullDog
11-24-2011, 06:14 PM
You've got the right idea using isset but which form element(s) are you trying to validate?
there are 3 options in the combo box I need to validate one is chosen, and not the default

did you try testing it?
yes I've been testing but honestly dont understand it 100% either

edbr
11-25-2011, 02:00 AM
your example is asking if that has been posted, not checking its value.

you could use an if condition
if(value != ) // value does not equal
{do something}

BullDog
11-26-2011, 12:34 AM
http://www.bugorama.com/test/contact.php

I had some help but it wasnt all working when I double checked it this morning.


CHANGED:
$string_exp = " /^[A-Za-z.'-]+$/";
if( !isset( $name ) ) {
$error[ 'name' ] = 1;
$errorFlag = 1;
}

TO:
$string_exp = " /^[A-Za-z.'-]+$/";
if( !isset( $name ) || strlen( $name ) < 1 ) {
$error[ 'name' ] = 1;
$errorFlag = 1;
}
It works now with the string length check. but I thought :

"if( !isset( $name ) )" checks to see if the field is set. meaning it has input entered. If thats the case why would I need to check that the input is less than 1?

Thank you.

BullDog
11-26-2011, 12:50 AM
Looking at the code I think in addition to the preg_match for name, I need to add preg match to phone and subject. Even tho its not required. wouldnt I want to prevent garbage and links put in those fields?

Rethinking this I think I need to use preg_match for the name, for the number to prevent them from entering a character by mistake if they want a call back, but leave the subject and message open as they may have a link they want looked at.

Does this make sense or am I over thinking a simple form?

edbr
11-26-2011, 03:17 AM
have a read of this http://www.html-form-guide.com/php-form/php-form-processing.html

BullDog
11-26-2011, 03:34 AM
Thank you.. definately an easier way to check if field is empty. I was looking more for opinions of weather or not to use something like preg_match to limit the characters available in the two un-required fields. but I think I am over-thinking it. I just started wondering if someone sent a malicious link and the site owner mistakenly clicked on it... they simply should know not to click on a link unless they know who its from and what it is.

I looked at a handful of other forms and if the field isn't required there doesn't seem to be character checks as the norm.

Thanx again for all the help everyone. I know I've been asking a lot of random questions as they come up for me. Just trying to retrain myself a little. I've obviously developed a LOT of poor coding habits over the years.

BullDog
11-26-2011, 05:53 AM
I have one last issue with the contact form. the thank you page displays // if there are any quotation marks used in the form input and the emailed results show "quote" as \"quote\"

I see this is a result of magic quotes or at least I think it is.

How would I use stripslashes() for all input fields without affecting the rest of the code? Or is there a better method?

BullDog
11-26-2011, 06:34 AM
would this work? they are on 1and1.com

http://faq.1and1.co.uk/scripting/php/using_php5/2.html

They have two sites on this shared server so I am not sure how this would affect either site.

BullDog
11-26-2011, 07:17 AM
I tried:


<?php echo stripslashes($message);?><br />


for both the email results and display results and this had no affect.

There must be a way to allow quotes, single quotes, and back slashes.

BullDog
11-26-2011, 08:31 AM
This was also already done for a past wordpress install.

Create a ".htaccess" file containing the following line:
AddType x-mapp-php5 .php

~~~~~~~~~~~~~~~

Not sure what to try next

BullDog
11-26-2011, 09:14 AM
tried adding the following to my .htaccess but it killed all php

php_flag magic_quotes_gpc Off

~~~~~~~~~~~~~~~~~~~~~~~~~~

creating a php.ini file worked for the email output but not for the thank you display echoing the input sent. problem being it has to be duplicated in directory where you want to disable magic quotes.

php.ini - contains the following line only:
magic_quotes_gpc = Off

~~~~~~~~~~~~~~~~~~~~~~~~~~

these may be antiquated methods, but I'm trying everything I can.

Corrosive
11-26-2011, 10:26 AM
Can you not just feed the message portion through stripslashes() into a second variable?

BullDog
11-26-2011, 10:44 AM
Over my head unfortunately corrosive. I need to take a step back, and do what I know until I can test & understand all of this. I love the forum and have learned a LOT. but I need to stick to whats needed for now. I find it hard not to attempt something new. not a bad thing, but the headache is not needed when I am trying to finish a project for someone.

I've gone back and checked a handful of my old sites, and not a one validates. every contact form I checked displays the same magic quote problem too.

They dont need the input echo'd with the thank you. so I'll remove that for now. the directory level php.ini works to disable magic quotes for php files in the same folder.

Corrosive
11-26-2011, 11:35 AM
I find it hard not to attempt something new. not a bad thing, but the headache is not needed when I am trying to finish a project for someone.



Yeah, me too :)

edbr
11-27-2011, 03:14 AM
its part of the process but it will come. for future reference you can maybe check a validation class written by Olaf Lederer, http://www.finalwebsites.com/snippets.php?id=15


or play with a more general function

function clean($data) {
$data = trim($data);
$data = htmlentities($data);
if(get_magic_quotes_gpc()) {
$data = stripslashes($data);
}
$data = mysql_real_escape_string($data);
return $data;
} use it like this $comment = clean($_POST['comment']);