表单操作在 php 中不起作用?
Form action not working in php?
我基本上一页有2个表格。
第一个用于登录,第二个用于插入数据。
第二种形式的动作工作正常。我可以用它插入数据。
但是我为登录用户使用的是相同的表单,但它不起作用。
单击提交按钮时什么也没有发生,只是页面刷新。
请查看我的代码并帮助我解决第一个表单的操作问题。
<div class="login_wrapper">
<div id="login" class="animate form login_form">
<section class="login_content">
<form action="login.php" method="post">
<h1>Login Form</h1>
<div>
<input type="text" class="form-control" placeholder="Username" required="" name="username" />
</div>
<div>
<input type="password" class="form-control" placeholder="Password" required="" name="password" />
</div>
<div>
<input type="submit" class="btn btn-default" name="insert" value="Sign In">
<a class="reset_pass" href="#forgetpass">Lost your password?</a>
</div>
<div class="clearfix"></div>
<div class="separator">
<p class="change_link">New to site?
<a href="#signup" class="to_register"> Create Account </a>
</p>
<div class="clearfix"></div>
<br />
<div>
<h1><i class="fa fa-paw"></i> DiGItal Society</h1>
<p>©2016 All Rights Reserved. DiGItal Society is a Web Portal for E-Society. Privacy and Terms</p>
</div>
</div>
</form>
</section>
</div>
<div id="register" class="animate form registration_form">
<section class="login_content">
<form action="insertUser.php" method="post">
<h1>Create Account</h1>
<div>
<input type="text" class="form-control" placeholder="Username" required="" name="username" />
</div>
<div>
<input type="email" class="form-control" placeholder="Email" required="" name="email" />
</div>
<div>
<input type="password" class="form-control" placeholder="Password" required="" name="password" />
</div>
<div>
<input type="hidden" name="roleid" value="">
<input type="submit" class="btn btn-default" name="insert" value="Log In">
</div>
<div class="clearfix"></div>
<div class="separator">
<p class="change_link">Already a member ?
<a href="#signin" class="to_register"> Log in </a>
</p>
<div class="clearfix"></div>
<br />
<div>
<h1><i class="fa fa-paw"></i> DiGItal Society</h1>
<p>©2016 All Rights Reserved. DiGItal Society is a Web Portal for E-Society. Privacy and Terms</p>
</div>
</div>
</form>
</section>
</div>
</div>
我还添加了我的操作页面。(不工作)。
login.php
第一种形式(无效)
<?php
include 'db.php';
if (isset($_REQUEST['insert']))
{
echo $username = $_REQUEST['username'];
echo $password = $_REQUEST['password'];
$sql = mysqli_query($conn,"SELECT * FROM `accountants` where `acc_email` = '$username' AND `acc_pass` = '$password'");
$data = mysqli_fetch_array($conn,$sql);
$_SESSION['role']=$data['roleId'];
$_SESSION['username']=$data['acc_name'];
$data = mysqli_num_rows($data);
if ($data>0)
{
header('Location: home.php');
}
else
{
header('Location: index.php');
echo 'incorrect login';
}
}
?>
和 insertUser.php
用于第二种形式。(工作)
<?php
include 'db.php';
if (isset($_REQUEST['insert']))
{
$acc_name = $_REQUEST['username'];
$acc_email = $_REQUEST['email'];
$acc_pass = $_REQUEST['password'];
$role_id = $_REQUEST['roleid'];
$sql = mysqli_query($conn,"INSERT INTO `accountants`(`acc_name`, `acc_email`, `acc_pass`, `roleId`) VALUES ('".$acc_name."','".$acc_email."','".$acc_pass."','2')");
if ($sql>0)
{
header('Location: home.php');
echo 'data added successfully';
}
$row = mysqli_query('SELECT * FROM `accountants`');
$data = mysqli_fetch_array($row);
$data = mysqli_num_rows($conn,$data);
$_SESSION['role'] = $data['roleId'];
}
?>
尝试通过表单登录重命名 <input type="submit" class="btn btn-default" name="insert" value="Sign In">
中的 name="insert"
。
因为名字相同。
例如<input type="submit" class="btn btn-default" name="login" value="Sign In">
别忘了重命名 login.php
<?php
include 'db.php';
if (isset($_REQUEST['login']))
{
echo $username = $_REQUEST['username'];
echo $password = $_REQUEST['password'];
$sql = mysqli_query($conn,"SELECT * FROM `accountants` where `acc_email` = '$username' AND `acc_pass` = '$password'");
$data = mysqli_fetch_array($conn,$sql);
$_SESSION['role']=$data['roleId'];
$_SESSION['username']=$data['acc_name'];
$data = mysqli_num_rows($data);
if ($data>0)
{
header('Location: home.php');
}
else
{
header('Location: index.php');
echo 'incorrect login';
}
}
?>
解决方法如下:试试这个:
if 条件内:
if (isset($_REQUEST['insert']))
把上面的改成这样:
if (isset($_REQUEST['login']))
PS:也更改这些:
echo $username = $_REQUEST['username'];
echo $password = $_REQUEST['password'];
至:
echo $username = $_REQUEST['user'];
echo $password = $_REQUEST['pass'];
希望对您有所帮助。
这是修复您的 login.php 页面的解决方案。
<?php
if (isset($_POST['insert']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$sql = mysqli_query($conn,"SELECT * FROM `accountants` where `acc_email` = '$username' AND `acc_pass` = '$password'");
$data = mysqli_fetch_array($conn,$sql);
$_SESSION['role']=$data['roleId'];
$_SESSION['username']=$data['acc_name'];
$data = mysqli_num_rows($data);
if ($data>0)
{
header('Location: home.php');
}
else
{
header('Location: index.php');
echo 'incorrect login';
}
}
?>
我基本上一页有2个表格。 第一个用于登录,第二个用于插入数据。 第二种形式的动作工作正常。我可以用它插入数据。 但是我为登录用户使用的是相同的表单,但它不起作用。 单击提交按钮时什么也没有发生,只是页面刷新。
请查看我的代码并帮助我解决第一个表单的操作问题。
<div class="login_wrapper">
<div id="login" class="animate form login_form">
<section class="login_content">
<form action="login.php" method="post">
<h1>Login Form</h1>
<div>
<input type="text" class="form-control" placeholder="Username" required="" name="username" />
</div>
<div>
<input type="password" class="form-control" placeholder="Password" required="" name="password" />
</div>
<div>
<input type="submit" class="btn btn-default" name="insert" value="Sign In">
<a class="reset_pass" href="#forgetpass">Lost your password?</a>
</div>
<div class="clearfix"></div>
<div class="separator">
<p class="change_link">New to site?
<a href="#signup" class="to_register"> Create Account </a>
</p>
<div class="clearfix"></div>
<br />
<div>
<h1><i class="fa fa-paw"></i> DiGItal Society</h1>
<p>©2016 All Rights Reserved. DiGItal Society is a Web Portal for E-Society. Privacy and Terms</p>
</div>
</div>
</form>
</section>
</div>
<div id="register" class="animate form registration_form">
<section class="login_content">
<form action="insertUser.php" method="post">
<h1>Create Account</h1>
<div>
<input type="text" class="form-control" placeholder="Username" required="" name="username" />
</div>
<div>
<input type="email" class="form-control" placeholder="Email" required="" name="email" />
</div>
<div>
<input type="password" class="form-control" placeholder="Password" required="" name="password" />
</div>
<div>
<input type="hidden" name="roleid" value="">
<input type="submit" class="btn btn-default" name="insert" value="Log In">
</div>
<div class="clearfix"></div>
<div class="separator">
<p class="change_link">Already a member ?
<a href="#signin" class="to_register"> Log in </a>
</p>
<div class="clearfix"></div>
<br />
<div>
<h1><i class="fa fa-paw"></i> DiGItal Society</h1>
<p>©2016 All Rights Reserved. DiGItal Society is a Web Portal for E-Society. Privacy and Terms</p>
</div>
</div>
</form>
</section>
</div>
</div>
我还添加了我的操作页面。(不工作)。
login.php
第一种形式(无效)
<?php
include 'db.php';
if (isset($_REQUEST['insert']))
{
echo $username = $_REQUEST['username'];
echo $password = $_REQUEST['password'];
$sql = mysqli_query($conn,"SELECT * FROM `accountants` where `acc_email` = '$username' AND `acc_pass` = '$password'");
$data = mysqli_fetch_array($conn,$sql);
$_SESSION['role']=$data['roleId'];
$_SESSION['username']=$data['acc_name'];
$data = mysqli_num_rows($data);
if ($data>0)
{
header('Location: home.php');
}
else
{
header('Location: index.php');
echo 'incorrect login';
}
}
?>
和 insertUser.php
用于第二种形式。(工作)
<?php
include 'db.php';
if (isset($_REQUEST['insert']))
{
$acc_name = $_REQUEST['username'];
$acc_email = $_REQUEST['email'];
$acc_pass = $_REQUEST['password'];
$role_id = $_REQUEST['roleid'];
$sql = mysqli_query($conn,"INSERT INTO `accountants`(`acc_name`, `acc_email`, `acc_pass`, `roleId`) VALUES ('".$acc_name."','".$acc_email."','".$acc_pass."','2')");
if ($sql>0)
{
header('Location: home.php');
echo 'data added successfully';
}
$row = mysqli_query('SELECT * FROM `accountants`');
$data = mysqli_fetch_array($row);
$data = mysqli_num_rows($conn,$data);
$_SESSION['role'] = $data['roleId'];
}
?>
尝试通过表单登录重命名 <input type="submit" class="btn btn-default" name="insert" value="Sign In">
中的 name="insert"
。
因为名字相同。
例如<input type="submit" class="btn btn-default" name="login" value="Sign In">
别忘了重命名 login.php
<?php
include 'db.php';
if (isset($_REQUEST['login']))
{
echo $username = $_REQUEST['username'];
echo $password = $_REQUEST['password'];
$sql = mysqli_query($conn,"SELECT * FROM `accountants` where `acc_email` = '$username' AND `acc_pass` = '$password'");
$data = mysqli_fetch_array($conn,$sql);
$_SESSION['role']=$data['roleId'];
$_SESSION['username']=$data['acc_name'];
$data = mysqli_num_rows($data);
if ($data>0)
{
header('Location: home.php');
}
else
{
header('Location: index.php');
echo 'incorrect login';
}
}
?>
解决方法如下:试试这个:
if 条件内:
if (isset($_REQUEST['insert']))
把上面的改成这样:
if (isset($_REQUEST['login']))
PS:也更改这些:
echo $username = $_REQUEST['username'];
echo $password = $_REQUEST['password'];
至:
echo $username = $_REQUEST['user'];
echo $password = $_REQUEST['pass'];
希望对您有所帮助。
这是修复您的 login.php 页面的解决方案。
<?php
if (isset($_POST['insert']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$sql = mysqli_query($conn,"SELECT * FROM `accountants` where `acc_email` = '$username' AND `acc_pass` = '$password'");
$data = mysqli_fetch_array($conn,$sql);
$_SESSION['role']=$data['roleId'];
$_SESSION['username']=$data['acc_name'];
$data = mysqli_num_rows($data);
if ($data>0)
{
header('Location: home.php');
}
else
{
header('Location: index.php');
echo 'incorrect login';
}
}
?>