Hi everyone!
So you can see how often I use PHP, seeing my last post was 2009.
My goal:
Keep the contact page HTML, hence the linked sheet
Have email come from the host, reply to the user
Have email and name required only
Have submission success on the SAME HTML page
So here is what I tried to do, never worked:
<?php
$to = "
[email protected]";
$subject = "XXX Web Inquiry";
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$selection = $_POST['selection'];
$comment = $_POST['comment'];
$body .= "Name: " .$POST['name']."\n";
$body .="E-Mail: ".$POST['email']."\n";
$body .="Phone: ".$POST['phone']."\n";
$body .="Selection: ".$POST['selection']."\n";
$body .="Comment: ".$POST['comment']."\n";
$errEmail = $errName = "";
$name = $email = $phone = $selection = $comment = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST['name'])) {
$errName = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
$errEmail = "Valid email is required";
} else {
$email = test_input($_POST['email']);
}
if (empty($_POST['phone'])) {
$phone = "";
} else {
$phone = test_input($_POST['phone']);
}
if (empty($_POST['comment'])) {
$comment = "";
} else {
$comment = test_input($_POST['comment']);
}
if (empty($_POST['selection'])) {
$selection = "";
} else {
$selection = test_input($_POST['selection']);
}
$msgError = "ERROR: Please re-submit the form; we apologize!";
$msgSuccess = "Sent -- Thank you " . $name . ", we will contact you shortly!";
$header .= 'From: '. $_REQUEST['name']. "\r\n". 'Reply-To: '. $_REQUEST['email'] . "\r\n";
if (isset($_POST['submit'])) {
mail($to, $header, $subject, $body, $email);
echo $msgSuccess;
} else {
echo $msgError;
}
?>
Here is one I stole that I got to somewhat work, as far as just emailing me, but has no restrictions (e.g., would submit empty), didn't always send content, and redirected to the PHP page, so I don't wish to use it; I think I may need to combine the two + add fxn to stay on the same page:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "
[email protected]";
$email_subject = "XXX Website Inquiry";
function died($error) {
// your error code can go here
echo "We are very 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 /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['selection']) ||
!isset($_POST['email']) ||
!isset($_POST['phone']) ||
!isset($_POST['comment'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$selection = $_POST['selection']; // required
$email = $_POST['email']; // required
$phone = $_POST['phone']; // not required
$comment = $_POST['comment']; // required
$email_message = "Hello, XXX! See 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)."\r\n";
$email_message .= "Email: ".clean_string($email)."\r\n";
$email_message .= "Phone: ".clean_string($phone)."\r\n";
$email_message .= "Selection: ".clean_string($selection)."\r\n";
$email_message .= "Comment(s): ".clean_string($comment)."\r\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
I even tried a blip of Javascript in the HTML form page and added a DIV for the responses to appear on the same page, but all it did was gray the button; nothing else:
<script>
$("#contact").submit(function(event)
{
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $( this ),
$submit = $form.find( 'input[type="submit"]' ),
name_value = $form.find( 'input[name="name"]' ).val(),
email_value = $form.find( 'input[name="email"]' ).val(),
phone_value = $form.find( 'input[name="phone"]' ).val(),
selection_value = $form.find( 'input[name="selection"]' ).val(),
comment_value = $form.find( 'textarea[name="comment"]' ).val(),
url = $form.attr('action');
/* Send the data using post */
var posting = $.post( url, {
name: name_value,
email: email_value,
phone: phone_value,
selection: selection_value,
comment: comment_value
});
posting.done(function( data )
{
/* Put the results in a div */
$( "#contactResponse" ).html(data);
/* Change the button text. */
$submit.text('Sent, Thank you');
/* Disable the button. */
$submit.attr("disabled", true);
});
});
</script>
Finally, the HTML:
<div>
<form action="contact2.php" method="post" id="contact">
<div class="form-horizontal control-group">
<div class="row">
<div class="span12 leftPad">
<label for="name">Name:</label><input type="text" name="name">
</div>
</div><!--row-->
<br>
<div class="row">
<div class="span12 leftPad">
<label for"email">Email:</label><input type="email" name="email">
</div>
</div><!--row-->
<br>
<div class="row">
<div class="span12 leftPad">
<label for="phone">Phone:</label><input type="tel" name="phone">
</div>
</div><!--row-->
<br>
<div class="row">
<div class="span12 leftPad">
<label for="selection">Area of Interest:</label>
<select name="selection">
<option value="General Question">General Question</option>
<option value="Inside Sales">Inside Sales</option>
<option value="Service">Service</option>
<option value="Request a Quote">Request a Quote</option>
</select>
</div>
</div><!--row-->
<br>
<div class="row">
<div class="span12 leftPad">
<label for="comment">Comments / Application Specifics</label><textarea rows="10" name="comment"></textarea>
</div>
</div><!--row-->
<br>
<div class="row">
<div class="span12 leftPad">
<input type="submit" name="submit">
</div>
</div><!--row-->
<br>
<!--<div id="contactResponse"></div>-->
<br>
</div> <!--form horizontal control group-->
</form>
</div>
I appreciate this very much!!...I assume it's simple for you guys, and I thought I could figure it out. Certainly it isn't impossible to fulfill my simple bulleted list above? I have failed.
-t