creating mail form in html and php - php

I am currently having issues getting my php and html working correctly. When I click 'submit' It goes to an error page saying it cannot find webpage I requested (www.website.com/contact-form-handler.php).
Here is the PHP and HTML
HTML:
<html>
<head></head
<body>
<div>
<form action="contact-form-handler.php" method="post" name="contact_form">
<div class="col-md-6 col-sm-6 form-group">
<input name="name" type="text" class="form-control" id="nameInput" placeholder="Name" required>
<input name="email" type="email" class="form-control" id="emailInput" placeholder="Email" required>
<input name="phone" type="text" class="form-control" id="phoneInput" placeholder="Phone" required>
<input name="location" type="text" class="form-control" id="locationInput" placeholder="Event Location" required>
<select name="eventType" type="select" class="form-control" id="eventTypeInput">
<option selected>Type of Event</option>
<option value="1">Business</option>
<option value="2">Party</option>
<option value="3">Speech</option>
</select>
<select name="reference" type="select" class="form-control" id="referenceInput">
<option selected>How did you hear about us?</option>
<option value="1">Social Media</option>
<option value="2">Word of Mouth</option>
<option value="3">Google Search</option>
</select>
</div>
<div class="col-md-6 col-sm-6 form-group">
<textarea name="message" rows="7" class="form-control" id="messageInput" placeholder="Message"></textarea>
</div>
<div class="col-md-offset-4 col-md-4 col-sm-offset-3 col-sm-6 font3">
<input name="submit" type="submit" class="form-control" id="submit" value="Submit">
</div>
</form>
</div>
</body>
</html>
PHP:
<?php
$errors = '';
$emails = 'myemail#myemail.com';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['location']) ||
{
$errors .= "\n Error: Please fill out required fields";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$location = $_POST['location'];
$eventType = $_POST['eventType'];
$reference = $_POST['reference'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $emails;
$subject = "David Does Stuff Inquiry: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Phone: $phone \n Location of event: $location \n Type of Event: $eventType \n How did you hear about us: $reference \n Message \n $message";
$headers = "From: $email_address\n";
$headers .= "Reply-To: $emails";
mail($to,$subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>

See if your PHP file name is "contact-form-handler.php" or maybe you're getting the error coz' your PHP file might not be in the same directory.

Please check your form
<form action="contact-form-handler.php" method="post" name="contact_form">
your header location is incorrect, it should be
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.php');

Related

Contact form works in safari (desktop) but nothing else

I have a basic contact form that sends to my gmail account. This form works in Safari on my Mac but its does not work in Chrome or Opera, nor does it work on my iPhone.
The website is whataprettyface.ca/whataprettyface_new/index.html
I can get the form working in all browsers and devices when the success message is a link to a separate thank-you.html page. But I am looking for it to work the way it does currently in safari with the overlay success message.
HTML:
<form method="POST" class="ajax-form" data-message-class="colors-d background-95 heading border" action="contact.php">
<div class="row">
<div class="col-md-6 control-group">
<div class="alt-placeholder">Name</div>
<input type="text" name="name" value="" size="40" placeholder="Name" data-validation-required-message="Please fill the required field." required>
<div class="help-block"></div>
</div>
<div class="col-md-6 control-group">
<div class="alt-placeholder">Email</div>
<input type="email" name="email" value="" size="40" placeholder="Email" data-validation-required-message="Please fill the required field." required>
<div class="help-block"></div>
</div>
<div class="col-md-12 control-group">
<div class="alt-placeholder">Message</div>
<textarea name="message" placeholder="Message" data-validation-required-message="Please fill the required field." required></textarea>
<div class="help-block"></div>
</div>
<div class="col-md-12 form-actions">
<input class="button" type="submit" value="Submit">
</div>
</div>
</form>
PHP:
<?php
$errors = '';
$myemail = 'my.email#gmail.com';//
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$successMessage = 'Message sent successfully!';
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "CONTACT VIA WHATAPRETTYFACE $name";
$email_body = "\n Name: $name \n Email: $email_address \n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
echo($successMessage);
}
I also have gen_validatorv31.js as a linked script.
So thanks to some the comments to the original post I was able to correct the issue by by removing the word 'contact' from my html. It now says 'kontact.php' and that spelling was reflected in the name of the php file itself.

When Clicking Submit My Browser goes to domain.com/contactform.php insted of running php

I hosted my website through Hostinger. I inserted a PHP form on my website to send an email using my mail id. But when I am testing this feature on domainname.com/contactform.php, system show blank screen instead of running the code. I tried to solve my issue but I can not find my mistake. Please Help!
<div class="col-md-6 padding">
<!-- banner form -->
<form action="contactform.php" method="post">
<h5 class="mb-3">Register Here</h5>
<div class="form-style-w3ls">
<input placeholder="Your Name" name="name" type="text" required="">
<input placeholder="Your Email Id" name="email" type="email" required="">
<input placeholder="Contact Number" name="number" type="text" required="">
<select>
<option value="0">Business Type</option>
<option value="1">Corporate</option>
<option value="1">Partnership</option>
<option value="1">Other</option>
</select>
<!--<input placeholder="Password" name="password" type="password" required=""> -->
<button Class="btn" name="submit"> Submit</button>
<span>By Registering, You agree to Our Terms & Conditions.</span>
</div>
</form>
PHP Form
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$MailFrom = $_POST['email'];
$contactnumber = $_POST['number'];
$Businesstype = $_POST['type'];
$mailTo = "care#thecraftedu.com";
$headers = "From: ".$MailFrom;
$txt = "You have received an Email from ".$name.".\n\n".$contactnumber;
mail($mailTo, $txt, $headers);
header("Location: index.php?mailsend");
}
?>

Trouble receiving mail from application form from my website

I have added a training application form in my website. Using Modal box. This is the code
<button type="submit" onclick="document.getElementById('id01').style.display='block'" style="width:auto;">Apply Now</button>
<div id="id01" class="modal">
<span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">×</span>
<form method="POST" enctype="multipart/form-data" class="modal-content" action="sendemailcontact.php">
<div class="container">
<h1>Application Form</h1>
<p>Please fill in this form to apply for the training.</p>
<hr>
<label for="name"><b>Name</b></label><br>
<input type="text" placeholder="Enter name" name="name" id="name" required><br>
<label for="email"><b>Email</b></label><br>
<input type="text" placeholder="Enter Email" name="email" id="email" required><br>
<label for="phone"><b>Phone number</b></label><br>
<input type="text" placeholder="Enter phone number" name="phone" id="phone" required><br>
<label for="location"><b>Location</b></label><br>
<input type="text" placeholder="Enter location" name="location" id="location" required><br>
<label for="college"><b>College</b></label><br>
<input type="text" placeholder="Enter college" name="college" id="college" required><br>
<!--<label for="uploaded_file"><b>Upload resume</b></label>
<input class="upl" type="file" name="uploaded_file" id="uploaded_file" required>-->
<div class="clearfix">
<input style="width:30%;" type="submit" name="apply" id="apply" class="signupbtn" value="Apply">
</div>
</div>
</form>
</div>
This form to my php code that send mail to my email address which the details received from each applicant from the above form. This is my php code
<?php
//error_reporting(-1);
//ini_set('display_errors', 'On');
//set_error_handler("var_dump");
$errors = '';
$myemail = 'example#gmail.com';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['location']) ||
empty($_POST['college']))
{
$errors .= "\n Error: all fields are required";
}
$email_address = $_POST['email'];
$name = $_POST['name'];
$phno = $_POST['phone'];
$location = $_POST['location'];
$college = $_POST['college'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Career Oriented Training Form submission: $name";
$email_body = "You have received a new applicant. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Phone: $phno \n Location: $location \n College: $college";
$headers = "From: $email_address\n";
$headers .= "Reply-To: $myemail";
mail($to,$email_subject,$email_body,$headers);
header('Location:index.html');
}
?>
When i click submit, its going to a blank page. Also i cannot receive any mail. Please help me
You need to have it echo back $errors as that will tell you why it's failing. You're stocking $errors and ignoring it other than checking that it's empty.
Also do not use regex to validate email addresses. See http://php.net/manual/en/filter.filters.validate.php specifically FILTER_VALIDATE_EMAIL

After adding recaptcha code my contact form doesn't redirect

Trying to add recaptcha to my contact form. I cannot get the contact form to redirect to a thank you page. The form does send the email correctly.
<?php
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Please check the the captcha form.</h2>';
exit;
}
$secretKey = "my key";
$ip = $_SERVER['REMOTE_ADDR'];
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
echo 'You are spammer';
} else {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$phone = $_POST['phone'];
$formcontent="From: $name \n Message: $message \n Email:$email \n Phone:$phone";
$recipient = "example#example.com";
$subject = "Website Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location:https://www.example.com/thank-you.html');
exit();
}
?>
Form Code: Adding site form code as requested
<div class="col-md-4">
<form id="quote-form" action="inc/sendmail.php" method="post">
<div class="sec-title text-center">
<h1>Request for Quote</h1>
<span class="border center"></span>
</div>
<div class="row">
<div class="col-md-12">
<input type="text" name="name" value="" placeholder="Your Name*" required="">
<input type="email" name="email" value="" placeholder="Your Mail*" required="">
<input type="text" name="phone" value="" placeholder="Your Phone*" required="">
<select class="selectmenu" name=message>
<option selected="selected">Select Service</option>
<option value="Need Quote on a prod 1</option>
<option value="Need Quote on a prod 2</option>
<option>Other</option>
</select>
<div class="g-recaptcha" data-sitekey="sitekeygoeshere-removed-forposting"></div>
<button class="thm-btn bg-clr1" name="submit" type="submit">Get a Quote</button>
</div>
</div>
</form>
</div>
I've removed the site key in the code.

php form to email only sending one variable

I am using this code for my form but it only sends one variable which in this case is "type". What seems to be my mistake?
php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$type = $_POST['type'];
$message = $_POST['message'];
$formcontent=" From: $name \n subject: $subject \n email: $email \n Type: $type \n Message: $message";
$recipient = "ashele#diloptic.co.za";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "We will call you as soon as possible!" . " -" . "<a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
this is the corresponding html5 code
<form action="mail.php" method="post" role="form" class="contactForm">
<div class="s-12 l-7"><input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" /></div>
<div class="validation"></div>
<div class="s-12 l-7"><input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" /></div>
<div class="validation"></div>
<div class="s-12"><input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" data-rule="minlen:4" data-msg="Please enter at cell number plus code" /></div>
<div class="validation"></div>
<div class="s-12 l-7"><p placeholder="Subject" data-rule="minlen:4">type
<select name="type" size="1">
<option value="update">Website Update</option>
<option value="change">Information Change</option>
<option value="addition">Information Addition</option>
<option value="new">New Products</option>
</select></p></div>
<div class="validation"></div>
<div class="s-12 l-7"><textarea class="form-control" name="message" id="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea><br/></div>
<div class="validation"></div>
<div class="s-12 m-6 l-4"><button type="submit" name="submit" value="submit" class="btn btn-primary btn-lg" required="required">Submit Message</button></div>
</form>
Try printing out all the post array by using this:
print_r($_POST);
I tried your html form and received all parameters properly.
There you go!
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$type = $_POST['type'];
$message = $_POST['message'];
$formcontent = " From: $name \n subject: $subject \n email: $email \n Type: $type \n Message: $message";
$recipient = "ashele#diloptic.co.za";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if (!mail($recipient, $subject, $formcontent, $mail_header)) {
echo "error";
} else {
echo "We will call you as soon as possible!" . " -" . "<a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
print_r($_POST);
}
}
?>
Tested and works fine.
Example can be found here!
Try it out and let me know the results. Always happy to help!

Categories