Problems with PHP form validation - php

My form is spread across two pages. Pages are turned using the JS slider called Slick. After filling in the form and clicking the send button on page 2, the validation says I didn't fill in my name. You can see this for yourself on this website: Rofordaward. Just complete the nomination form and push send. HTML and PHP code below.
HTML
SNIPPET OF FORM (from nominate.html):
<form action="nominate.php" method="post">
<div class="single-item slider">
<div class="page1">
<label class="row">
<h2 class="headline">Your full name</h2>
<input type="text" name="yourname" placeholder="Forename and surname"></input>
</label>
<label class="row email">
<h2 class="headline">Your email address <p>Don't worry, we won't spam you or share your email address</p></h2>
<input type="text" name="email" placeholder="example#rofordaward.co.uk"></input>
</label>
<label class="row">
<h2 class="headline">Name of company</h2>
<input type="text" name="companyname" placeholder="e.g. Roford"></input>
</label>
</div>
<div class="page2">
<label class="row reason">
<h2 class="headline">Reason for nomination</h2>
<textarea id="textarea" rows="6" cols="25" maxlength="1000" name="reason" placeholder="A brief evidence based summary"></textarea>
<div id="text-area-wrap">
<div id="textarea_feedback"></div>
</div>
</label>
<div class="row button-wrap">
<div class="column small-12">
<input class="button" type="submit" value="Send it!">
</div>
</div>
</div>
</div>
</form>
PHP
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$email = check_input($_POST['email']);
$companyname = check_input($_POST['companyname']);
$reason = check_input($_POST['reason'], "Write your reason");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/*Message for the e-mail */
$message = "New submission
Name: $yourname
E-mail: $email
Company name: $companyname
Reason:
$reason
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();
/* Functions used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
When I remove the scripts for the JS slider, the form becomes fully functional.

In your PHP Code, You are using $_POST['email'] which suits good according to code on your last HTML Block when <input type="text" name="email" /> as you have set it to 'email'
On other hand, in your First HTML Code Block:
<label class="row email">
....
<input type="text" name="youremail" ....></input>
</label>
You set name="youremail", which is definitely going to derive wrong result with your PHP Code. You should be using it like $_POST['youremail'] in your PHP Code to get it working.

Related

Emailing a form with PHP and HTML error message "Cannot POST mail.php"

Second time I've posted about this website I'm coding but I'm making good progress! I'm trying to set up a form that when the user fills out will send to my email - not too difficult right? Well when I eventually hit the submit button, I get the error:
Cannot POST /mail.php
This is the HTML for the form that I've got:
<form method="post" action="mail.php">
<div class="field half first">
<label for="name">Name</label>
<input type="text" name="name" id="name" />
</div>
<div class="field half">
<label for="email">Email</label>
<input type="text" name="email" id="email" />
</div>
<div class="field">
<label for="message">Message</label>
<textarea name="message" id="message" rows="5"></textarea>
</div>
<ul class="actions">
<li>Send Message</li>
</ul>
</form>
And this is my mail.php file
<?php
$myemail = "MYEMAIL";
$name = check_input($_POST['name'], "Enter your name");
$email = check_input($_POST['email']);
$message = check_input($_POST['message'], "Write your message");
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
$message = "
Name: $name
E-mail: $email
Subject: $subject
Message:
$message
";
mail($myemail, $subject, $message);
header('Location: thankyou.html');
exit();
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
Both files are in the same directory so I have no idea why this isn't working :( Thanks guys <3
Edit: This is what the form looks like
Edit 2: Changed button to:
<li><input type="submit" value="Send Message" class="button submit" /></li>
but still getting same problem :/
Edit 3: Can also confirm that
<li><button type="submit" value="Send Message" class="button submit" /></li>
doesn't make a difference :((
You'll need to use a button for that
<button class="yourclasshere" type="submit" name="sendMail">Send Mail</button>
In your PHP you'll need to add a line of code that checks to see if the form is being submitted. This can be accomplished with the following:
if(isset($_POST['sendMail'])) {
//validate your inputs and submit the form here
}
What this does is checks to see if you've clicked the submit button (in this example you're going by the name of the button which I've named 'sendMail') and if you have, you can set the values of your other variables (name, email, message) using your existing function.
From there if everything is good, you should be able to send your mail.

How do I get my php form to display a succes notice?

This is my first post here. I'm really hoping you can help me out!
I just implemented a bootstrap contact form on my website. The form is set to redirect to another website if the mail is sent successfully. I really want it to just show a notice saying "E-mail sent" or something like that.
I do realize that
header('Location: http://address-of-confirmation-page.html');
exit();
is what needs to be changed, i just don't know how to change it to the success message
Here's my code:
The PHP
/* Set e-mail recipient */
$myemail = "your-email#gmail.com";
/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "Your Name");
$email = check_input($_POST['inputEmail'], "Your E-mail Address");
$subject = check_input($_POST['inputSubject'], "Message Subject");
$message = check_input($_POST['inputMessage'], "Your Message");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */
$subject = "Someone has sent you a message";
$message = "
Someone has sent you a message using your contac form:
Name: $name
Email: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: http://address-of-confirmation-page.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
The HTML
<div class="container">
<div class="panel panel-default" style="margin:0 auto;width:500px">
<div class="panel-heading">
<h2 class="panel-title">Contact Form</h2>
</div>
<div class="panel-body">
<form name="contactform" method="post" action="php/contact.php" class="form-horizontal" role="form">
<div class="form-group">
<label for="inputName" class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputName" name="inputName" placeholder="Your Name">
</div>
</div>
<div class="form-group">
<label for="inputEmail1" class="col-lg-2 control-label">Email</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputEmail" name="inputEmail" placeholder="Your Email">
</div>
</div>
<div class="form-group">
<label for="inputSubject" class="col-lg-2 control-label">Subject</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputSubject" name="inputSubject" placeholder="Subject Message">
</div>
</div>
<div class="form-group">
<label for="inputPassword1" class="col-lg-2 control-label">Message</label>
<div class="col-lg-10">
<textarea class="form-control" rows="4" id="inputMessage" name="inputMessage" placeholder="Your message..."></textarea>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" class="btn btn-default">
Send Message
</button>
</div>
</div>
</form>
</div>
</div>
</div>
Bootstrap already has success messages like this one, which would be nice to have:
<div class="bs-example">
<div class="alert alert-success fade in">
×
<strong>Success!</strong> Your message has been sent successfully.
</div>
</div>
Thanks in advance!
I suppose you can use
header('Location: http://current-page.php?success');
And put this somewhere suitable
if(isset($_GET['success'])){
//echo the success message
}
I guess this is kinda "hacky" but it works.
I would also recommend jQuery to submit the form to an external page and have it return a success and have jQuery return a success flag and then dynamically show the success message on the page. You can do some research on that, rather than using PHP to do this.
Why not just set the "http://address-of-confirmation-page.html" page to a simple thank you page? This wouldn't involve any clever scripting. Its basic, but it fits with your script!
so change :
/* Redirect visitor to the thank you page */
header('Location: http://address-of-confirmation-page.html');
exit();
to
/* Redirect visitor to the thank you page */
header('Location: http://yourwebsite.com/thank_you.php');
exit();
and thank_you.php would be :
<div class="bs-example">
<div class="alert alert-success fade in">
×
<strong>Success!</strong> Your message has been sent successfully.
</div>
</div>
Does that answer your question, or is that too simplistic of a solution?
The best way to do it, and it's not so hard (and easy to maintain) is using Ajax like the other suggested.
Javascript/jQuery
First add an id to the form element, let's say #contact-form, then let's send the form via ajax:
$("#contact-form").submit(function(e) {
$.ajax({
type: "POST",
url: php/contact.php,
data: $("#contact-form").serialize(),
success: function(data){
$('#message')
.addClass('alert-'+ data.status)
.append(data.message);
}
);
e.preventDefault();
});
HTML
Hide the message by default, and let's show the correct message depending of the p
<div id="message" class="alert" style="display: none;">
×
</div>
<!-- Your contact form here -->
PHP
That's your homework, the script should be waiting a response in json, something like:
$result = array(
'status' => // success | error ,
'message' => // 'Success Message' | 'validation message'
);
echo json_encode($result);

Code or Server issue - PHP contact form?

I'm redesigning a site, and having just uploaded the files to the host, am now testing that everything works. The message form is not working consistently - sometimes it sends the user to the thank you confirmation page and sometimes it displays the error: "500 Internal Server Error, The server encountered an internal error or misconfiguration and was unable to complete your request." However, no test email has arrived at the recipient address showing that the form has worked. Strangely, it successfully went through when I substituted my personal email address in the mailer.php document. This makes me think that the code works.
I'm suspecting that this is a server or email configuration issue, because I've also been having issues with email, after changing hosts, but possibly it has to do with my code?
HTML:
<form class="form-horizontal" method="post" action="mailer.php" class="form-horizontal" role="form">
<div class="form-group">
<label for="name" class="col-sm-2"><small>Name</small></label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputName" name="inputName" placeholder="Enter your full name" required>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2"><small>Email</small></label>
<div class="col-sm-10">
<input type="email" class="form-control" id="inputEmail" name="inputEmail" placeholder="Enter your email, example#domain.com" required>
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2"><small>Telephone</small></label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputPhone" name="inputPhone" placeholder="Enter your telephone number" required>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2"><small>Message</small></label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="inputMessage" id="inputMessage" placeholder="Enter your message here" required></textarea>
</div>
</div>
<div class="form-group">
<!-- 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" name="robotest" id="robotest" class="robotest" />
</p>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input style="font-size:22px;" id="submit" name="submit" type="submit" value="Send" class="btn">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<! Will be used to display an alert to the user>
</div>
</div>
</form>
mailer.php file:
<?php
/* Set e-mail recipient */
$myemail = "info#domain.com";
/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "Your name was not entered correctly.");
$email = check_input($_POST['inputEmail'], "Your email address was not entered correctly.");
$phone = check_input($_POST['inputPhone'], "Your telephone number was not entered correctly.");
$message = check_input($_POST['inputMessage'], "Your message was not entered correctly.");
$robotest = $_POST['robotest'];
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address.");
}
/* ROBOT TEST */
if($robotest)
{
show_error("Denied, robot.");
}
/* prepare the message for the e-mail */
$subject = "Inquiry from Website";
$message = "
Someone has sent you a message using your website's contact form:
Name: $name
Email: $email
Telephone: $phone
Subject: $subject
Message:
$message
";
/* send the message using mail() function */
mail($myemail, $subject, $message);
/* redirect visitor to the thank you page */
header('Location: http://www.website.com/thankyou.html');
exit();
/* functions used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again.</p>
</body>
</html>
<?php
exit();
}
?>
It appears you have a server/configuration problem. I've used this code and put it on my own server and it works perfectly fine. (also, nice work, very clean code)
One thing to think about when checking whether you get emails or not (specifically when a personal email works, but a corporate one does not) is email heuristics. Try changing the content setup and/or the subject line. (the subject line looks suspcious to me, I'd start there).
https://en.wikipedia.org/wiki/Naive_Bayes_spam_filtering

Php mailer script not working on Apache hosted Bootstrap website

I am creating a simple Django/Bootstrap website hosted on a VM running apache.
The path to my website is var/www/experiment/
I have followed this excellent tutorial on creating a php mailer contact form. I was having an issue with php not being installed on the account, but running sudo apt-get install libapache2-mod-php5 has solved this.
I also tried a second script given to me by a friend who has used it on several websites.
My problem is that the contact form emails are not being sent. I have checked it with 2 different email accounts and I have checked the spam folders in case. As I said, I have also checked two separate scripts.
What else could be wrong or needs to be changed/configured at my end?
My mailer.php file is located in var/www/experiment/php is this the right location?
Would anyone have a link to a better tutorial on using php mailer and apache?
EDIT: added code
Bootstrap - HTML
<form name="contactform" method="post" action="http://location_of_my_web_app/php/mailer.php"class="form-horizontal" role="form">
<div class="form-group">
<label for="inputName" class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputName" name="inputName" placeholder="Your Name">
</div>
</div>
<div class="form-group">
<label for="inputEmail1" class="col-lg-2 control-label">Email</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputEmail" name="inputEmail" placeholder="Your Email">
</div>
</div>
<div class="form-group">
<label for="inputSubject" class="col-lg-2 control-label">Subject</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputSubject" name="inputSubject" placeholder="Subject Message">
</div>
</div>
<div class="form-group">
<label for="inputPassword1" class="col-lg-2 control-label">Message</label>
<div class="col-lg-10">
<textarea class="form-control" rows="4" id="inputMessage" name="inputMessage" placeholder="Your message..."></textarea>
</div>
</div>
<div class="form-group">
<center>
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" class="btn btn-primary">Send Message</button>
</div>
</center>
</div>
</form>
PHP
<?php
/* Set e-mail recipient */
$myemail = "my_address#gmail.com";
/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "Your Name");
$email = check_input($_POST['inputEmail'], "Your E-mail Address");
$subject = check_input($_POST['inputSubject'], "Message Subject");
$message = check_input($_POST['inputMessage'], "Your Message");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */
$subject = "Someone has sent you a message";
$message = "
Someone has sent you a message using your contac form:
Name: $name
Email: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: http://location_of_my_thank_you_page/Static/contact_thankyou.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
Are you using PHP's mail() function, or connecting to a server and sending mail via SMTP? Whatever the case, the official PHPMailer website has a variety of examples. Do note that if you want to test sending mail via SMTP, you can always configure a gmail account and mess around with it.
https://github.com/PHPMailer/PHPMailer/tree/master/examples

PHP Mailer. Clicking send downloads the php script but does not send mail

I am following a tutorial on how to implement a simple Bootstrap contact form with a PhP mailer script.
However when I click Send Message, my contact form downloads the PHP script rather than sending the email. What am I doing wrong?
Bootstrap - HTML
<form name="contactform" method="post" action="http://location_of_my_web_app/php/mailer.php" class="form-horizontal" role="form">
<div class="form-group">
<label for="inputName" class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputName" name="inputName" placeholder="Your Name">
</div>
</div>
<div class="form-group">
<label for="inputEmail1" class="col-lg-2 control-label">Email</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputEmail" name="inputEmail" placeholder="Your Email">
</div>
</div>
<div class="form-group">
<label for="inputSubject" class="col-lg-2 control-label">Subject</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputSubject" name="inputSubject" placeholder="Subject Message">
</div>
</div>
<div class="form-group">
<label for="inputPassword1" class="col-lg-2 control-label">Message</label>
<div class="col-lg-10">
<textarea class="form-control" rows="4" id="inputMessage" name="inputMessage" placeholder="Your message..."></textarea>
</div>
</div>
<div class="form-group">
<center>
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" class="btn btn-primary">Send Message</button>
</div>
</center>
</div>
</form>
PHP
<?php
/* Set e-mail recipient */
$myemail = "my_address#gmail.com";
/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "Your Name");
$email = check_input($_POST['inputEmail'], "Your E-mail Address");
$subject = check_input($_POST['inputSubject'], "Message Subject");
$message = check_input($_POST['inputMessage'], "Your Message");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */
$subject = "Someone has sent you a message";
$message = "
Someone has sent you a message using your contac form:
Name: $name
Email: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: http://location_of_my_thank_you_page/Static/contact_thankyou.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
"my contact form downloads the PHP script rather than sending the email"
It sounds like PHP is either not installed/running or not properly configured.
I suggest that you create a file called test.php file with <?php echo "Hello world"; ?> inside it and see if it does the same thing.
If it still wants to appear like it wants to download, then there's the problem; PHP is not installed or not properly configured and isn't parsing PHP as it normally should.
Create another file and place <?php phpinfo(); ?> and it should show you the server's information.

Categories