I am using the code below, but want error messages when the user skips a required field. Can anyone help, please? So if a user forgets to fill the required input filed "first name", that an error message appears above the field with the text "First name is a required field".
<form id="comment_form" action="form.php" method="post">
<div class="compulsoryfield">
<input class="inputfield-radio" type="radio" name="gender" value="Mr" required><label class="label-radio">Mr.</label>
<input class="inputfield-radio" type="radio" name="gender" value="Ms"><label class="label-radio">Ms.</label>
<span class="requiredmark-radio">*</span>
</div>
<div class="compulsoryfield"><span class="requiredmark">*</span>
<input class="inputfield3" type="firstname" placeholder="first name" required>
</div>
<div class="compulsoryfield"><span class="requiredmark">*</span>
<input class="inputfield1" type="lastname" placeholder="last name" required>
</div>
<input class="inputfield2" type="companyname" placeholder="company name (if applicable)">
<input class="inputfield2" type="customernumber" placeholder="customer number (on invoice if available)" >
<br><br>
<div class="compulsoryfield"><span class="requiredmark">*</span>
<input class="inputfield3" type="email" placeholder="email address" required>
</div>
<div class="compulsoryfield"><span class="requiredmark">*</span>
<input class="inputfield1" type="emailagain" placeholder="re-enter email address (to confirm)" required>
</div>
<input class="inputfield2" type="telephonenumber" placeholder="telephone number (country code included)">
<br><br>
<div class="compulsoryfield"><span class="requiredmark">*</span>
<input class="inputfield3" type="subject" placeholder="subject of message" required>
</div>
<div class="compulsoryfield"><span class="requiredmark">*</span>
<textarea id="textareafieldid" class="textareafield" name="message" placeholder="add your message here" rows="8" cols="39" required></textarea></div><br><br>
<p id="recaptcha-header">before sending, please show us you're real:</p>
<div><span class="requiredmark">*</span><div id="g-recaptcha-outer" class="compulsoryfield2">
<div class="g-recaptcha" data-sitekey="mySitekey"></div></div><br><br>
<input id="button-type1" type="submit" name="submit" value="SEND">
</form>
and
<?php
$email;$comment;$captcha;
if(isset($_POST['gender'])){
$email=$_POST['gender'];
}if(isset($_POST['firstname'])){
$email=$_POST['firstname'];
}if(isset($_POST['lastname'])){
$email=$_POST['lastname'];
}if(isset($_POST['companyname'])){
$email=$_POST['companyname'];
}if(isset($_POST['customernumber'])){
$email=$_POST['customernumber'];
}if(isset($_POST['email'])){
$email=$_POST['email'];
}if(isset($_POST['emailagain'])){
$email=$_POST['emailagain'];
}if(isset($_POST['telephonenumber'])){
$email=$_POST['telephonenumber'];
}if(isset($_POST['subject'])){
$email=$_POST['subject'];
}if(isset($_POST['message'])){
$email=$_POST['message'];
}if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Please check the captcha form.</h2>';
exit;
}
$secretKey = "mtSecretKey";
$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 '<h2>You are a spammer !</h2>';
} else {
echo '<h2>Thanks for your email.</h2>';
}
?>
I suggest you to try js plugins like :
http://parsleyjs.org/
or
http://www.formvalidator.net/
You gonna see, the first is for me the best , and the second is very simple.
With the second you just have to add an attribute rules to your field like :
data-validation="required".
With this example you can required a field.
send the destination of the form to the same page the form is on.
At the top of the page before any html code is sent to the client, do your form handling.
$problemstring = "";
$firstname = "";
if(isset($_POST['submit'])){
if(!isset($_POST['firstname'])){$problemstring = "Error in firstname"}
//repeat for all fields
}
if(strlen($problemString)==0) {
//Do database actions here
//The Line below redirects to another page if form good
if(strlen($problemString)==0) die(header('Location: index.php'));
}
This code you check your form for blank data then if there are no problems run the database operations. If there is a problem the rest of the page will load.
Put the code below where you want the error message to be displayed.
<?php
if(strlen($problemString)>0) {
echo '<table><tr>';
echo '<td colspan="3">';
echo '<font color="#FF0000"><strong>There was a problem with your form</strong><br>';
echo $problemString;
echo '</font></td>';
echo '</tr>';
echo '<tr><td colspan="3" height="16"></td></tr></table>';
}
?>
In your form do this:
<input class="inputfield3" type="firstname" placeholder="first name" value="<?php echo $firstname; ?>" required>
this will populate the field with what was entered into the form before the submit, if this was the empty field it would be blank. if the page is loaded for the first time the value would be blank (or whatever you set).
Ive used this in my website and it works well.
Related
I am trying to write a simple html form which requires the user to enter the correct email and password, using $_SERVER as the action for my form. I do not want to send the POST info to another php page, but I want to do it on the same page instead.
I have set two variables, $correct_email and $correct_password.
Here is the form;
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="eml">Email:</label>
<input type="email" name="eml" id="eml" required>
<br>
<label for="pwd">Password:</label>
<input type="password" name="pwd" id="pwd" required>
<br>
<input type="hidden" name="checkMe" value="12345">
<input type="submit" name="submit" value="Submit">
</form>
Here is what I am trying to get work in PHP
$correct_email = "(my personal email)";
$correct_password = "password"];
if ($_POST['eml']) == $correct_email && ($_POST['pwd']) == $correct_password {
echo "<h1>You are logged in</h1>";
} else {
echo "<h1>error</h1>";
}
It is not working, please help! I am also unclear on whether the PHP should come before or after the form.
Also, I would like to have the form be cleared from view, on the page, when the correct info is entered.
Thanks so much
Change the name of the submit button - never call anything "submit" in a form
Put the test at the top
You had issues with the ( and ) in the test
Also an issue with a ] after "password"
<?php
if ($_POST["subBut"] === "Submit") {
$correct_email = "(my personal email)";
$correct_password = "password";
if ($_POST['eml'] == $correct_email && $_POST['pwd'] == $correct_password) {
echo "<h1>You are logged in</h1>";
} else {
echo "<h1>error</h1>";
}
}
else {
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]); ?>">
<label for="eml">Email:</label>
<input type="email" name="eml" id="eml" required>
<br>
<label for="pwd">Password:</label>
<input type="password" name="pwd" id="pwd" required>
<br>
<input type="hidden" name="checkMe" value="12345">
<input type="submit" name="subBut" value="Submit">
</form>
<?php } ?>
I cant figure out whats wrong with the code.
The problem occurs when i try to send form data to email.
So far its good but i cant get response code to work.
I mean when i fill form and click send it wont show predefined error when some of the forms are empty or when it was sent. Response should be in a same window or form.
Code i have at the moment:
<form name="action" method="post" onsubmit="return false;">
<?php if( isset($error_msg) && $error_msg != '' ) { echo $error_msg; } ?>
<input type="text" name="name" id="name" placeholder="Ees-ja perekonnanimi">
<input type="text" name="cname" id="cname" placeholder="Ettevõtte nimi" style="width: 55%;"> <input type="text" name="regnum" id="regnum" placeholder="Reg. number" style="width: 25%;">
<input type="text" name="email" id="email" placeholder="E-post">
<textarea name="info" placeholder="Sõnum" cols="40"></textarea>
<input type="submit" value="Esita" onclick="return getData()">
<div id="error_msg"><?php echo $error_msg; ?></div>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$action=$_REQUEST['action'];
{
$name=$_REQUEST['name'];
$cname=$_REQUEST['cname'];
$regnum=$_REQUEST['regnum'];
$email=$_REQUEST['email'];
$info=$_REQUEST['info'];
if (($name=="")||($email==""))
{
$error_msg="All fields are required";
}
else{
$data="Kliendi nimi: $name \r\nEttevõtte nimi: $cname \r\nEttevõtte reg. number: $regnum \r\nE-post: $email \r\nSõnum: $info";
$subject="Tellimus";
mail("someone#somewhere.com", $subject, $data);
$error_msg="Your message was sent, Thank you!";
}
}
}
?>
</form>
any advice?
Hi I've got a problem with submitting my own simple form in Wordpress it always redirects me to the page witch not exists. I use almost all thing for action field in form like (get_permalink,$_SERVER["PHP_SELF"]) and even did it manually.
<?php
if (isset($_POST["email"]) && $_POST["email"]) {
$content = "user email: {$_POST["email"]}; phone : {$_POST['phone']} ";
$subject = "Test email ";
$to = "some#gmail.com";
$status = wp_mail($to,$subject,$content);
}
?>
<div class="center form-wrapper">
<form action="<?php echo get_permalink(); ?>" method="post">
<h2 class="center white form-logo">
Subscribe
</h2>
<input type="text" placeholder="Enter email" name="email">
<input type="text" placeholder="Enter name" name="name">
<input type="text" placeholder="Enter phone number" name="phone">
<input type="hidden">
<input type="submit" name="submit" value="send" >
</form>
I was wondering if there was a way to to check if html text inputs are empty, and if so, execute a certain PHP code, which I will provide.
HTML
<input name="name" type="text" class="form-control form-control-lg" id="exampleInputName2" placeholder="your name" maxlength="36" value="<?php echo $name; ?>">
<input style="margin-top: 10px;" name="email" type="text" class="form-control form-control-lg" id="exampleInputEmail2" placeholder="your email" maxlength="36" value="<?php echo $email; ?>">
<button name="mySubmitBtn" type="submit" class="btn btn-default btn-md">subscribe</button>
PHP
if(!isset(['name']) || !isset(['email']) == 0){
$msg_to_user = '<h1>Fill out the forms</h1>';
}
I was wondering if:
My PHP code is correct syntax, which I believe it's not
Is it possible to check if an input is empty, and if it is empty,
execute a PHP code (specifically '$msg_to_user')
It is possible to tweak my code so that when the user clicks the
submit button, and the fields are BOTH empty, to have the
$msg_to_user code execute
Thank you in advance!
You should use isset check on array - $_POST or $_GET according to your HTML form method.
It should be:
<form method="POST">
<input name="name" type="text" class="form-control form-control-lg" id="exampleInputName2" placeholder="your name" maxlength="36" value="">
<input style="margin-top: 10px;" name="email" type="text" class="form-control form-control-lg" id="exampleInputEmail2" placeholder="your email" maxlength="36" value="">
<button name="mySubmitBtn" type="submit" class="btn btn-default btn-md">subscribe</button>
</form>
PHP:
if (!isset($_POST['name']) || !isset($_POST['email']))
{
$msg_to_user = '<h1>Fill out the forms</h1>';
} else {
// process your results
}
I guess, I have answered questions A and B. What about question C - it's up to your architecture. For example, if you want to post form from page to itself (I mean index.php file has a form with action='index.php' or empty action), then you can do it this way (just an example approach, definitely not the best):
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!isset($_POST['name']) || !isset($_POST['email']))
{
$msg_to_user = '<h1>Fill out the forms</h1>';
} else {
// do something in database
if ($doSomethingInDatabaseSuccess)
$msg_to_user = '<h1>Succesffully saved!</h1>'; // really? h1? :)
else
$msg_to_user = '<h1>Something went wrong!</h1>';
} else {
$msg_to_user = '';
}
<form method="POST">
<input name="name" type="text" class="form-control form-control-lg" id="exampleInputName2" placeholder="your name" maxlength="36" value="">
<input style="margin-top: 10px;" name="email" type="text" class="form-control form-control-lg" id="exampleInputEmail2" placeholder="your email" maxlength="36" value="">
<button name="mySubmitBtn" type="submit" class="btn btn-default btn-md">subscribe</button>
</form>
<p class="result"><?php echo($msg_to_user); ?></p>
A. No your syntax is off.
B. Yes
C. Yes, first you need to give your button a name property, so that it can be picked up by PHP. You also need to assign the $email and $name variables with a $_POST variable.
HTML
<input type="text" name="name" class="form-control" id="exampleInputName2" placeholder="your name" maxlength="36" value="<?php echo $name; ?>">
<input type="email" style="margin-top: 10px;" name="email" class="form-control" id="exampleInputEmail2" placeholder="your email" maxlength="36" value="<?php echo $email; ?>">
<input type="submit" name="mySubmitBtn" class="btn btn-default btn-md" value="subscribe">
PHP
//check to see if the submit button is pressed
if(isset($_POST['mySubmitBtn'])){
//assign the variables from the content of the input form
$email = $_POST['email'];
$name = $_POST['name'];
//check if name is empty
if(!isset($name) || trim($name) ==''){
$errorMsg = "You must enter a name";
return;
}
//check if email is empty
if(!isset($email) || trim($email) ==''){
$errorMsg = "You must enter an email";
return;
}
//do something else
I've gone thru tons of the forms and cant seem to find the answer. I have been working on this problem with my php form on and off for days. hope to find help here. the form is working perfect. all the fields are working correct upon submit, but there always seems to be a second form sent out from a day to two days later that is blank. If there is any suggestions to why this occurs it would be helpful.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
// recipient address
$to = "grandforkssuites#gmail.com";
// subject of email
$re = "Contact Us Form Delivery";
// message creation
$contact = "Name:".$name."\nEmail:".$email."\nSubject:".$subject."\r\n";
$txt = "Comments:".$comments."\r\n";
$fmsg = $contact."\r\n".$txt;
$msg = wordwrap($fmsg, 70);
// send email
mail($to,$re,$msg);
?>
<form action="contact1.php" method=post name="form" id="form">
<div class="col_w280 float_l">
<p><em>
<label for="author">Name:</label> <input type="text" id="name" name="name" class="required input_field" />
<div class="cleaner_h10"></div>
<label for="email">Email:</label> <input type="text" id="email" name="email" class="validate-email required input_field" />
<div class="cleaner_h10"></div>
<label for="email">Phone:</label> <input type="text" id="phone" name="phone" class="required input_field" />
<div class="cleaner_h10"></div>
<label for="subject">Subject:</label> <input type="text" name="subject" id="subject" class="input_field" />
<div class="cleaner_h10"></div>
</div>
<div class="col_w280 float_r">
<label for="text">Comments:</label> <textarea id="comments" name="comments" rows="0" cols="0" class="required input_field"></textarea>
<div class="cleaner_h10"></div></em></p>
<input name=submit type=submit id="submit" onClick="MM_validateForm('name','','R','email','','RisEmail');return document.MM_returnValue" value="Send">
</div></form>
Add validation to the PHP, else even if no values was sent via POST, just by visiting the page its going to send a blank email. Most likely a search engine or such bot is just crawling.
So check its POST
<?php
if($_SERVER['REQUEST_METHOD']==='POST'){
//put code here
}
?>
and check your values are set min-max length ect
<?php
...
...
...
//Comments
if(empty($_POST['comments'])){
//comments empty, do or set something
}else if(strlen($_POST['comments']) < 5){
//not long enough, do or set something
}else if(strlen($_POST['comments']) > 50){
//too large, do or set something
}
?>
and most importantly check email is really an email..
<?php
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
//is an email
}else{
//not an email
}
?>
Also your want to add a basic captcha else your be enjoying 1000s of marketing/spam emails per day.
Good luck, implementing it.