Having some trouble with the contact form on my website, http://www.techcom.co.nz/
It works fine on google chrome, however on IE and Firefox it doesn't send any message and just redirects back up to the top of the page/refreshes the page.
Here is the php code for the form:
// EDIT THE FOLLOWING LINE BELOW AS REQUIRED
$send_email_to = "techcomnz#gmail.com";
function send_email($name,$email,$email_subject,$email_message)
{
global $send_email_to;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ".$email. "\r\n";
$message = "<strong>Email = </strong>".$email."<br>";
$message .= "<strong>Name = </strong>".$name."<br>";
$message .= "<strong>Message = </strong>".$email_message."<br>";
#mail($send_email_to, $email_subject, $message,$headers);
return true;
}
function validate($name,$email,$message,$subject)
{
$return_array = array();
$return_array['success'] = '1';
$return_array['name_msg'] = '';
$return_array['email_msg'] = '';
$return_array['message_msg'] = '';
$return_array['subject'] = '';
if($email == '')
{
$return_array['success'] = '0';
$return_array['email_msg'] = 'email is required';
}
else
{
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$return_array['success'] = '0';
$return_array['email_msg'] = 'enter valid email.';
}
}
if($name == '')
{
$return_array['success'] = '0';
$return_array['name_msg'] = 'name is required';
}
else
{
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $name)) {
$return_array['success'] = '0';
$return_array['name_msg'] = 'enter valid name.';
}
}
if($subject == '')
{
$return_array['success'] = '0';
$return_array['subject_msg'] = 'subject is required';
}
if($message == '')
{
$return_array['success'] = '0';
$return_array['message_msg'] = 'message is required';
}
else
{
if (strlen($message) < 2) {
$return_array['success'] = '0';
$return_array['message_msg'] = 'enter valid message.';
}
}
return $return_array;
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$return_array = validate($name,$email,$message,$subject);
if($return_array['success'] == '1')
{
send_email($name,$email,$subject,$message);
}
header('Content-type: text/json');
echo json_encode($return_array);
die();
?>
and here is the html:
<fieldset id="contact_form">
<div id="msgs"> </div>
<form id="cform" name="cform" method="post" action="">
<input type="text" id="name" name="name" value="Full Name*" onfocus="if(this.value == 'Full Name*') this.value = ''"
onblur="if(this.value == '') this.value = 'Full Name*'" />
<input type="text" id="email" name="email" value="Email Address*" onfocus="if(this.value == 'Email Address*') this.value = ''"
onblur="if(this.value == '') this.value = 'Email Address*'" />
<input type="text" id="subject" name="subject" value="Subject*" onfocus="if(this.value == 'Subject*') this.value = ''"
onblur="if(this.value == '') this.value = 'Subject*'" />
<textarea id="msg" name="message" onfocus="if(this.value == 'Your Message*') this.value = ''"
onblur="if(this.value == '') this.value = 'Your Message*'">Your Message*</textarea>
<button id="submit" class="button" style=""> Send Message</button>
</form>
</fieldset>
Any help would be greatly appreciated.
Regards
Leaving the action attribute blank will cause most browsers to submit the form data to the current page by reloading it with the data in GET or POST.
If this is your intention, try removing the action="" altogether as it has been known to cause issues with older browsers.
Related
I have a simple Contact Form
it works very freat but I want the result on the same page in a small box
just under the form
where the fail messages are coming and success too
<form method="post" action="contact.php" name="contactform" id="contactform">
<div class="one_half">
<input name="name" type="text" id="name" size="30" onfocus="if(this.value == 'Name') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Name'; }" value="Name">
<input name="alter" type="text" id="alter" size="3" onfocus="if(this.value == 'Alter') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Alter'; }" value="Alter">
<input name="email" type="text" id="email" size="30" onfocus="if(this.value == 'E-Mail') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'E-Mail'; }" value="E-Mail">
<input name="phone" type="text" id="phone" size="30" onfocus="if(this.value == 'Handynummer') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Handynummer'; }" value="Handynummer">
<input name="facebook" type="text" id="facebook" size="200" onfocus="if(this.value == 'Facebook') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Facebook'; }" value="Facebook">
<input name="instagram" type="text" id="instagram" size="200" onfocus="if(this.value == 'Instagram') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Instagram'; }" value="Instagram">
</div>
<div class="one_half last">
<textarea name="comments" cols="40" rows="3" id="comments" onfocus="if(this.value == 'Nachricht') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Nachricht'; }">Nachricht</textarea>
</div>
<input type="submit" class="send_message" id="submit" value="Senden"/>
</form>
and here the PHP file
<?php
if (!$_POST) exit;
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
$alter = $_POST['alter'];
$facebook = $_POST['facebook'];
$instagram = $_POST['instagram'];
if (get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
$address = "xxx#xxx.de";
$e_subject = 'Contact from ' . $name;
$e_body = "von: $name" . PHP_EOL . PHP_EOL;
$e_reply = "Alter: $alter\r\nE-mail: $email\r\nHandynummer: $phone";
$e_content = "Nachricht:\r\n$comments" . PHP_EOL . PHP_EOL;
$e_links = "Facebook:\r\n$facebook\r\nInstagram:\r\n$instagram" . PHP_EOL . PHP_EOL;
$msg = wordwrap($e_body . $e_links . $e_content . $e_reply, 70);
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if (mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h1>Bewerbung erfolgreich</h1>";
echo "<p>Danke <strong>$name</strong>, deine Bewerbung wurde erfolgreich an uns gesendet</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'FEHLER!';
}
It works PERFECT
but it's always getting on a new site, no matter if error or success.
I want to add a small block under the form where all results are displayed
I hope u can help me.
I don't want ajax or something else. I just want it added like it is.
A solution for your problem might be to have in the same PHP file the logic with the form rendering. Doing this, the form action will be the same PHP file, so it will load the PHP code before rendering the form view. By that, you'll be able to render bellow the form whatever you want to according with the output from the sending email.
For example, take a closer look at the $mailResult variable:
<?php
$name = $_POST['name'] ?? null;
$email = $_POST['email'] ?? null;
$phone = $_POST['phone'] ?? null;
$comments = $_POST['comments'] ?? null;
$alter = $_POST['alter'] ?? null;
$facebook = $_POST['facebook'] ?? null;
$instagram = $_POST['instagram'] ?? null;
if (get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
$address = "xxx#xxx.de";
$e_subject = 'Contact from ' . $name;
$e_body = "von: $name" . PHP_EOL . PHP_EOL;
$e_reply = "Alter: $alter\r\nE-mail: $email\r\nHandynummer: $phone";
$e_content = "Nachricht:\r\n$comments" . PHP_EOL . PHP_EOL;
$e_links = "Facebook:\r\n$facebook\r\nInstagram:\r\n$instagram" . PHP_EOL . PHP_EOL;
$msg = wordwrap($e_body . $e_links . $e_content . $e_reply, 70);
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
$mailResult = '';
if (isset($name, $email)) {
if (mail($address, $e_subject, $msg, $headers)) {
$mailResult = "<fieldset>";
$mailResult .= "<div id='success_page'>";
$mailResult .= "<h1>Bewerbung erfolgreich</h1>";
$mailResult .= "<p>Danke <strong>$name</strong>, deine Bewerbung wurde erfolgreich an uns gesendet</p>";
$mailResult .= "</div>";
$mailResult .= "</fieldset>";
} else {
$mailResult .= 'FEHLER!';
}
}
?>
<html lang="en">
<head>
<title>Title page!</title>
</head>
<body>
<form method="post" name="contactform" id="contactform">
<div class="one_half">
<input name="name" type="text" id="name" size="30"
onfocus="if(this.value === 'Name') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'Name'; }"
value="Name">
<input name="alter" type="text" id="alter" size="3"
onfocus="if(this.value === 'Alter') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'Alter'; }"
value="Alter">
<input name="email" type="text" id="email" size="30"
onfocus="if(this.value === 'E-Mail') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'E-Mail'; }"
value="E-Mail">
<input name="phone" type="text" id="phone" size="30"
onfocus="if(this.value === 'Handynummer') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'Handynummer'; }"
value="Handynummer">
<input name="facebook" type="text" id="facebook" size="200"
onfocus="if(this.value === 'Facebook') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'Facebook'; }"
value="Facebook">
<input name="instagram" type="text" id="instagram" size="200"
onfocus="if(this.value === 'Instagram') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'Instagram'; }"
value="Instagram">
</div>
<div class="one_half last">
<textarea name="comments" cols="40" rows="3" id="comments"
onfocus="if(this.value === 'Nachricht') { this.value = ''; }"
onblur="if(this.value === '') { this.value = 'Nachricht'; }">
Nachricht
</textarea>
</div>
<input type="submit" class="send_message" id="submit" value="Senden"/>
</form>
<?php echo $mailResult ?>
</body>
</html>
I would anyway avoid this kind of solution. I would approach this problem with AJAX.
In these solutions, we are mixing the logic with the rendering.
I have the following HTML:
<form method="post" action="https://www.domain.co.uk/v2/contact.php" id="contactform">
<p><label for="name"><span class="required">*</span> Your Name</label><br>
<input name="name" type="text" id="name" size="30" value="" class="input" required> </p>
<p><label for="email"><span class="required">*</span> Email</label><br>
<input name="email" type="email" id="email" size="30" value="" class="input" required></p>
<p><label for="phone">Phone</label><br>
<input name="phone" type="tel" id="phone" size="30" value="" class="input"></p>
<p><label for="subject">Subject</label><br>
<select name="subject" id="subject" class="input">
<option value="Not sure" selected="selected">Not sure</option>
<option value="this">this</option>
<option value="that">that</option>
</select></p>
<p><label for="comments"><span class="required">*</span> Message</label><br>
<textarea name="comments" cols="40" rows="3" id="comments" class="input" required></textarea></p>
<p><input type="submit" class="submit" id="submit" value="Submit"></p>
</form>
and the following in contact.php
<?php
if ($_POST['name'] != "") {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Please enter a valid name.<br/><br/>';
}
} else {
$errors .= 'Please enter your name.<br/>';
}
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}
if ($_POST['phone'] != "") {
$_POST['phone'] = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
if ($_POST['phone'] == "") {
$errors .= 'Please enter your phone number.<br/>';
}
} else {
$errors .= 'Please enter your phone number.<br/>';
}
if ($_POST['subject'] != "") {
$_POST['subject'] = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
if ($_POST['subject'] == "") {
$errors .= 'Please choose a subject.<br/>';
}
} else {
$errors .= 'Please choose a subject.<br/>';
}
if ($_POST['comments'] != "") {
$_POST['comments'] = filter_var($_POST['comments'], FILTER_SANITIZE_STRING);
if ($_POST['comments'] == "") {
$errors .= 'Please enter a message.<br/>';
}
} else {
$errors .= 'Please enter a message.<br/>';
}
if (!$errors) {
$mail_to = 'info#myemails.co.uk';
$subject = 'Enquiry';
$message .= 'Regarding: ' . $_POST['subject'] . "\n\n";
$message .= 'Name: ' . $_POST['name'] . "\n\n";
$message .= 'Email: ' . $_POST['email'] . "\n\n";
$message .= 'Phone: ' . $_POST['phone'] . "\n\n";
$message .= 'Message ' . $_POST['comments'] . "\n\n";
$success = mail($mail_to, $subject, $message, "From: <$email>");
if ($success){
print "sent";
}
else{
print "failed";
}
}
?>
no matter what I change or try I end up on a blank white page for contact.php instead of seeing the sent or failed message (having removed my javascript validation incase I was causing issue there), likewise there is nothing in the error log and despite having gone back over the code I can't spot the issue? Unsure if I have stared at it for too long and missing something obvious or there is a deeper problem?
Any pointers appreciated.
var_dump shows it is getting the information:
array(5) { ["name"]=> string(10) "Joe Bloggs" ["email"]=> string(16) "joe#anyemail.com" ["phone"]=> string(11) "07123456789" ["subject"]=> string(8) "Not sure" ["comments"]=> string(17) "test message here" }
You used string concatenation, but you didn't defined your variables before that, if you change your code, like this:
<?php
$errors = '';
if ($_POST['name'] != "") {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Please enter a valid name.<br/><br/>';
}
} else {
$errors .= 'Please enter your name.<br/>';
}
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}
if ($_POST['phone'] != "") {
$_POST['phone'] = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
if ($_POST['phone'] == "") {
$errors .= 'Please enter your phone number.<br/>';
}
} else {
$errors .= 'Please enter your phone number.<br/>';
}
if ($_POST['subject'] != "") {
$_POST['subject'] = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
if ($_POST['subject'] == "") {
$errors .= 'Please choose a subject.<br/>';
}
} else {
$errors .= 'Please choose a subject.<br/>';
}
if ($_POST['comments'] != "") {
$_POST['comments'] = filter_var($_POST['comments'], FILTER_SANITIZE_STRING);
if ($_POST['comments'] == "") {
$errors .= 'Please enter a message.<br/>';
}
} else {
$errors .= 'Please enter a message.<br/>';
}
$message = '';
if (empty($errors)) {
$mail_to = 'info#myemails.co.uk';
$subject = 'Enquiry';
$message .= 'Regarding: ' . $_POST['subject'] . "\n\n";
$message .= 'Name: ' . $_POST['name'] . "\n\n";
$message .= 'Email: ' . $_POST['email'] . "\n\n";
$message .= 'Phone: ' . $_POST['phone'] . "\n\n";
$message .= 'Message ' . $_POST['comments'] . "\n\n";
$success = mail($mail_to, $subject, $message, "From: <$email>");
if ($success){
print "sent";
}
else{
print "failed";
}
} else {
echo $errors;
}
?>
It works. You said that you are at live site, so probably errors are not show up, if you want you can add this lines:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
at the top of your php file, to see errors, but clients also would see these errors. You can add IP Check for your IP around them.
I'm trying to make $name, $email, and $message validate in one script without making them all look like a error (make them all a red color) rather than the one that is actually incorrect.
Her is the code I'm using:
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$visitors_site = $_POST['site'];
$message = $_POST['message'];
$email_from = 'mattmowen1#gmail.com';
$email_subject = 'New Contact Submission';
$to = 'matt.owen#example.com';
$headers = "From:" . $email;
$headers = "Contact Submission From: " . $email;
$message1 = "Name: " . $name;
$message2 = "\n\nEmail: " . $email;
$message3 = "\n\nPhone: " . $phone;
$message4 = "\n\nTheir Site: " . $visitors_site;
$message5 = "\n\nMessage: " . $message;
$email_body = $message1 . $message2 . $message3 . $message4 . $message5;
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
mail($to, $email_subject, $email_body,$headers);
exit(json_encode(array('error' => 0)));
} else {
exit(json_encode(array('error' => 1)));
}
if ($name == "") {
exit(json_encode(array('error' => 1)));
} else {
mail($to, $email_subject, $email_body,$headers);
exit(json_encode(array('error' => 0)));
}
?>
AJAX Script:
var sendEmail = function(){
var url = 'main.php';
$.ajax({
url : url,
type : "POST",
dataType : "JSON",
data : $('#contact-form').serialize(),
success : function(response) {
if (response.error == 0) {
$('#contact-form')[0].reset();
alert('Form submitted successfully. We will contact you asap.');
} else {
$('#email-input').css('color', 'red');
alert('ERROR MESSAGE');
}
}
})
return false;
}
HTML:
<div id="contact">
<div class="container">
<form id="contact-form" method="post" onsubmit="return sendEmail()">
<h1>Contact Form</h1>
<fieldset>
<input placeholder="Your Name" type="text" name="name" id="name-input" required value="<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>">
</fieldset>
<fieldset>
<input placeholder="Your Email Address" type="email" name="email" id="email-input" required value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''; ?>">
</fieldset>
<fieldset>
<input placeholder="Your Phone Number (optional)" type="tel" name="phone" required>
</fieldset>
<fieldset>
<input placeholder="Your Web Site (optional)" type="url" name="site" required>
</fieldset>
<fieldset>
<textarea placeholder="Type your message here...." name="message" required value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''; ?>"></textarea>
</fieldset>
<fieldset>
<button type="submit" id="contact-submit" name="submit">Submit</button>
</fieldset>
</form>
</div>
</div>
Just send back a list of bad elements, instead of a blanket error statement
<?php
// ...
$errors = [];
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$errors[] = "email";
}
if ($name == "") {
$errors[] = "name";
}
if ($message == "") {
$errors[] = "message";
}
if (count($errors) === 0) {
mail($to, $email_subject, $email_body,$headers);
}
echo json_encode($errors);
//...
Then in your JS:
success : function(response) {
if (response.length == 0) {
$('#contact-form')[0].reset();
alert('Form submitted successfully. We will contact you asap.');
} else {
for (var i = 0; i < response.length; i++) {
$('#' + response[i] + '-input').css('color', 'red');
alert('ERROR MESSAGE');
}
}
}
My Javascript is a bit rusty but that should do the trick.
Note that <textarea> doesn't have a value attribute, contents are added as a child text node. You should also use htmlspecialchars() on all output from PHP to prevent XSS problems.
in your js:
$erro = 0;
if(document.getElementById("name-input").value == null or document.getElementById("name-input").value == ""){
$erro = 1;
document.getElementById("name-input").style.borderColor = "red";
}
if(document.getElementById("email-input").value == null or document.getElementById("email-input").value == ""){
$erro = 1;
document.getElementById("email-input").style.borderColor = "red";
}
...
if($erro == 0){
//run ajax
}
You can put a bit more html code to make a hidden textbox appear using.
if(document.getElementById("email-input").value == null or document.getElementById("email-input").value == ""){
$erro = 1;
document.getElementById("email-input").style.borderColor = "red";
document.getElementById("id_erro1").style.visibility = "visible";
}
create in your html:
<fieldset>
<input placeholder="Your Email Address" type="email" name="email" id="email-input" required value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''; ?>">
<input type="hidden" name="error_mensage1" id="id_erro1" value="Required field" >
</fieldset>
Use css to Spice up.
I would like to have a contact form on my that requires the *fullname, *email address, and *subject and *message. I followed a tutorial to develop my form but for some reason it is not sending my test message.
I'm not experienced enough with PHP to figure out what I am doing wrong, so I am hoping to get suggestions on how to resolve this issue. All questions, suggestions, and possible solutions are welcome. Thanks.
contact form php:
Code:
<?php
// EDIT THE FOLLOWING LINE BELOW AS REQUIRED
$send_email_to = "jb#me.com";
function send_email($name,$email,$email_subject,$email_message)
{
global $send_email_to;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ".$email. "\r\n";
$message = "<strong>Email = </strong>".$email."<br>";
$message .= "<strong>Name = </strong>".$name."<br>";
$message .= "<strong>Message = </strong>".$email_message."<br>";
#mail($send_email_to, $email_subject, $message,$headers);
return true;
}
function validate($name,$email,$message,$subject)
{
$return_array = array();
$return_array['success'] = '1';
$return_array['name_msg'] = '';
$return_array['email_msg'] = '';
$return_array['message_msg'] = '';
$return_array['subject'] = '';
if($email == '')
{
$return_array['success'] = '0';
$return_array['email_msg'] = 'email is required';
}
else
{
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$return_array['success'] = '0';
$return_array['email_msg'] = 'enter valid email.';
}
}
if($name == '')
{
$return_array['success'] = '0';
$return_array['name_msg'] = 'name is required';
}
else
{
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $name)) {
$return_array['success'] = '0';
$return_array['name_msg'] = 'enter valid name.';
}
}
if($subject == '')
{
$return_array['success'] = '0';
$return_array['subject_msg'] = 'subject is required';
}
if($message == '')
{
$return_array['success'] = '0';
$return_array['message_msg'] = 'message is required';
}
else
{
if (strlen($message) < 2) {
$return_array['success'] = '0';
$return_array['message_msg'] = 'enter valid message.';
}
}
return $return_array;
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$return_array = validate($name,$email,$message,$subject);
if($return_array['success'] == '1')
{
send_email($name,$email,$subject,$message);
}
header('Content-type: text/json');
echo json_encode($return_array);
die();
?>
Contact Form HTML:
Code:
<fieldset id="contact_form">
<div id="msgs"> </div>
<form id="cform" name="cform" method="post" action="">
<input type="text" id="name" name="name" value="Full Name*" onfocus="if(this.value == 'Full Name*') this.value = ''"
onblur="if(this.value == '') this.value = 'Full Name*'" />
<input type="text" id="email" name="email" value="Email Address*" onfocus="if(this.value == 'Email Address*') this.value = ''"
onblur="if(this.value == '') this.value = 'Email Address*'" />
<input type="text" id="subject" name="subject" value="Subject*" onfocus="if(this.value == 'Subject*') this.value = ''"
onblur="if(this.value == '') this.value = 'Subject*'" />
<textarea id="msg" name="msg" onfocus="if(this.value == 'Your Message*') this.value = ''"
onblur="if(this.value == '') this.value = 'Your Message*'">Your Message*</textarea>
<button id="submit" class="button"> Send Message</button>
</form>
</fieldset>
Your form's action isn't set to anything. You'll need to point it at the script you have there that sends the email.
Ie:
<form id.. name.. method.. action="/handle_post.php">
Set the correct action and then try it should work..
if it still does not work test it with curl utility to see if ur script is fine..
One more mistake that i saw was that ur text area form name is msg while on server ur expecting ur post request it have message. That wont work..
If u still face problem then we wd debug further :)
First you need to set action to some script that will handle $_POST inputs (contactform.php) or $_SERVER['PHP_SELF'] for the same file.
Second you should send data by input type=submit not button.
In the following code the email sanitizing and validation:
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}
is allowing:
ck#////bushidodee/xom
New to filters, and don't get why this is not sanitized?
Full Code:
if (isset($_POST['Submit'])) {
if ($_POST['name'] != "") {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Please enter a valid name.<br/><br/>';
}
} else {
$errors .= 'Please enter your name.<br/>';
}
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}
if ($_POST['homepage'] != "") {
$homepage = filter_var($_POST['homepage'], FILTER_SANITIZE_URL);
if (!filter_var($homepage, FILTER_VALIDATE_URL)) {
$errors .= "$homepage is <strong>NOT</strong> a valid URL.<br/><br/>";
}
} else {
$errors .= 'Please enter your home page.<br/>';
}
if ($_POST['message'] != "") {
$_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
if ($_POST['message'] == "") {
$errors .= 'Please enter a message to send.<br/>';
}
} else {
$errors .= 'Please enter a message to send.<br/>';
}
if (!$errors) {
// $mail_to = 'me#somewhere.com';
// $subject = 'New Mail from Form Submission';
// $message = 'From: ' . $_POST['name'] . "\n";
// $message .= 'Email: ' . $_POST['email'] . "\n";
// $message .= 'Homepage: ' . $_POST['homepage'] . "\n";
// $message .= "Message:\n" . $_POST['message'] . "\n\n";
// mail($to, $subject, $message);
print_r($_POST);
echo "Thank you for your email!<br/><br/>";
} else {
echo '<div style="color: red">' . $errors . '<br/></div>';
}
}
?>
<form name="form1" method="post" action="form-email.php">
Name: <br/>
<input type="text" name="name" value="<?php echo $_POST['name']; ?>" size="50" /><br/><br/>
Email Address: <br/>
<input type="text" name="email" value="<?php echo $_POST['email']; ?>" size="50"/> <br/><br/>
Home Page: <br/>
<input type="text" name="homepage" value="<?php echo $_POST['homepage']; ?>" size="50" /> <br/><br/>
Message: <br/>
<textarea name="message" rows="5" cols="50"><?php echo $_POST['message']; ?></textarea>
<br/>
<input type="submit" name="Submit" value="Submit Form Data" />
</form>
Hm, I do not get your problem, you first sanitize the input from $_POST and store it in $email if you print that var you will see it is ck#bushidodeexom and then you validate the sanitized input -- of course it passes.
Try this...
<?php
if ($_POST['email'] != ""){
$_POST['email'] = stripslashes(trim($_POST['email']));
$tmpEmail=filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if ( filter_var($tmpEmail, FILTER_VALIDATE_EMAIL) == TRUE) {
}
else{
$errors .= "Invalid Email";
}
}
else{
$errors .= "Please enter email";
}
?>