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.
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 am currently making a report error form that has 4 fields:
Job ID $jobid
Part ID part_id
Machine
Note
The user clicks on a table corresponding the their work and are brought to a new page with a url that has variable. At the moment all the fields are empty however I want the fields to be populated automatically except for notes.
Current Model
Link to report error form:
$EM_html = ''.$tick.'
Report error form:
<form action="" method="post">
Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
Example URL
http://sra-pstest/report_error_form.php?JobID=KANBAN16-09-04-01&Machine=EM&PartID=124047
How do "extract" the information out of the url (JobID, Machine, PartID) and automatically fill out the form?
You can use $_GET
<?php
if(isset($_GET))
{
foreach($_GET as $key=>$value)
{
$$key=$value;
}
echo $JobID."<br>".$Machine."<br>".$PartID;
}
?>
Please try this
<?php
$jobid = #$_REQUEST['JobID'];
$part_id = #$_REQUEST['PartID'];
$machCode = #$_REQUEST['Machine'];
?>
<form action="" method="post">
Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
You use $_GET Method like this code
<?php
$jobid=$part_id=$machine="";
if(isset($_GET['JobID']))
{
$jobid= $_GET['JobID'];
}
if(isset($_GET['Machine']))
{
$machine= $_GET['Machine'];
}
if(isset($_GET['PartID']))
{
$part_id= $_GET['PartID'];
}
?>
<form action="" method="post">
<?php $jobNumber = isset($_GET['JobID']) ? $_GET['JobID'] : '' ?>
Job Number: <input type="text" value="<?php echo jobNumber; ?>" name="jobNum"><br>
<input type="submit" name="submit" value="Submit">
</form>
Try using isset and post method to check if variable are declared and get the variable data on submit of form
<?php
if(isset($_POST['submit'])){
$jobid = $_POST['JobID'];
$part_id = $_POST['PartID'];
$machCode = $_POST['Machine'];
}
?>
<form action="" method="post">
Job Number: <input type="text" value="<?php echo $jobid; ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php echo $part_id; ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode" value="<?php echo $machCode; ?>"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
Hope this help
I am doing the login page If the user not successfully login I will use the the below to handle it, which means the message will be showed on the left of text box pwd. But the message never showed when the login failed. Can you tell me why
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>Username:</label>
<input type="text" name="name" size="10"> </br>
<label>Password:</label>
<input type="text" name="pwd" size="10"> </br>
<?php
echo '<div style="position:absolute; left:650px; top:25px;float:left">';
if (isset($username))
{
// if they've tried and failed to log in
echo 'Your password is not right<br />';
}
echo '</div>';
?>
Register  
<input type=submit name=login value=Login>
</form>
If I dont put PHP code, the message will be showed blow the pwd textbox.But I want is to the left of text box. I am sure the problem is CSS style, because when I just define DIV only has one properties, which is float left the error will be showed blow pwd text box, login and register will be to the right of this error message, which is very strange
If you are not using AJAX, and you are not familiar with it.
Try this!
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>Username:</label>
<input type="text" name="name" size="10"> </br>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if(!empty($_POST['name']) && !empty($_POST['pwd'])){
$name = trim($_POST['name']);
$password = trim($_POST['pwd']); # Normally this password is encrypted
# Database Connection Here
if // if they've tried and failed to log in
{
echo '<div style="position:absolute; left:650px; top:25px;float:right">Your password is not right<div/>';
}
}
}
?>
<label>Password:</label>
<input type="text" name="pwd" size="10"> </br>
Register  
<input type="submit" name="login" value="Login">
The better solution or practice would be to use AJAX. With AJAX you can display your error message anywhere you want. If you want to practice go to the website link below:-
http://www.developphp.com/view.php?tid=1296
haven't programmed PHP in a while but I
have to assemble something for a client really fast.
I've set up 2 forms with POST but when I go to the next file it's just blank space, for some reason POST isn't being registered but is set cause I'm not getting an error echo.
Hese's the forms:
<form action="Funkcije.php" method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj">
</form>
<br>
<div id="newItem">
<form action="Funkcije.php" method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj">
</form>
</div>
And here's the 2nd file:
if(isset($_POST["AddFromDB"], $_POST["ArtNo"])){
addExisting ($_POST["ArtNo"]);
}
else if(isset($_POST["AddNew"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
newItem ($_POST["Art"] && $_POST["ImeProizvoda"] && $_POST["Dobava"] && $_POST["Cijena"]);
}
else if (!isset ($_POST)){
echo "error";
}
So, by code I should be getting an error if POST is not set but I get nothing. Just a blank space.
here, you must be give a name to the submit button to check which form is POST like this...
<form method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj" name="form1">
</form>
<br>
<div id="newItem">
<form method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj" name="form2">
</form>
</div>
<?php
if(isset($_POST["form1"], $_POST["ArtNo"])){
echo "1";
}
else if(isset($_POST["form2"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
echo "2";
}
else{
echo "error";
}
?>
now, this work fine..
thank you.. enjoy coding...
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.