-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?
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:'';?>">
<?php require 'db_connect.php'; ?>
<form id="form1" name="form1" method="post" enctype="multipart/form-data" action="register.php" >
Username :
<input type="text" id="username" class="username" name="username" value="">
<br>
Password :
<input type="password" id="password" class="password" name="password" value="">
<br>
Confirm Password :
<input type="password" id="con_password" class="con_password" name="con_password" value="">
<br>
Phone No. :
<input type="text" id="phoneno" class="phoneno" name="phoneno" value="">
<br>
<input type="submit" id="submit" name="submit" class="submit" value="Register">
</form>
<?php
if(isset($_REQUEST['submit']))
{
$res = "insert into register(username, password, phoneno) value('".$_POST["username"]."','".$_POST["password"]."','".$_POST["phoneno"]."')";
mysqli_query($conn, $res);
if(!empty($_POST)){
echo "Registered!";
}
else {
echo "Try Again!";
}
}
?>
I want to check all $_Post fields check for empty, null or 0 if values of form is not filled show error "try again" and filled show error "registered"? please tell me first is this function is correct or not. if not correct what the correct function.
If you want to validate fields through php, you have to check each field individually. There are some checks you can apply on a data.
if ($_POST['field'] === null) //I would suggest === because it will return false if $_POST['field'] is 0 or empty string
if (empty($_POST['field'])) //to check empty data
if (isset($_POST['field'])) //to check if data exists
If you want to validate your fields like is either string? or digits? or alpha numeric? or all lowercase? or uppercase? There are certain functions which allows you to check field by just calling a single function
Please have a look at this page: CType Functions
You can also use this option. Make all fields required you want to check null or 0, this will make sure that data in field is must before submitting form.
Try changing input field from:
<input type="text" id="username" class="username" name="username" value="">
to:
<input type="text" id="username" class="username" name="username" value="" required >
Also as Rishi said you should also edit query, change values to values
Also 1 tip, you are inserting data in database first and then validating data, according to me it is not good practice, you should first validate data then if all went good then insert it into database, else you will add bad data in database also.
Your final code will be:
<?php require 'db_connect.php'; ?>
<form id="form1" name="form1" method="post" enctype="multipart/form-data" action="register.php" >
Username :
<input type="text" id="username" class="username" name="username" required >
<br>
Password :
<input type="password" id="password" class="password" name="password" required >
<br>
Confirm Password :
<input type="password" id="con_password" class="con_password" name="con_password" required >
<br>
Phone No. :
<input type="text" id="phoneno" class="phoneno" name="phoneno" required >
<br>
<input type="submit" id="submit" name="submit" class="submit" value="Register">
</form>
<?php
if(isset($_POST['submit']))
{
$res = "insert into register(username, password, phoneno) values('".$_POST["username"]."','".$_POST["password"]."','".$_POST["phoneno"]."')";
mysqli_query($conn, $res);
}
?>
Hi there!
I have a login form like this:
<form method="post" action="login.php">
Username:
<p><input type="text" name="id" /></p>
Password:
<p><input type="password" name="pass" /></p>
<input type="submit" value="Log In" name="login" />
<input type="reset" value="Reset" />
</form>
and the validation is in login.php aswell, a single file validation).
If the email or password doesn't match the error message appears below the form. If the input is valid, it redirects to homepage.php (header ("location: homepage.php");)
What I want to do is: if the password is wrong, or doesn't match with the username, it displays the form again with the original username input.
Is it by using cookie?
If anyone has any suggestion, please let me know.
Sankyu :)
Try saving the old value inside the session variable ($_SESSION) and displaying it on the value property of the element.
Ok i am trying to find if this is possible, i have a single page that has two login forms. But uses the same username and password. Is it possible whereas once i log into one form my username is pulled from the from that already has my username and the password is pulled from the database?
<form action="home.php" method="post" id="LoginForm" style="color:#FFF">
<input type="hidden" name="SessionID" value="new"></input>
Username:
<input type="text" name="AccountNo" value="" class="input" size="28"></input>
<BR /><BR />
Password:
<input type="password" name="Password" value="" class="input" size="30"></input>
<BR />
<input type="image" src="images/input-img.gif" value="Log On" class="input-img"></input>
<BR /><br />
Register now! Forgotten your password?
</form>
If your question is whether it is possible, then answer is yes it is possible.
After you submit the form, you can use JavaScript to read the username from first form and populate it in the second form. To get password, you can AJAX.
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/