Php 单击“提交”后无法输出数据
我有3个php文件: 第一次登录-form.phpPhp 单击“提交”后无法输出数据,php,Php,我有3个php文件: 第一次登录-form.php <?php if(isset($_POST['user']) && isset($_POST['password'])) { if(!empty($_POST['user']) && !empty($_POST['password'])) { $user=$_POST['user']; $password=$_POST['password']; header('loc
<?php
if(isset($_POST['user']) && isset($_POST['password']))
{
if(!empty($_POST['user']) && !empty($_POST['password']))
{
$user=$_POST['user'];
$password=$_POST['password'];
header('location:index-test.php');
}
}
?>
<form action='index-test.php' method='post'>
Username <input type='text' name='user'/><br>
Password <input type='text' name='password'/><br>
<input type='submit' name='submit' value='submit'/>
</form>
用户名
密码
第二个索引-test.php
<?php
require 'core-test.php';
if(loggedin())
{
echo 'ok';
}
else
{
include 'login-form.php';
}
?>
3rd core-test.php
<?php
function loggedin()
{
if(isset($user) && isset($password))
{
return true;
}
else
{
return false;
}
}
?>
当我输入index.php时,它会输出login-from.php,因为我还没有输入数据。但是当我输入数据并单击提交时,它仍然会显示登录表单,而不是消息“ok”。我错在哪里?在core-test.php中,您需要
$\u POST['user']
而不是$user
;密码也是如此。我想你的意思是将login form.php
中的表单发布到login form.php
本身。如果您像之前那样将其发布到index test.php
,则不会设置$user
和$password
您可以按照其他人给出的步骤操作,或者只更改表单的action=
属性,并保留现有代码
你需要做的唯一一件事就是改变这条线
<form action='index-test.php' method='post'>
对此
<form action='login-form.php' method='post'>
宾果
希望这有帮助。您需要在
loggedin()中获取$user
和$password
,使用它才能工作!。但是我不明白为什么它没有收到赋值$name和$password?这在古代的PHP版本中可能是有效的,但是现在你需要使用_get和_postsuperglobals,看看并使用$\u POST['user']
和$\u POST['password']
而不是core test.php
中的$user
和$password
。感谢@faffaff-之前没有注意到你的答案。