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/
Related
I have a form in a file named signup.php like this,
<form action="includes/signup.inc.php" method="post">
<input type="text" name="uid" placeholder="Username..."><br><br>
<input type="text" name="mail" placeholder="Email..."><br><br>
<input type="password" name="pwd" placeholder="Password..."><br><br>
<input type="password" name="repeatPwd" placeholder="Repeat Password..."><br><br>
<input type="submit" name="btnSignup" value="Signup">
</form>
Then I have this if statement in the signup.inc.php file.
if (!preg_match("/^[a-zA-z0-9]*$/", $username)) {
header("Location: ../signup.php?error=invaliduid&mail=".$email);
exit();
}
That is if the user enter invalid username, then the user will be route back to the signup page, and I get an error message in the address bar, now I want to get the email from the address bar which the user entered, I make an if statement back in the signup.php file like this,
if ($_GET['error'] == "invaliduid") {
echo "Invalid username.";
$email = $_GET['mail'];
}
Now I want to fill the original form
<input type="text" name="mail" placeholder="Email...">
And put the email from $_GET['mail'] and put it in the value portion of the form, but I am stuck on how to do this.
The program should work like this, if the user entered proper mail and an invalid username then the user route back to the signup page with the message invalid username, and email field should be populated with the email which he already entered.
From what I understand, what you're looking for is simply this:
<input type="text" name="mail" placeholder="Email..." value="<?= htmlspecialchars($email) ?>">
(which is a less ugly way to write the following:)
<input type="text" name="mail" placeholder="Email..." value="<?php echo htmlspecialchars($email); ?>">
This here is working as intended:
<input type="text" name="mail" placeholder="Email..." value="<?php echo(isset($email))?$email:'';?>">
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 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>
-Checking if the username is entered.
-Basically I am trying to say if the user did not enter a username, echo "Please insert a username".
<form action="form_3.php" method="get">
Username: <input type="text" name="username" value="" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
$username=$_GET["username"];
if (!isset($username)) {echo "Please insert a USERNAME";}
else{echo "Hello: ".$username;}
?>
You can do this simply with HTML.
Username: <input type="text" name="username" value="" required>
You should add your php code on a different page to avoid any load errors.
You shouldn't use
isset();
but
empty();
The "empty" function is internally doing a "isset" on the value and checking that the variable is not an empty string.
http://php.net/manual/en/function.empty.php
The html5 validator can also be used to avoid your serveur to actually have to return the form saying "Please enter your username", but it can be easily disabled by a visitor.
<input type="text" name="username" value="" required>
When I load the page the first time it shows an error because the username is already empty. How can fix that error?
I have this form and have issues with my $_POST data not coming through:
Form:
<form action="" id="contact-form" method="post">
<input id="nombre" size="16" type="text" placeholder="Nombre">
<input id="email" size="16" type="text" placeholder="Email">
<textarea id="texto"></textarea>
<input type="submit" value="Enviar">
PHP:
<?php
if(!empty($_POST) {
echo "YES";
} else {
echo "NO";
}
?>
It always echoes "NO" when I complete and submit all the fields.
You missed the name attribute of the input fields. Change your html and add them:
<form action="" id="contact-form" method="post">
<input id="nombre" name="nombre" size="16" type="text" placeholder="Nombre">
<input id="email" name="email" size="16" type="text" placeholder="Email">
<textarea id="texto" name="texto"></textarea>
<input type="submit" value="Enviar">
Note that the id attribute is not required. You can omit it if you don't need it for other purposes like using it in a stylesheet or in javascript. For simply sending the form just the name attribute is required
if you want a specific field, try:
if(isset($_POST['field']{0})){
If you want to check for multiple fields POST, try:
if(count($_POST)>0){
Your input should have the name attribute set, which sets them in $_POST array. id is ignored in posting forms!
<input id="nombre" size="16" type="text" placeholder="Nombre" name="nombre">
and you will get it in $_POST['nombre']
This is an alternative solution ,if you add "name" attribute to your inputs , it should work:
if (count($_POST) == 0) {
echo "YES";
}
else {
echo "NO";
}
You need to specify a variable, so $_POST['email'] for example. For all of the fields try;
if(!empty($_POST['Nombre']) && !empty( $_POST['email']) && !empty ( $_POST['texto'])) { ...
Sorry I'm on a mobile device, such a pain to get all the spelling correct.
if (empty($_POST['qty'])) {
echo "<script>alert('inputan Quality tidak boleh kosong');history.go(-1);</script>";
}
else {
...
}
so no validation error, here I use array