简体   繁体   English

提交按钮第一次单击时无法在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'; ?>

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. 但是,当我填写文本框并单击“提交”按钮时,它不起作用且未执行php代码。

When I try with empty text-box it executes php code and gives this error - invalid username password. 当我尝试使用空文本框时,它将执行php代码并给出此错误-无效的用户名密码。

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. 既然您给了它PHP标记,它应该可以工作并将您的表单重定向到当前文件。

If you would like to use another php file: 如果您想使用另一个php文件:

<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 在您的表单操作中添加#并更改要post方法

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

and in php 和在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")

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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