How do i prevent the error-message from showing before the 'submit'-button is clicked?
For some reason the content of 'else'-statement is visible on pageload - the message "NO" should only appear after typing something else than 'a' in this.. what am i doing wrong?
<form method="POST">
<input type="text" class="textfield" id="cursor" name="pass">
<input type="submit" class="button" name="submit" value="OK">
</form>
<?php
$pass = $_POST['pass'];
if($pass == 'a') {
echo "YES";
}
else{
echo "NO";
}
?>
Add this PHP code:
<?php
if(isset($_POST['pass'])) {
// your check here
}
?>
Test to see if the submit button is in the submitted data.
if (isset( $_POST['submit'] )) {
<form method="POST">
<input type="text" class="textfield" id="cursor" name="pass">
<input type="submit" class="button" name="submit" value="OK">
</form>
<?php
$pass = $_POST['pass'];
//You need to check if the form is submitted
if($_POST['submit']){
if(isset($pass) && !empty($pass) && $pass == 'a') {
echo "YES";
}
else{
echo "NO";
}
?>
Related
<?php
session_start();
?>
<form action="HardDisk.php" method="post">//form
<input type="text" name="user" />
<?php
if(isset($_POST['user']))
{
$_SESSION['userGet']=$_POST['user'];
}
?>
<input type="submit" value="submit" />
</form>
2nd page
<?php
session_start();
?>
<?php
if (isset($_SESSION['userGet'])) {
$form_value = $_SESSION['userGet'];
}
echo $form_value ;
?>
..........................................................................................................
You have used the text box name with that check the submit button name too.
<form action="HardDisk.php" method="post">//form
<input type="text" name="user" />
<input type="submit" value="submit" name="submit" />
</form>
<?php
session_start();
if(isset($_POST['submit'])&&isset($_POST['user'])) {
$_SESSION['userGet']=$_POST['user'];
}
?>
And in your second page
<?php
session_start();
if (isset($_SESSION['userGet'])) {
echo $form_value = $_SESSION['userGet'];
}
Give the submit button a name of submit then use the following PHP code:
if(isset($_POST['submit'])){
if(isset($_POST['user']))
{
$_SESSION['userGet']=$_POST['user'];
}
}
i want to do somthing like:
1.php:
<html>
<form action=1.php method=POST enctype="multipart/form-data">
Choose a user name:<input type="text" name="username">
<input type="submit" value="Save and Proceed">
</html>
<?php
if(isset($_POST['username']))
{
$name=$_POST['username'];
echo $name;
if($name=='azra')
{
?>
<html>
<form method="POST" action="1.php" enctype="multipart/form-data"></br>
enter age:</font> <input type="text" name="age">
<input type="submit" value="done">
</form>
</html>
<?php
f( isset($_POST['age']))
{
$age=$_POST['age'];
echo $age;
if($age==25)
{
echo "your age is ". $age;
echo"you are eligible";
}
}
}
}
?>
After the second form is submitted i do not want the script 1.php to run from the start but i want it to run the code following the form which is echoing the age only.
i do not want to go putting the later code in second script and accessing the variable of first script through making them session variables.please help. thankyou in advance
Change your condition to this
if ((isset($_POST['username'])) && ($_POST['submit'] == 'Save and Proceed')) {
}
this is the code i changed:
<html>
<form method="POST" action="2.php" enctype="multipart/form-data"></br>
Choose a user name:</font> <input type="text" name="username">
<input type="submit" value="Save and Proceed">
</form>
</html>
<?php
if(isset($_POST['username']) && ($_POST['submit'] == 'Save and Proceed'))
{
$name=$_POST['username'];
echo $name;
if($name=='azra')
{
?>
<html>
<form method="POST" action="2.php" enctype="multipart/form-data"></br>
enter age:</font> <input type="text" name="age">
<input type="submit" value="done">
</form>
</html>
<?php
if( isset($_POST['age']) && ($_POST['submit'] == 'done'))
{
$age=$_POST['age'];
echo $age;
if($age==25)
{
echo "your name is" .$name;
echo "your age is ". $age;
echo"you are eligible";
}
}
}
}
?>
I am a newbie I am having a little problem on how can i call the button inside my if statement. I am trying to work on a userlog where when you click a button it will output on the user log.
but how can i call the >>
THE VIEW button inside my if statement so that it will fwrite inside the userlog.txt.
inside this if($_POST['submit'] = ") same goes in the search button and the add edit delete button. did u get my point?
<body background="images.jpg">
<?php
session_start();
if($_SESSION['username'])
{
echo "Welcome, ".$_SESSION['username']."!<br><a href='logout.php'>Logout</a><br>";
echo '<FORM METHOD="LINK" ACTION="mydata4.php">
<INPUT TYPE="submit" VALUE="Edit/Delete/add">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="mydata2.php">
<INPUT TYPE="submit" VALUE="View" id="viewbutton">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="display_data.php">
<INPUT TYPE="submit" VALUE="Search">
</FORM>';
}
else
{
die("You must be logged in!");
}
if($_POST['submit'] = ")
{
$date=date("Y-m-d H:i:s");
$updatefile = "userlogs.txt";
$fh = fopen($updatefile, 'a') or die("can't open file");
$stringData = "User: $username click view button";
fwrite($fh, "$stringData".PHP_EOL);
fclose($fh);
}
?>
Use
if(isset($_POST))
instead of
if($_POST['submit'] = ")
try this... you are doing mistake in your if condition.
<body background="images.jpg">
<?php
session_start();
if($_SESSION['username'])
{
echo "Welcome, ".$_SESSION['username']."!<br><a href='logout.php'>Logout</a><br>";
echo '<FORM METHOD="LINK" ACTION="mydata4.php">
<INPUT TYPE="submit" VALUE="Edit/Delete/add">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="mydata2.php">
<INPUT TYPE="submit" VALUE="View" id="viewbutton">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="display_data.php">
<INPUT TYPE="submit" VALUE="Search" name="submit" id="submit">
</FORM>';
}
else
{
die("You must be logged in!");
}
if($_POST['submit'] == "submit")
{
$date=date("Y-m-d H:i:s");
$updatefile = "userlogs.txt";
$fh = fopen($updatefile, 'a') or die("can't open file");
$stringData = "User: $username click view button";
fwrite($fh, "$stringData".PHP_EOL);
fclose($fh);
}
?>
you can set the same name to your submit buttons ...
<input type="submit" name="submit" value="View" />
<input type="submit" name="submit" value="Search" />
and in your PHP, you can simply use as you mentioned
if( $_POST["submit"] == "View" )
...
if( $_POST["submit"] == "Search" )
...
one more things is that, you have to use method="post" in your <form> tag if you want to use $_POST in php
<form method="post" action="....php">
I have a multi-step form, let's say for the sake of ease it's 2 steps. First step I want to select a radio button and based on that radio button selection it takes me to a certain page, but I also want that selection stored in a session. I have 2 pages:
page1.php
session_start();
if(isset($_POST['post'])) {
if (($_POST['country'] == 'US')) {
header("Location: US_Products.php"); }
elseif (($_POST['country'] == 'CDN')) {
header("Location: CDN_Products.php"); }
else { die("Error"); }
exit;
}
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="USA">USA:</label>
<input type="radio" name="country" value="US">
<label for="CDN">Canada:</label>
<input type="radio" name="country" value="CDN">
<input type="submit" name="post" value="Go To Filter">
</form>
Page2.php (either A or B)
session_start();
$_SESSION['country'] = $_POST['country'];
<?php echo $_SESSION['country']; ?>
The Country choice is not being passed when I have it do this conditional redirect. Is there a problem with session variables and redirects or session variables and PHP_SELF or something?
Page 1:
session_start();
if(isset($_POST['post'])) {
$_SESSION['country'] = $_POST['country'];
if (($_POST['country'] == 'US')) {
header("Location: US_Products.php"); }
elseif (($_POST['country'] == 'CDN')) {
header("Location: CDN_Products.php"); }
else { die("Error"); }
exit;
}
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="USA">USA:</label>
<input type="radio" name="country" value="US">
<label for="CDN">Canada:</label>
<input type="radio" name="country" value="CDN">
<input type="submit" name="post" value="Go To Filter">
</form>
Page 2:
session_start();
<?php echo $_SESSION['country']; ?>
Or using include method, just use one page:
session_start();
if(isset($_POST['post'])) {
if (($_POST['country'] == 'US')) {
include("US_Products.php"); }
elseif (($_POST['country'] == 'CDN')) {
include("CDN_Products.php"); }
else { die("Error"); }
exit;
}
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="USA">USA:</label>
<input type="radio" name="country" value="US">
<label for="CDN">Canada:</label>
<input type="radio" name="country" value="CDN">
<input type="submit" name="post" value="Go To Filter">
</form>
and you should be able to use echo $_POST['country'] on US_Products.php and CDN_Products.php, or
Set the session variable before you redirect - the post is lost, as the redirect is essentially a regular GET request
if (($_POST['country'] == 'US'))
{
$_SESSION['country'] = $_POST['country'];
header("Location: US_Products.php");
}
elseif (($_POST['country'] == 'CDN'))
{
$_SESSION['country'] = $_POST['country'];
header("Location: CDN_Products.php");
}
else
{
die("Error");
}
<form method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>" />
Username:<input type = "text" name ="user"> <br />
Password:<input type = "password" name = "pass"><br />
<input type = "submit" value ="View Logs!"><br />
<?php
$user = $_POST['user'];
$pass = $_POST['pass'];
//Problem here, I need to only allow the user to see logs
// after he or she has entered the correct info.
//Currently code just shows all, when the user hits View Logs
// without any credentials
if (($user == "php") && ($pass == "student"))
echo "Enjoy the Logs!";
else echo "<b>Access Denied!</b>";
?>
The problem is that your form is posting directly to log.txt and not processing any of your PHP after the form submission. You'll need to change the action to post to the PHP file itself and then use http_redirect to redirect the user to log.txt after checking the password.
Having said that it's still not going to be very secure though as anyone could get to log.txt by using a direct URL, so you'll need to do some kind of authorisation there. The best thing to do is probably to store log.txt somewhere that's not accessible by through HTTP and then load and display the file using readfile in place of your echo:
<form action="" method="post">
Username:<input type="text" name="user"/> <br />
Password:<input type="password" name="pass"/><br />
<input type="submit" value="View Logs!"/><br />
</form>
<?php
$user = $_POST['user'];
$pass = $_POST['pass'];
if (($user == "php") && ($pass == "student")) {
echo '<pre>';
readfile('log.txt');
echo '</pre>';
}
else {
echo "<b>Access Denied!</b>";
}
?>
<?
if (
isset( $_POST['user'] )
&& isset( $_POST['pass'] )
) {
$user = $_POST['user'];
$pass = $_POST['pass'];
if (
($user == 'php')
&& ($pass == 'student')
) {
echo "Enjoy the Logs!";
readfile('log.txt');
}
else {
echo '<b>Access Denied!</b>';
}
} else {
?>
<form method="post">
Username:<input type="text" name="user"> <br />
Password:<input type="password" name="pass"><br />
<input type="submit" value="View Logs!"><br />
<?
}