dongzan2740 2016-10-19 10:07
浏览 38
已采纳

PHP仅在提交表单之前注意到未定义的索引

I am trying to do a simple value check from a form and I want to assign some variables the values entered by the user. I have done this form :

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
                <label for="raspuns1"><span class="glyphicon glyphicon-pencil"></span> Raspuns subpunct a</label>
                <input type="text" class="form-control" name="raspuns1" placeholder="Raspuns a"><br>
                <label for="raspuns2"><span class="glyphicon glyphicon-pencil"></span> Raspuns subpunct b</label>
                <input type="text" class="form-control" name="raspuns2" placeholder="Raspuns b"><br>
                <label for="raspuns3"><span class="glyphicon glyphicon-pencil"></span> Raspuns subpunct c</label>
                <input type="text" class="form-control" name="raspuns3" placeholder="Raspuns c"><br>
                <label for="raspuns4"><span class="glyphicon glyphicon-pencil"></span> Raspuns subpunct d</label>
                <input type="text" class="form-control" name="raspuns4" placeholder="Raspuns d"><br>
                <button type="submit" class="btn btn-success glyphicon glyphicon-check"> Verifica răspunsurile</button>
    </form>

and this is the php code

<?php 
$corect1="raspunsulcorect";
$corect2="raspunsulcorect";
$corect3="raspunsulcorect";
$corect4="raspunsulcorect";

if(isset($_POST['submit']))
{
$raspuns1=$_POST['raspuns1'];
$raspuns2=$_POST['raspuns2'];
$raspuns3=$_POST['raspuns3'];
$raspuns4=$_POST['raspuns4'];
}

?>

The problem is that when I open the page, it shows me

Notice: Undefined index: raspuns1 in A:\XAMPP\htdocs\Copie\Teste\test1-geometrie.php on line 5

Notice: Undefined index: raspuns2 in A:\XAMPP\htdocs\Copie\Teste\test1-geometrie.php on line 6

Notice: Undefined index: raspuns3 in A:\XAMPP\htdocs\Copie\Teste\test1-geometrie.php on line 7

Notice: Undefined index: raspuns4 in A:\XAMPP\htdocs\Copie\Teste\test1-geometrie.php on line 8

After pressing the submit button, everything is okay, but if the page is opened the first time, this will be shown. What am I doing wrong? That first if should not let the program check the variables

  • 写回答

1条回答 默认 最新

  • donglu5041 2016-10-19 10:37
    关注

    Go ahead and add a method which checks if this is a POST request before accessing the $_POST superglobal.

    Example:

    function isPost() {
      return strtolower($_SERVER['REQUEST_METHOD']) == 'post';
    }
    

    In your code (assuming you have defined the above function):

    if(isPost() && isset($_POST['submit']))
    {
    $raspuns1=$_POST['raspuns1'];
    $raspuns2=$_POST['raspuns2'];
    $raspuns3=$_POST['raspuns3'];
    $raspuns4=$_POST['raspuns4'];
    }
    

    Also check the filter_input and filter_var functions when fetching values from the $_GET and $_POST superglobals.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答