表单按钮打印错误,未提交


Form button printing error without submitting

当我第一次进入页面时,我什么也没做,我在下面收到此错误。我不明白为什么,因为我正在检查是否单击了提交按钮。

注意:未定义的索引:用户名在/Applications/XAMPP/xamppfiles/htdocs/kwame.php 第 3 行

<?
$user_name = $_POST['username'];
$submit = isset($_POST['submit']);

if($submit){
echo $user_name;
}
else {
echo 'leave';
}
?>

<html>
<head>
<title> My Php Exercises </title>
</head>
<body>
<FORM name='login' method='POST' action='kwame.php'>
<input type='text' name='username' >
<input type='submit' name='submit'>

</FORM>
</body>
</html>

你没有正确打开你的php。用:

<?php
if(isset($_POST['submit'])){
$user_name = $_POST['username'];
echo $user_name;
}
else {
echo 'leave';
}
?>

php 数组变量在密钥数据传输时给出警告消息。您应该检查是否为此定义了变量。

确定变量是否设置为 http://php.net/manual/en/function.isset.php

<?php
if(isset($_POST['submit']) && isset($_POST['username'])){
   echo $_POST['username'];
 }else {
    echo 'leave';
 }
?>

你应该使用

if(isset($_POST['username']))
    {$user_name = $_POST['username'];}
else
    {$user_name = "";}
instread 

或者你可以简单地将你的行移动到那个位置:

if($submit)
    {$user_name = $_POST['username'];
    ....
    }   
相关文章: