Change a field in PHP form from required to optional - php

I am a complete novice when it comes to PHP. I downloaded a form I found online to include in my Bootstrap site. It is a very simple form anyone can use to send me a message. I managed to set up Wamp to test out the PHP but when I leave the Phone field blank it gives me an error message telling me please go back and correct the error. I want to make it so if someone leaves out the phone number it still sends the email. Thank you.
HTML
<form name="contactform" method="post" action="index.php" class="form-vertical">
<div class="form-group">
<label for="inputName" class="control-label">Name</label>
<input type="text" class="form-control" id="inputName" name="inputName" placeholder="First and Last">
</div>
<div class="form-group">
<label for="inputEmail" class="control-label">Email*</label>
<input type="text" class="form-control" id="inputEmail" name="inputEmail" placeholder="Required">
</div>
<div class="form-group">
<label for="inputPhone" class="control-label">Phone Number</label>
<input type="text" class="form-control" id="inputPhone" name="inputPhone" placeholder="Optional">
</div>
<div class="form-group">
<label for="inputMessage" class="control-label">Message</label>
<textarea class="form-control" rows="5" id="inputMessage" name="inputMessage" placeholder="Brief Description"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-custom pull-right hvr-underline-from-left">Send</button>
</div>
</form>
PHP
<?php
/* Set e-mail recipient */
$myemail = "myaccount#gmail.com";
/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "First and Last");
$email = check_input($_POST['inputEmail'], "Required");
$phone = check_input($_POST['inputPhone'], "Optional");
$message = check_input($_POST['inputMessage'], "Brief Description");
/* 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 = "Contact Message from mywebsite.net";
$message = "
Someone has sent you a message using your contact form:
Name: $name
Email: $email
Phone: $phone
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location:contact.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();
}
?>

Please change this line:
$phone = check_input($_POST['inputPhone'], "Optional");
to:
$phone = check_input($_POST['inputPhone']);
This way show_error($problem); won't be called.

Because function check_input include function show_error.
And function show_error have exit() when have errors.
So, your phone input == NULL => so have error and call to exit().
Solution for this case
Don't check require with phone number input.

PHP
<?php
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();
}
/* 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;
}
/* Set e-mail recipient */
$myemail = "myaccount#gmail.com";
/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "First and Last");
$email = check_input($_POST['inputEmail'], "Required");
$phone = $_POST['inputPhone'];
$message = check_input($_POST['inputMessage'], "Brief Description");
/* 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 = "Contact Message from mywebsite.net";
$message = "
Someone has sent you a message using your contact form:
Name: $name
Email: $email
Phone: $phone
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location:contact.html');
exit();
?>

You are facing this error because you are validating the phone number i.e $_POST['inputPhone'] using the check_input function in the line
$phone = check_input($_POST['inputPhone'], "Optional");
You can avoid this error in multiple ways:
$phone = $_POST['inputPhone']; //not secure
OR
$phone = check_input($_POST['inputPhone'], "");
OR
$phone = filter_var($_POST['inputPhone'],FILTER_SANITIZE_STRIPPED);

Related

How to make a submit form talk to my php file to send an email

I am trying to get a website form to send to my email. The host has provided the Path to SendMail as:
/usr/sbin/sendmail but I am not sure where to put it. Below is the form HTML and the PHP that I currently have.
This is the form HTML that I have
<form class="email" action="pages/mailer.php" method="post">
<div class="brown">
<h2>Request A FREE In-House Estimate</h2></div>
<div="boxy">
<div class="row">
<p class="col-sm-6 col-sm-offset-4">
<label><span>Name</span>
<input type="text" class="input_text" name="name" id="name"/> </label>
<label>
<span>Email</span>
<input type="text" class="input_text" name="email" id="email"/> </label>
<label>
<span>Number</span>
<input type="text" class="input_text" name="subject" id="subject"/> </label>
<label>
<span>Message</span>
<textarea class="message" name="message" id="message"></textarea>
<br/>
<input class="button" type="submit" value="Send" />
</label>
</div>
</div>
</form>
This is the PHP 'mailer.php' that lives in my pages folder.
<?php
/* Set e-mail recipient */
$myemail = "personalemail#live.com";
/* Check all form inputs using check_input function */
/* 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 contact 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: ../index.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();
}
?>
Currently, when I hit submit the url changes to pages/mailer.php and I get an error
'Please correct the following error:
Invalid e-mail address
Hit the back button and try again'.
Any help is greatly appreciated!Thanks!

php form not validatin or sending mails

I a trying to fix a php form that doesn't work
I am new to php, please help
I have the form in the index.html file and have another validation.php to validate the form fields.
<!-- contact form --->
<div class="containerq">
<div class="form_container">
<h2 class="text-center">Contact us</h2>
<form action="index.php" method="post" class="rd-mailform">
<input type="hidden" value="contact" name="form-type">
<fieldset>
<div class="row">
<div class="col-md-prefix-1 col-md-12">
<label data-add-placeholder="" class="mfInput">
<input type="text" name="name" placeholder="Name">
</label>
</div>
<div class="col-md-prefix-1 col-md-12">
<label data-add-placeholder="" class="mfInput">
<input type="text" name="email" placeholder="Email">
</label>
</div>
<div class="col-md-prefix-1 col-md-12">
<label data-add-placeholder="" class="mfInput">
<input type="text" name="phone" placeholder="Phone">
</label>
</div>
<div class="col-md-prefix-1 col-md-12">
<label data-add-placeholder="" class="mfSelect">
<select class="opt">
<option>Choose subject</option>
<option>I have a problem with a game</option>
<option>I have a problem with a course</option>
<option>I have an Idea</option>
<option>Collaboration with schools</option>
<option>Collaboration with game developers</option>
<option>General query and other subjects</option>
</select>
<ul class="dropdown">
<li class="option">I have a problem with a game</li>
<li class="option">I have a problem with a course</li>
<li class="option">I have an Idea</li>
<li class="option">Collaboration with schools</li>
<li class="option">Collaboration with game developers</li>
<li class="option">General query and other subjects</li>
</ul><span class="mfPlaceHolder"></span>
</label>
</div>
<div class="col-md-prefix-1 col-md-12">
<label data-add-placeholder="" class="mfInput">
<textarea data-constraints="" name="message" placeholder="Message" class="mtext"></textarea>
</label>
</div>
<!--<input class="header_button" type="submit" name="submit" value="Send Request">-->
<div class="header_button">Send Request </div>
<span class="success"><?php echo $successMessage;?></span>
<div class="mfInfo mfProgress"><span class="cnt"></span><span class="loader"></span><span class="msg"></span></div>
</div>
</fieldset>
</form>
</div>
</div>
in the header (which is in included at the index.php start) I added:
<?php require_once "validation.php";?>
because if I put require_once validation.php at the start of the index the form wouldn't be accessed.
in the validation.php this is the part that sends the mail:
if( !($name=='') && !($email=='') && !($phone=='') &&!($message=='') )
{ // Checking valid email.
if (preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/",$email))
{
$header= $name."<". $email .">";
$headers = "mywebsite.com"; /* Let's prepare the message for the e-mail */
$msg = "Hello! $name Thank you...! For Contacting Us.
Name: $name
E-mail: $email
Phone: $phone
Message: $message
This is a Contact Confirmation mail. We Will contact You as soon as possible.";
$msg1 = " $name Contacted Us. Hereis some information about $name.
Name: $name
E-mail: $email
Phone: $phone
Message: $message "; /* Send the message using mail() function */
if(mail($email, $headers, $msg ) && mail("receiver_mail_id#mywebsite.com", $header, $msg1 ))
{
$successMessage = "Message sent successfully.......";
}
}
else
{
$emailError = "Invalid Email";
}
}
but id doesn't send anything
the page gets reloaded to index.php#
I don't get the invalid email or Message sent successfully messages either
this is the complete validation.php:
<?php // Initialize variables to null.
//echo "validating";
$name =""; // Sender Name
$email =""; // Sender's email ID
$purpose =""; // Subject of mail
$message =""; // Sender's Message
$phone ="";
$nameError ="";
$emailError ="";
$phoneError ="";
$messageError ="";
$successMessage =""; // On submittingform below function will execute.
if(isset($_POST['submit']))
{ // Checking null values in message.
echo "validating2";
if (empty($_POST["name"])){
$nameError = "Name is required";
}
else
{
$name = test_input($_POST["name"]); // check name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$nameError = "Only letters and white space allowed";
}
} // Checking null values inthe message.
if (empty($_POST["email"]))
{
$emailError = "Email is required";
}
else
{
$email = test_input($_POST["email"]);
} // Checking null values inmessage.
if (empty($_POST["phone"]))
{
$phoneError = "Phone is required";
}
else
{
$phone = test_input($_POST["phone"]);
} // Checking null values inmessage.
if (empty($_POST["message"]))
{
$messageError = "Message is required";
}
else
{
$message = test_input($_POST["message"]);
} // Checking null values inthe message.
if( !($name=='') && !($email=='') && !($phone=='') &&!($message=='') )
{ // Checking valid email.
if (preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/",$email))
{
$header= $name."<". $email .">";
$headers = "mywebsite.com"; /* Let's prepare the message for the e-mail */
$msg = "Hello! $name Thank you...! For Contacting Us.
Name: $name
E-mail: $email
Phone: $phone
Message: $message
This is a Contact Confirmation mail. We Will contact You as soon as possible.";
$msg1 = " $name Contacted Us. Here is some information about $name.
Name: $name
E-mail: $email
Phone: $phone
Message: $message "; /* Send the message using mail() function */
if(mail($email, $headers, $msg ) && mail("contact#mywebsite.com", $header, $msg1 ))
{
$successMessage = "Message sent successfully.......";
}
}
else
{
$emailError = "Invalid Email";
}
}
} // Function for filtering input values.function test_input($data)
// Function for filtering input values.
function test_input($data)
{
$data = trim($data);
$data =stripslashes($data);
$data =htmlspecialchars($data);
return $data;
}
?>
any ideas?

PHP Contact form not returning errors, but I am still not receiving any emails

Forgive me if this is a stupid question, but I am pretty new to PHP and I am running into some issues. I am trying to build a contact form with HTML, CSS, and PHP, but I can't seem to get my PHP form to send the contents of the form to my email address. This is what the code looks like for the HTML:
<div id="contact-form">
<ul>
<li><button id="quote" class="button1">Project Quote</button></li>
</ul>
<form class= "emai" action="mailer.php" method="post">
<p>Have a project in mind? Fill in the form for a quote!</p>
<div>
<p><label for="name">What can I call you? <span>*</span></label></p>
<input type="text" id="name" name="name">
</div>
<div>
<p><label for="email">What is your email? <span>*</span></label></p>
<input type="email" name="email" id="email">
</div>
<div>
<p><label for="type">Type of Project? <span>*</span></label></p>
<select id="type" name="type">
<option value="logo">Logo Design</option>
<option value="web dev">Website/WebApp Dev</option>
<option value="other">Other</option>
</select>
</div>
<div>
<p><label for="purpose">What is the main purpose of your project? <span>*</span></label></p>
<textarea id="purpose" name="purpose"></textarea>
</div>
<div>
<p><label for="features">Any extra features?</label></p>
<textarea id="features" name="features"></textarea>
</div>
<input class="button1" type="submit" value="submit">
</form>
</div>
And in a separate doc called "mailer.php" this is what the code looks like:
<?php
/* Set e-mail recipient */
$myemail = "christopher.kenrick#gmail.com";
$subject = "Project Request";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$email = check_input($_POST['email']);
$type = check_input($_POST['type'], "Select a type of project");
$purpose = check_input($_POST['purpose'], "What is the purpose of your project?");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "
Name: $name
E-mail: $email
Type: $type
Purpose: $purpose
Features: $features
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.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();
}
?>
Here is a link to my website if it will help. Can someone tell me what it is I am doing wrong?
Your code lacks proper mail headers. (and originally had a missing subject variable which you now added).
Add and modify your present mail() function with the following code, otherwise mail will be sent directly to Spam as it did for my test.
$headers = 'From: ' . $email . "\r\n";
mail($myemail, $subject, $message, $headers);
with a conditional statement:
if(mail($myemail, $subject, $message, $headers)){
echo "Success"; } else{ echo "There was a problem.";}
After running sudo apt-get install sendmail I finally started receiving the contents of the contact form. This solution should work for those using DigitalOcean as their host.

How to send form to self email?

This is my contanct.html
<form action="sendmail.php" method="post">
<p><b>Your Name:</b> <input type="text" name="yourname" /><br />
<b>Subject:</b> <input type="text" name="subject" /><br />
<b>E-mail:</b> <input type="text" name="email" /><br />
Website: <input type="text" name="website"></p>
<p>Do you like this website?
<input type="radio" name="likeit" value="Yes" checked="checked" /> Yes
<input type="radio" name="likeit" value="No" /> No
<input type="radio" name="likeit" value="Not sure" /> Not sure</p>
<p>How did you find us?
<select name="how">
<option value=""> -- Please select -- </option>
<option>Google</option>
<option>Yahoo</option>
<option>Link from a website</option>
<option>Word of mouth</option>
<option>Other</option>
</select>
<p><b>Your comments:</b><br />
<textarea name="comments" rows="10" cols="40"></textarea></p>
<p><input type="submit" value="Send it!"></p>
<p> </p>
<p>Powered by PHP form</p>
</form>
This is my php
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$subject = check_input($_POST['subject'], "Write a subject");
$email = check_input($_POST['email']);
$website = check_input($_POST['website']);
$likeit = check_input($_POST['likeit']);
$how_find = check_input($_POST['how']);
$comments = check_input($_POST['comments'], "Write your comments");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
$website = '';
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Name: $yourname
E-mail: $email
URL: $website
Like the website? $likeit
How did he/she find it? $how_find
Comments:
$comments
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: confirmation.htm');
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)
{
?>
I got a problem when i upload in domain
It's working but didn't not send to my email of Form
I don't know what is my problem?
I'm newbie in web.dev
sorry for my bad english thanks.
I found and figured out what the problem is.
The function show_error($myError) was unfinished.
Plus you also needed to add $myemail = "email#example.com"; replacing it with your E-mail address.
Also make sure this page exists on your server header('Location: confirmation.htm');
Tested, working.
<?php
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$subject = check_input($_POST['subject'], "Write a subject");
$email = check_input($_POST['email']);
$website = check_input($_POST['website']);
$likeit = check_input($_POST['likeit']);
$how_find = check_input($_POST['how']);
$comments = check_input($_POST['comments'], "Write your comments");
$myemail = "email#example.com"; // Replace with your E-mail address.
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
$website = '';
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Name: $yourname
E-mail: $email
URL: $website
Like the website? $likeit
How did he/she find it? $how_find
Comments:
$comments
End of message
";
/* Send the message using mail() function */
$headers="From: $name<$email>\r\nReturn-path: $email\r\n";
mail($myemail, $subject, $message, $headers);
/* Redirect visitor to the thank you page */
header('Location: confirmation.htm');
// echo "ok"; // For testing purposes only
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)
{
?>
<b>We apologize for the inconvenience, an error occurred.</b><br />
<?php echo $myError; ?>
<?php
exit();
}

Contact Form with recaptcha producing 500 error

I have a contact form with recaptcha that i am trying to set up but produces a 500 error when information is entered and the submit button is clicked. I'm going to bet money that I have done something stupid as I'm not the greatest with PHP, so if I post my code here, would anyone be able to spot anything wrong?
This is the Contact Form itself.
<form class="email" action="mailer.php" method="post">
<p>Name:</p>
<input type="text" name="name" />
<p>E-mail:</p>
<input type="text" name="email" />
<p>Subject:</p>
<input type="text" name="subject" />
<p>Message:</p>
<textarea name="message"></textarea></p>
<script type="text/javascript" src="http://api.recaptcha.net/challenge?k=6Le8_t4SAAAAACIxacT6Xn8NVvDa93loylG-L6mk"></script>
<noscript>
<iframe src="http://api.recaptcha.net/noscript?k=PUBLICKEYGOESHERE" height="300" width="500" frameborder="0"></iframe>
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type="hidden" name="recaptcha_response_field" value="manual_challenge">
</noscript>
<input class="send" type="submit" value="Send">
</form>
And this is the mailer.php
<?php
require_once('recaptchalib.php');
if ($_POST['email'] != '')
{
$privatekey = "6Le8_t4SAAAAAME8kuqO1bvzcSWmGytwISUYLo3w";
$resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
if (!$resp->is_valid)
{
die("The reCAPTCHA wasn't entered correctly.
Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")");
}
else
{
/* Set e-mail recipient */
$myemail = "mandy#smarterbookkeeping.com.au";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$subject = check_input($_POST['subject'], "Enter a subject");
$email = check_input($_POST['email']);
$message = check_input($_POST['message'], "Write your message");
$captcha = check_input($_POST['captcha']);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "
Name: $name
E-mail: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.php');
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();
}
}
}
?>
I have the recaptchalib in the right place, same goes for the thanks.php. I'm not very good at PHP, but this one has me stumped.

Categories