简体   繁体   中英

Submit button not working in PhP on first click but it works on second time

<?php include 'header.php';

?>
<div id="body">
<form name="form4" method="get" action="" >
 <table border="1" align="center">
  <tr>
<td colspan="2" align="center">
    <b>Login</b>
</td>
</tr>
<tr>
<td>
  Email : </td>
<td><input type="email" name="txtEmail"></td>
</tr>
<tr>
<td>Passowrd : </td>
<td><input type="password" name="txtPwd"></td>
</tr>

<tr>
<td align="center"> <input name="login" type="submit" id="login" value="login"></td>
<td align="center"><input type="button" name="txtCancel" value="Cancel"/></td>
</tr>
</table>
</form>
</div>
<?php
include('connect.php');
if(isset($_REQUEST['login']))
{
$Email=$_GET['txtEmail'];
$Pwd=$_GET['txtPwd'];
$query="select * from usermaster where EmailId='$Email' and  Password='$Pwd'";
 echo $query;
$result=$conn->query($query);
if ($result->num_rows > 0)
    {
        echo "valid UserName and Password";
        $_SESSION['email']=$Email;// session already started in header.php file
        header("location:user/index.php");
    } 
else 
{
echo "Error: " . $query . "<br>" . $conn->error;
//echo "Invalid UserName and Password";
}

}
?>
 <?php include 'footer.php'; ?>

I have two text boxes 'username' and 'password'. But when I fill the text boxes and click on submit button it is not working and not executing php code.

When I try with empty text-box it executes php code and gives this error - invalid username password.

You haven't filled the action part of your form

A good example:

<form method="get" name="form4" action="<?= $_SERVER['PHP_SELF']; ?>">        
    <!-- Your form here -->
</form>

Since you gave it the PHP tag this should work and redirect your form to the current file.

If you would like to use another php file:

<form method="get" name="form4" action="php_file.php">
    <!-- Your form here -->
</form>

Update

A complete form example. Maybe this will help you out.

<?php
if (isset($_GET)) {
    $username = isset($_GET['username']) ? $_GET['username'] : false;
    $password = isset($_GET['password']) ? $_GET['password'] : false;

    if (false === $useranme || false === $password) {
        die("Not all fields are filled!");
    }
}
?>
<form method="GET" action="<?= $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="username" />
    <input type="password" name="password" />
    <input type="submit" name="login" value="Login" />
</form>

Note:

If this doesnt help you, place the following code at the top of your file:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
?>

That will turn on error reporting and show errors that might occur. If you have any errors, post them here (edit your question) so we can help you further.

in your form action add # and change method to post

<form name="form4" method="post" action="#" >

and in php

if (isset($_POST['login']))
{
    $email = $_POST['txtEmail'];
    $pwd = $_POST['txtPwd'];

    $query = "select * from usermaster where EmailId='$email' and  Password='$pwd'";

    $result = mysql_query($query);
    $count = count($result);

    if (empty($count) || $count > 1)
    {
        #invalid user
        echo "Invalid username";
    }
    else
    {
        #valid user
        echo "valid UserName and Password";
        //$_SESSION['email'] = $email; // session already started in header.php file
        //header("location:user/index.php")

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM