Help, I'm Stupid

I’m just setting up a small contact form, but seeing as my knowledge of PHP is all through copypasta, I need help. Everything was sending fine until I tried to set up an HTML email format… now the code just shows up in my email without any of the values filled in. In addition to fixing that, I was hoping to redirect users to a thank you page, and perhaps make the error boxes popups without reloading the entire form (Javascript?), and bring democracy to Cuba. If this is not possible, just help with the HTML thing would be much appreciated. Code:

<?php
	if($_POST){
		$to = '---removed---';
		$subject = 'Contact';
		$from_name = $_POST['name'];
		$from_email = $_POST['email'];
		$from_phone = $_POST['phone'];
		$comments = $_POST['comments'];
		$robotest = $_POST['robotest'];
		if($robotest)
			$error = "Are you a robot?";
		else{
			if($from_name && $from_email or $from_phone && $comments){

$headers = 'From: $_POST['$from_name']\\r\
';
$headers .= 'Reply-To: $_POST['$from_email']\\r\
';
$headers .= 'CC: ---removed---\\r\
';
$headers .= 'MIME-Version: 1.0\\r\
';
$headers .= 'Content-Type: text/html; charset=ISO-8859-1\\r\
';
$message = '<html><body>';
$message .= '<font size="10">---Title---</font> $Date $Time';
$message .= '<hr width="100%">';
$message .= '<table cellpadding="2" cellspacing="0" border="0">';
$message .= '<tr><td width="250">Name:</td><td><b>$from_name</b></td></tr>';
$message .= '<tr><td width="250">E-mail Address:</td><td>$from_email</td></tr>';
$message .= '<tr><td width="250">Phone Number:</td><td><b>$phone</b></td></tr>';
$message .= '</table>';
$message .= '<hr width="100%">';
$message .= '<table cellpadding="2" cellspacing="0" border="0">';
$message .= '<tr><td width="250">Comments:</td><td><b>$comments</b></td></tr>';
$message .= '</body></html>';

				$header = "From: $from_name <$from_email>";

				if(mail($to, $subject, $message, $headers))
					$success = "Thank you for contacting us. If a response is required, someone will 

get back to you in one business day.";
				else
					$error = "There was a problem sending the email.";
			}else
				$error = "Please enter your name, message and email or phone number.";
		}
		if($error)
			echo '<div class="msg error">'.$error.'</div>';
		elseif($success)
			echo '<div class="msg success">'.$success.'</div>';
	}
?>

<form method="post" action="">

<span class="contact"><b>Contact Us</b></span><br>
<br>

<table cellpadding="0" cellspacing="0" border="0">

<tr>
<td width="70px" valign="top" class="contact" align="left">Name:</td>
<td width="20px"></td>
<td align="left"><input type="text" name="name" id="name" size="35" /><br><br></td>
</tr>

<tr>
<td width="70px" valign="top" class="contact" align="left">Email:</td>
<td width="20px"></td>
<td align="left"><input type="text" name="email" id="email" size="35" /><br><br></td>
</tr>

<tr>
<td width="70px" valign="top" class="contact" align="left">Phone:</td>
<td width="20px"></td>
<td align="left"><input type="text" name="phone" id="phone" size="25" /><br><br></td>
</tr>

<tr>
<td width="70px" valign="top" class="contact" align="left">Message:</td>
<td width="20px"></td>
<td align="left"><textarea name="comments" id="comments" rows="7" cols="70" maxlength="2000"></textarea><br><br></td>
</tr>

<tr>
<td></td>
<td></td>
<td>
<!-- The following field is for robots only, invisible to humans: -->
<p class="robotic" id="pot">
<label>If you're human leave this blank:</label>
<input name="robotest" type="text" id="robotest" class="robotest" />
</td>
</tr>

<tr>
<td width="70px"></td>
<td width="20px"></td>
<td align="left"><input type="submit" name="submit" value="Submit"></td>
</tr>

</table>

</form>

Perhaps coloring things in will help us see something (and i dont have mod powers, so i’ll do it meself :wink:


<?php
if($_POST){
$to = '---removed---';
$subject = 'Contact';
$from_name = $_POST['name'];
$from_email = $_POST['email'];
$from_phone = $_POST['phone'];
$comments = $_POST['comments'];
$robotest = $_POST['robotest'];
if($robotest)
$error = "Are you a robot?";
else{
if($from_name && $from_email or $from_phone && $comments){

$headers = 'From: $_POST['$from_name']\\r\
';
$headers .= 'Reply-To: $_POST['$from_email']\\r\
';
$headers .= 'CC: ---removed---\\r\
';
$headers .= 'MIME-Version: 1.0\\r\
';
$headers .= 'Content-Type: text/html; charset=ISO-8859-1\\r\
';
$message = '<html><body>';
$message .= '<font size="10">---Title---</font> $Date $Time';
$message .= '<hr width="100%">';
$message .= '<table cellpadding="2" cellspacing="0" border="0">';
$message .= '<tr><td width="250">Name:</td><td><b>$from_name</b></td></tr>';
$message .= '<tr><td width="250">E-mail Address:</td><td>$from_email</td></tr>';
$message .= '<tr><td width="250">Phone Number:</td><td><b>$phone</b></td></tr>';
$message .= '</table>';
$message .= '<hr width="100%">';
$message .= '<table cellpadding="2" cellspacing="0" border="0">';
$message .= '<tr><td width="250">Comments:</td><td><b>$comments</b></td></tr>';
$message .= '</body></html>';

$header = "From: $from_name <$from_email>";

if(mail($to, $subject, $message, $headers))
$success = "Thank you for contacting us. If a response is required, someone will 

get back to you in one business day.";
else
$error = "There was a problem sending the email.";
}else
$error = "Please enter your name, message and email or phone number.";
}
if($error)
echo '<div class="msg error">'.$error.'</div>';
elseif($success)
echo '<div class="msg success">'.$success.'</div>';
}
?>

Well, not immediate, but i’d escape those direct references to $_POST, personally…

EDIT: Dont if($_POST). if(isset($_POST[‘name’])) … $_POST will always exist…

EDIT2 : $headers = ‘From: $_POST[’$from_name’]\r
'; … surely you meant either $_POST[‘name’] or $from_name, not the amalgamation of both…

Lastly, you can’t use $_POST[‘name’] within single quotes, nor can you use \r
as single quotes means to output the literal character, don’t parse it. You need to use double quotes and you really should put $_POST[‘name’] within curly braces {$_POST[‘name’]}

Well, I really appreciate the responses. I tried to implement the changes you guys mentioned, but like the thread title says… In any case, per divine intervention and a lot of research, I put together this Frankenstein’s monster of code that actually works! Everything works. It brings tears of joy to my eyes. Though I do have two questions (which I’m still poking around on the internet for the answers).

  1. I have a drop-down menu with a blank value as its first choice. How do I get the PHP to read this as a blank value and NOT send the form when it’s chosen? The field is already required, but the PHP is still reading it as a valid choice.

  2. Out of curiosity, is there a way to make required fields optional… as in, “email OR phone” required?

Thanks! Updated code to follow.


<?php
    $filled = true;
    $required = array("location", "name", "email", "comments"); //all the required fields

    //Cycle through each field and make sure its filled

    foreach ($required as &$value) {

        if($_POST[$value]==""){
            $filled = false;
        }
    }

    //If there are any fields not filled out
    if (!$filled){
            echo "
                <script language=\\"javascript\\" type=\\"text/javascript\\">
                    alert('Please fill in your location, name, email and message.');
                    history.go(-1);
                </script>"; 
    }

	if($robotest){
		echo "
                <script language=\\"javascript\\" type=\\"text/javascript\\">
                    alert('You are a robot!');
                    history.go(-1);
                </script>"; 
    }

    else{
	$location = stripslashes($_POST['location']);
        $name = stripslashes($_POST['name']);
        $email = stripslashes($_POST['email']);
        $phone = stripslashes($_POST['phone']);
        $comments = stripslashes($_POST['comments']);
	$robotest = $_POST['robotest'];

    $mail_to = "---edit";
    $subject = "Contact";

    $date = date("l F d Y H:i a");

    $message = "<html><body>";
    $message .= "<font size=9>---title</font> $date<br>";
    $message .= "<hr>";
    $message .= "<table><tr>";
    $message .= "<td width=150><b>Name:</b></td>";
    $message .= "<td><b>$name</b></td></tr>";
    $message .= "<tr><td width=150><b>Email:</b></td>";
    $message .= "<td>$email</td></tr>";
    $message .= "<tr><td width=150><b>Phone Number:</b></td>";
    $message .= "<td>$phone</td></tr>";
    $message .= "<tr><td width=150><b>Closest Location:</b></td>";
    $message .= "<td>$location</td></tr></table>";
    $message .= "<hr>";
    $message .= "<table><tr>";
    $message .= "<td valign='top' width=150><b>Comments:</b></td>";
    $message .= "<td>$comments</td></tr>";
    $message .= "</body></html>";

    $headers = "From: $name <$email> ";
    $headers .= "MIME-Version: 1.0\\r\
";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\\r\
";

    $mail_status = mail($mail_to,$subject,$message,$headers);

        if ($mail_status) { 
            echo "
                <script language=\\"javascript\\" type=\\"text/javascript\\">
                    window.location = 'thanks.htm';
                </script>";    
        }

        else {
            echo "
                <script language=\\"javascript\\" type=\\"text/javascript\\">
                    alert('Sorry, the email failed to send. Please contact ---edit.');
                    window.location = 'contact2.php';
                </script>";
        }
    }
?>

Sorry about the prior lack of coloring/proper posting. Figured out how to do it.

For 1., if the posted value from your form is what you set your value to be for your first option in your drop down menu, then don’t accept the submission.

For 2. don’t accept the submission if both posts are empty:

if (empty($_POST['email']) && empty($_POST['name'])) {
//don't submit
}

Hmm… I guess I don’t understand how to do this. I tried giving the blank selection its own name/id, with and without the <select> tag having a name. I just repeated the code I used for $robotest, which I thought would catch that specific field. So, I thought something like?


<select>
<option value="" name="blank"></option>
<option value="Option 1" name="location">Option 1</option>
<option value="Option 2" name="location">Option 2</option>
<option value="Option 3" name="location">Option 3</option>
</select>


    if (!$blank){
            echo "
                <script language=\\"javascript\\" type=\\"text/javascript\\">
                    alert('Please choose your closest location.');
                    history.go(-1);
                </script>"; 
    }

No combination of what I’m doing works. If any option on the drop-down menu is selected, it gives the pop-up error.

EDIT: Oh, it gives the error, sends the form anyway, and then goes back to the page per the history.go script. Haha.

What you are missing is that the form has nothing to do with PHP.

PHP may ( or may not, as you can hard code HTML) output the HTML to a for, and PHP can process the data that is return , but the behaviour of a form is in the realm of HTML. In other words, dont look for an answer to #1 in PHP :slight_smile:

Speaking of which, your HTML is invalid. the name attribute goes in the SELECT not the option, and the OPTION holds the value.


<select name="location">
<option></option>
<option value="Option 1"  >Option 1</option>
<option value="Option 2"  >Option 2</option>
<option value="Option 3"  >Option 3</option>
</select>

SELECTs ( what you are calling “drop down”) send values upon submission. After submission , you can USE PHP to validate the form… and go back to the original document if any required fields are missing.


if (empty($_POST['email']) && empty($_POST['name'])  && !$_POST['location']) {
//don't submit
}  

An alternative which I find less palatable, is to use javascript (if you think you are struggling now… ). You can disable the form onSumbit, so that its only triggered via a .js you would then attach to an onclick even on a form button, the onclick event would then call a script to see the value of form.location (and any other required fields you code it to check for ) is blank and and return false, if the reason I find it less palatable is because the user can turn off javascript in his/her browser and then you’d be back where you started.