I have some html forms that I verify completeness with php. The issue I have is that when one required form is not filled, the filled forms are cleared.
The HTML
<p>Email: <span class="required">* <?php echo $EmailErr;?></span><input type="text" name="Email" placeholder="Email" /></p>
<p>Comments: =input type="text" name="Comments" maxlength="75" placeholder="Comments"/></p>
This is the PHP
if (empty($_POST["Email"])) {
$EmailErr = "";
} else {
$Email = validateEmail($_POST["Email"]);
}
if (empty($_POST["Comments"])) {
$Comments = "";
} else {
$Comments = test_input($_POST["Comments"]);
}
The question remains, how do I prevent the other forms from being cleared upon submission?
You should do a client side validation in order to retain the values on your form.
That being said you should still have server validation.
There is different way to do it, using javascript/jquery or even simply adding required attribute to your tag, for example:
<input type="text" name="Email" placeholder="Email" required/>
for javascript:
http://www.w3schools.com/js/js_validation.asp
for jquery , here is a good plugin:
http://jqueryvalidation.org/
<p>
Email: <span class="required">*<?php echo $EmailErr; ?></span>
<input type="text" name="Email" placeholder="Email" value="<?php echo!empty($_POST['Email']) ? htmlspecialchars($_POST['Email']) : '' ?>"/>
</p>
<p>
Comments: <textarea name="Comments" maxlength="75" placeholder="Comments"><?php echo!empty($_POST['Comments']) ? htmlspecialchars($_POST['Comments']) : '' ?></textarea>
</p>
Related
I have two inputs with same name in my form ( I use css to display one or the other depending of the screen size ), but on the form submition, is only gets the last one's value, which is a problem to me because is can be empty if I display:none it. I figured out a trick which consists of cloning the input value which is not empty to the other, so that when I send my POST variable to PHP, any one can return the value entered.
Here's my HTML code :
<form id="form" class="form" name="postuler" method="post" action="receive-post.php" enctype="multipart/form-data" novalidate>
<div class="not-hidden">
<label for="mail">Email address <span class="required">*</span></label>
<input type="email" class="email" placeholder="ex: jdoe#exelcia-it.com" name="email" id="email" maxlength="40" />
</div>
<div class="hidden-fields">
<label for="mail">Email address <span class="required">*</span></label>
<input type="email" class="email" placeholder="ex: jdoe#exelcia-it.com" name="email" id="email" maxlength="40"/>
</div>
<input type="submit" name="upload" id="send" value="Submit" class="postu"/>
</form>
And here's my jQuery code :
$(document).ready(function(){
$('#envoyez').on('click', function () {
if($.trim($('.hidden-fields #email').val())==0){
$('.hidden-fields #email').val()==$.trim($('.not-hidden #email').val())
} else if ($.trim($('.not-hidden #email').val())==0){
$('.not-hidden #email').val()==$.trim($('.hidden-fields #email').val())
}
})
})
Here's my php code :
if(!empty($_POST['email'])){
var_dump($_POST['email']);
} else {
echo "Theres a problem somewhere" ;
}
The problem that I get is that if I fill the not-hidden field, on the php side I get an empty string because it always retrieve the hidden-fields value.
Hello you can try like this
<input type="email" class="email" placeholder="ex: jdoe#exelcia-it.com" name="email[]" id="email" maxlength="40" />
<input type="email" class="email" placeholder="ex: jdoe#exelcia-it.com" name="email[]" id="email" maxlength="40"/>
Then you can loop in the php script to check if the array is empty and get the populated key.
UPDATE
Here's how you can check the array and get the populated key
if (isset($_POST['email'])) {
$emails = $_POST['email'];
foreach ($emails as $key => $email) {
if (!is_null($email)) {
echo $email;
break;
}
}
}
This is basic check you can save the email in variable or perform additional checks but this is the basic to get the value
There are couple of things wrong in your jQuery code, such as:
After triming the input text field's value, you're directly comparing it with 0, which is wrong. Instead you should compare it's length with 0.
$('... #email').val()==$.trim($...), == is for comparison and = is for assignment.
So your jQuery code should be like this:
$(document).ready(function(){
$('#envoyez').on('click', function () {
if($.trim($('.hidden-fields #email').val()).length == 0){
$('.hidden-fields #email').val($.trim($('.not-hidden #email').val()));
} else if ($.trim($('.not-hidden #email').val()).length == 0){
$('.not-hidden #email').val($.trim($('.hidden-fields #email').val()));
}
})
});
With the above snippet, it doesn't matter which input text field gets filled, the other field will also get updated with the same value automatically. And once you hit the submit button, you would get just one email id with the POST request.
Another approach would be to use name attribute as name='email[]' for both of your email input fields. There's no need to use any jQuery here.
<input type="email" class="email" placeholder="ex: jdoe#exelcia-it.com" name="email[]" id="email" maxlength="40" />
<input type="email" class="email" placeholder="ex: jdoe#exelcia-it.com" name="email[]" id="email" maxlength="40"/>
This way, you can access the submitted email id(doesn't matter which input text field gets filled) in the following way,
if(isset($_POST['email'])){
$email = array_values(array_filter($_POST['email']));
if(count($email))
echo $email[0];
else
echo "Theres a problem somewhere";
}
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.
I am trying to set up an application form on my site where there is a main large application form on one page (parent.php)and a smaller form on another page (child2.php) that when users fill out some of their details and submit that smaller form, they are taken to the larger form and the details they have entered already appear in the corresponding textbox on the larger form along side some extra boxes for them to fill out(if that makes sense!)
I can get it to work in that the textboxes on the 2nd page display the values of the matching textboxes on the first page, but only when a value is set. As users can either access the main application form through the smaller form OR by directly accessing it, I need to have it so that if the value is set, the set value is displayed and will also be the value entered into the database OR if the value is not pre-set from the smaller form, the user can enter in their info to the main form and this is what's sent to the DB. I think I might need to use ifisset and have tried to do so but am getting nowhere.
Apologies for messy code and the set up of the textboxes as they are just for testing this out and I am still getting to grips with all this and would be grateful if anyone could help me/let me know if I'm on the right track/totally off. Thanks in advance!
Page 1 (parent.php)
<form action="child2.php" method="post" class="validate">
<div>
<input class="tb" type="text" name="fName" placeholder="first name" id="fName" value="<?php $fName ?>" required/><br/>
<br/>
<input class="tb" type="text" name="sName" placeholder="surname" id="sName" value="<?php $sName ?>" required/><br/>
<br/>
<input class="tb" type="email" name="email" required placeholder="email address" id="email" value="<?php $email ?>" required/>
<br/>
<input class="tb" type="address" name="address" placeholder="address" value="<?php $address ?>" id="address" />
<br/>
<input id="submit" name="submit" type="submit" value="Submit">
</div>
</form>
Page 2 (child2.php)
<?php
function renderForm($fName, $sName, $email, $address){
?>
<form action="" method="post" class="validate">
<label class="label">first name</label><input class="tb" type="text" id="fName" name="fName" value="<?php if (isset($fName)) { echo $fName = $_REQUEST['fName'];} else { echo "first name"; }?>"/>
</br>
<label class="label">surname</label><input class="tb" type="text" id="sName" name="sName" value="<?php if (isset($sName)) { echo $sName = $_REQUEST['sName'];} else { echo "surname"; }?>"/>
</br>
<label class="label">email</label><input class="tb" type="email" id="email" name="email" value="<?php if (isset($email)) { echo $email = $_REQUEST['email'];} else { echo "email"; }?>"/>
</br>
<label class="label">address</label><input class="tb" type="text" id="address" name="address" value="<?php if (isset($address)) { echo $address = $_REQUEST['address'];} else { echo "address"; }?>"/>
</br>
What you're looking for are $_SESSION variables, which stay active until the user closes the browser or until the Session expires (I think the default PHP config time is 24 minutes..?). This is a lot more efficient than storing them in a database for temporal purposes.
So you can set the variables with $_SESSION['fName'] = $_POST['fName']; etc. and then call them later with echo $_SESSION['fName']; etc.
To utilize session variables you will need <?php session_start(); ?> at the beginning of your pages (before any HTML is used)
As suggested by khanahk, using SESSION variables will do your job. The problem with the solution you are thinking is that its fine only as long as you are passing values from one page to some other page, that too after so much mess.
So, you should instead, you should use something like this
session_start();
$email = $_SESSION['email'];
<label class="label">email</label><input class="tb" type="email" id="email" name="email" value="<?php echo $email; ?>"/>
How can I make fields still in the form fields after submitting the form and return with invalid input data?
<form action="" method="post">
<label for="cellPhoneNo">البريد الالكتروني</label>
<input type="text" name="emailAddress" class="textField"/>
<span>*</span>
<span><?php
if($emailValidator != CORRECT_VALUE)
echo $errorMessage[$emailValidator];?>
</span>
<br>
</form>
and here's where I check the input
$emailValidator=checkInput($_POST['emailAddress'],INVALID_EMAIL_ADDRESS,'/^[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$/');
when user submit invalid email address, the validation result shows the error message to user. How can I make the invalid error email address still in the input?
add to input
<input type="text" name="emailAddress" class="textField" value="<? echo (isset($_POST) ? $_POST['emailAddress'] : "") ?>"/>
instead of "" you can insert your default value or just leave it like this :)
Try,
<input type="text" name="emailAddress" value="<?php echo $_POST['emailAddress']; ?>" class="textField"/>
Try to use javascript to validate the email before posting it to your server-side code :)
Check this link: http://www.zparacha.com/validate-email-address-using-javascript-regular-expression/