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
Related
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.
So the PHP contact form for my website doesn't work. No idea why. Tried many different templates. Only one worked for a while, before I tried to insert captcha there, and then it also started making 500 error.
No idea how PhP works from the word du tout - I just need a simple form for my personal website to have a captcha and that's all I ever want from PHP at this stage.
Below is the code that doesn't work (supposedly it should, as I have done it from this tutorial https://www.youtube.com/watch?v=4cr85kvM8I0
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$subject = $_POST['subject'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$email_from = 'contact#mywebsite.com';
$email_subject = 'New email that you have received';
$email_body = "Name: $name.\n".
"Subject: $subject.\n".
"Email: $email_address.\n".
"Message: $message.\n";
$to_email = "contact#mywebsite.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-to: $email_address\r\n";
//your site secret key
$secretKey = 'mykey';
$responseKey = $_POST['g-captcha-response'];
$UserIP = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$UserIP";
$response = file_get_contents($url);
$response = json_decode($response);
if ($response-> success)
{
mailto($to_email,$email_subject,$email_body,$headers);
echo "Message Sent Successfully";
}
else {
echo "Invalid Captcha, try again";
}
}
?>
and this is the contact form (part of it on the html)
Head code:
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
...
<div class="container">
<h3>Contact Form</h3>
<form action="contact-form-handler.php" method="POST"
enctype="multipart/form-data" name="contact_form">
<div class="nameemail">
<label for="name">Name</label>
<input name="name" type="text" required placeholder="Name"/>
<label for="subject">Subject</label>
<input name="subject" type="text" required placeholder="Subject"/>
<label for="email">Email</label>
<input name="email" type="email" required placeholder="you#domain.com"/>
</div>
<br>
<label for="message">Message</label><br>
<textarea name="message" cols="30" rows="10" placeholder="Enter your message here ..." required> </textarea>
<div class="center">
<div class="g-recaptcha" data-sitekey="mykey"></div>
<input type="submit" value="Submit">
</div>
</form>
</div>
So why in the mother of God this doesn't work?
I have a php simple project.
The project is to create an email submission page.
I use phpstorm software.
<?php
//get user input
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
//error message
$missingName = '<p><strong>Please enter your name</strong></p>';
$missingEmail = '<p><strong>Please enter your email address</strong></p>';
$invalidEmail = '<p><strong>Please enter a valid email address</strong></p>';
$missingMessage = '<p><strong>Please enter a message</strong></p>';
//if the user has submitted the form
if ($_POST["submit"]){
//check for errors
if (!$name){
$errors .= $missingName;
}else{
filter_var($name, FILTER_SANITIZE_STRING);
}
if (!$email){
$errors .= $missingEmail;
}else{
filter_var($email, FILTER_SANITIZE_EMAIL);
if (!filter_var($invalidEmail, FILTER_VALIDATE_EMAIL)){
$errors .= $invalidEmail;
}
}
if (!$message){
$errors .= $missingMessage;
}else{
$message = filter_var($message, FILTER_SANITIZE_STRING);
}
//if there are any errors
if ($errors){
$resultMessage = '<div class="alert alert-danger">' . $errors . '</div>';
}
}
?>
<form action="index.php" method="post">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" name="name" placeholder="Name" id="name" class="form-control">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="text" name="email" placeholder="Email" id="email" class="form-control">
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea name="message" id="message" class="form-control" rows="5"></textarea>
</div>
<input type="submit" name="submit" id="submit" class="btn btn-success btn-lg" value="Send Message">
</form>
When I run the project, I encounter the errors you see in the image
error messages
Errors are from the following lines
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
And this line
if ($_POST["submit"])
please help!
Before accessing POST variable you should check if its not empty:
if(isset($_POST['submit']){
//your code
}
My php webform was working until the point I added an if(isset) and also a reCaptcha.
Help?
Tried to debug the code - could not find an issue
html
<form class = "form-inline d-flex justify-content-center" action="contact.php" method = "post">
<div class="mc-field-group-1">
<input name="name" type="text" placeholder="Name" id="mce-LNAME"/>
</div>
<div class="mc-field-group-2">
<input name = "email" type="text" placeholder="Email address*" class="required email" id="mce-EMAIL" required/>
</div>
<div class="mc-field-group-3">
<input name = "subject" type="text" placeholder="Subject" class="required email" id="mce-EMAIL"/>
</div>
<div class="mc-field-group-4">
<textarea name="message" rows="10" cols="39" placeholder="Your message"></textarea>
</div>
<div class="g-recaptcha" data-sitekey="my_key(have the real key here in my code)"></div>
<div class="clear">
<input type="submit" name="submit" id="mc-embedded-subscribe" class="button" value="Send">
</div>
</form>
php
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "realemail#gmail.com";
$mailheader = "From: $email \r\n";
$secretKey = "my_key(have the real key here in my code)";
$responseKey = $_POST['g-recaptcha-response'];
$UserIP = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$UserIP";
$response = file_get_contents($url);
$response = json_decode($response);
if ($response ->success)
{
mail($recipient, $subject, $formcontent, $mailheader);
echo "Thank You!";
}
else
{
echo "Invalid Captcha, Please Try Again";
}
}
?>
When I click 'Send' in the form takes me to the contact.php page but email not sent to my gmail inbox. Has nothing to do with gmail as I said was working before I tweaked the code to add the reCaptcha.
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');