I have got my validation working but I wanted to only output it, when it has been submitted but isset nor empty seems to work for me for some reason? I am using php 5.5.3 I believe
My code:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="register">
<input type="text" placeholder="Username" maxlength="15" name="username"/><br>
<input type="password" maxlength="15" name="password1"/><br>
<input type="password" maxlength="15" name="password2" /><br>
<input type="text" placeholder="your#email.com" maxlength="25" name="email"/><br>
<input type="text" maxlength="20" name="county" /><br>
<input type="submit" value="Register" name "register"/>
</form>
<?php
//Form Validation
if(empty($_POST['register']))
{
echo "Option A <br>";
}
else
{
echo "Option B <br>";
}
Also, I can't remember how I would enter in the same information for that user if it validates fine?
value="<?php $_POST['stuff'] ?>
<input type="submit" value="Register" name "register"/>
^
You're missing an = there.
It should be:
<input type="submit" value="Register" name="register"/>
It's betterto use isset() instead of empty(), in this case:
if(isset($_POST['register']))
{
echo "Option A <br>";
}
else
{
echo "Option B <br>";
}
I'm not sure what Option A and B are, but with this code, you'll always see Option B when you load the page. The condition in your if block will execute to FALSE and hence the else block will executed. If you want to avoid this, you can use an elseif statement instead of an else.
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 } ?>
<?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);
}
?>
Some thing is wrong in this code, because when i press the submit button the code inside this block of code it is not executed.
Here is the code:
if(isset($_POST["form"])) {
if (Form::isValid($_POST["form"])){
include 'config_php/insert_lead.php';
} else{
header("Location: " . $_SERVER["PHP_SELF"]);
exit();
}
}
This is a small portion of the form:
<form action="index.php?s=2468" id="multiphase" method="post" class="form-horizontal">
<fieldset>
<input type="text" class="form-control shadow" name="first_name" required="" placeholder="Voornaam:" id="multiphase-element-22">
<input type="text" class="form-control shadow" name="last_name" required="" placeholder="Achternaam: " id="multiphase-element-23">
<input type="email" class="form-control shadow" name="email1" required="" placeholder="E-mailadres:" id="multiphase-element-28">
<input type="submit" value="Submit" name="submit" class="btn btn-primary" id="multiphase-element-29">
</fieldset>
</form>
One more thing the form is generated using PFBC
There isn't a post value named 'form', so you should set one to look for on the submission.
Notice that I have also used the entire POST array in the Form::isValid() check.
PHP:
if(isset($_POST["postback"])) {
$valid = true;
if (Form::isValid($_POST)){
include 'config_php/insert_lead.php';
} else{
header("Location: " . $_SERVER["PHP_SELF"]);
exit();
}
}
HTML:
<form action="index.php?s=2468" id="multiphase" method="post" class="form-horizontal">
<fieldset>
<input type="text" class="form-control shadow" name="first_name" required="" placeholder="Voornaam:" id="multiphase-element-22">
<input type="text" class="form-control shadow" name="last_name" required="" placeholder="Achternaam: " id="multiphase-element-23">
<input type="email" class="form-control shadow" name="email1" required="" placeholder="E-mailadres:" id="multiphase-element-28">
<input type="hidden" name="postback" value="1">
<input type="submit" value="Submit" name="submit" class="btn btn-primary" id="multiphase-element-29">
</fieldset>
</form>
In your code $_POST["form"] is the fault you doing...
if you are using $_POST["form"] then it means you have named any of your input field as "form" which i dont see in your code...
so instead of $_POST["form"] you should use any of the input name you using...
I suggest to use..
$_POST["submit"] instead of $_POST["form"]
because you have already named submit input field as submit...
See if it help
Change form tor submit:
<?php
if(isset($_POST["submit"])) {
if (Form::isValid($_POST["form"])){
include 'config_php/insert_lead.php';
} else{
header("Location: " . $_SERVER["PHP_SELF"]);
exit();
}
}
The the if will be executed BUT the you will still have problems with the next if (Form::isValid($_POST["form"])) I don't know how isValid works but $_POST["form"] does not exist. You can validate all inputs one by one.
I am trying to get a username and password from an HTML form using $_POST method,
but i am getting an undefined index error with given input.
the form portion of my index file is as follows
<form class="form-signin" role="form"action="" method="post">
<h2 class="form-signin-heading">title<em class='current'>title</em></h2>
<input id="username" type="username" class="form-control" placeholder="Username" required autofocus>
<input id="password" type="password" class="form-control" placeholder="Password" required>
<input name ="submit" type="submit" value="sign in">
</form>
i also have my php script (called auth.php) included in the top of my index file.
this is my auth.php script code sample
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
echo "auth is called";
if(isset($_POST['submit'])){
echo "submit is pressed";
//this if statement enters when submit is pressed
if(empty($_POST['username']) || empty($_POST['password'])) {
echo "Username or Password is empty";
//this if statement enters too, even with input given
}
else{
echo "YAY";
}
}
?>
The "username or password is empty" statement keeps getting printed, even when I enter in a username and password into the input fields. What is the problem
Missing "name" attribute in input fields, it should be
EDIT 2 : Missing type="text" in username input
<input type="text" name='username' id="username" class="form-control" placeholder="Username" required autofocus>
<input name='password' id="password" type="password" class="form-control" placeholder="Password" required>
Correct your form , 1. Name missing, 2. No datatype with the name of username
<form class="form-signin" role="form" action="" name="loginform" method="post">
<h2 class="form-signin-heading">title<em class='current'>title</em></h2>
<input id="username" name="username" type="text" class="form-control" placeholder="Username" required autofocus>
<input id="password" name="password" type="password" class="form-control" placeholder="Password" required>
<input name="submit" type="submit" value="sign in">
</form>
<input id="username" name="username" type="username" class="form-control" placeholder="Username" required autofocus>
<input id="password" type="password" name="password" class="form-control" placeholder="Password" required>
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
echo "auth is called";
if(isset($_POST['submit'])){
echo "submit is pressed";
//this if statement enters when submit is pressed
if(empty($_POST['username']) || empty($_POST['password'])) {
echo "Username or Password is empty";
//this if statement enters too, even with input given
}
else if(empty($_POST['username']){
echo "Username is empty";
}
else if(empty($_POST['password']){
echo "Password is empty";
}
else{
echo "YAY";
}
}
?>
input type = text and name attr required
<input name='username' id="username" type="text" class="form-control" placeholder="Username" required autofocus>
Check values using print_r() or var_dump if you are not getting the desired output.
This will help you to check which part of your code malfunctions. If you have done so, you would have found that there's something wrong only in your html form content, as you wont be having any field content printed and you would have easily sorted it out...
Set the username input's type to "text":
<input name='username' id="username" type="text" class="form-control" placeholder="Username" required autofocus>
<input name='password' id="password" type="password" class="form-control" placeholder="Password" required>
I have a simple php page that contain form with action that directed to a file called getData.php, here’s the PHP page:
<?php
if(isset($_GET['login_attempt'])) {
echo 'LOGIN ATTEMP 1';
}else{
echo 'NO LOGIN ATTEMP';
}
?>
<form id="info" action="getData.php" method="post">
<input type="text" class="input" name="info1" /><br>
<input type="password" class="input" name="info2" /><br>
<input type="submit" value="" id="submit-button">
</form>
And here's the getData.php file:
if (!isset($_GET['login_attempt'])) {
header('location: /login.php?login_attempt=1');
} else {
header('location: http://www.stackoverflow.com');
}
As you can see in the getData.php file after two attempts of logging in the php page should go to http://www.stackoverflow.com, but the php page get stuck in /login.php?login_attempt=1 forever after the first attempt, why?
Here is how you can handle this
<?php
$attempt = 1;
if(isset($_GET['login_attempt'])) {
$attempt ++;//Now attempt will have 2
echo 'LOGIN ATTEMP 1';
}else{
echo 'NO LOGIN ATTEMP';
}
?>
<form id="info" action="getData.php" method="post">
<input type="hidden" class="input" name="login_attempt" value='<?php echo $attempt;?>' /><br>
<input type="text" class="input" name="info1" /><br>
<input type="password" class="input" name="info2" /><br>
<input type="submit" value="" id="submit-button">
</form>
And here's the "getData.php" file:
<?php
if (isset($_POST['login_attempt']) AND $_POST['login_attempt'] !== 2) {
header('location: /login.php?login_attempt=1');
} else {
header('location:http://www.stackoverflow.com');
}
?>
EDIT
Why your page stuck on first attempt. Here is the answer.
When ever you redirect to login page the first condition is matched. As you have no criteria to tell getData.php which attempt is this there fore you are stuck. Use a variable $attempt as i have used in login.php initialize it with 1. If there is 2nd attempt increment it with 1. Put it in h hidden field so that getData.php its number. and will then redirect to according to your needs.
Change the line
<input type="submit" value="" id="submit-button">
to
<input type="submit" value="" id="submit-button" name="login_attempt">
and
from file getData.php
change the line
if (!isset($_GET['login_attempt']))
to
if (!isset($_POST['login_attempt']))
as you are using post method in the form ( <form id="info" action="getData.php" method="post">).
and change this line to
if(isset($_GET['login_attempt']))
to
if($_GET['login_attempt']==1)
i think this will solve your problem.