提交按钮不工作在PhP上的第一次点击,但它的第二次工作


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'; ?>

我有两个文本框'用户名'和'密码'。但是当我填写文本框,点击提交按钮,它不工作,不执行php代码。

当我尝试使用空文本框时,它执行php代码并给出此错误-无效的用户名密码。

在您的表单动作中添加#并将方法更改为post

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

和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")
    }
}

你还没有填写表单的动作部分

一个很好的例子:

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

既然你给了它PHP标签,这应该工作和重定向你的表单到当前文件。

如果您想使用其他php文件:

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

更新

完整的表单示例。也许这能帮到你。

<?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>

注意:

如果这对你没有帮助,把下面的代码放在你的文件的顶部:

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

将打开错误报告并显示可能发生的错误。如果你有任何错误,请把它们贴在这里(编辑你的问题),这样我们可以进一步帮助你。