【问题标题】:How do I hide a form on submission and show the result at the same time?如何在提交时隐藏表单并同时显示结果?
【发布时间】:2019-02-09 13:17:08
【问题描述】:

我正在制作一个表格,但我没有按照我的意愿工作。这是代码:

<?php
  $output = '';
  $display_form = True;
  //echo $display_form;
  //Once the form is submitted it should be hidden so first I set $display_form to false at line 19.
  //Then I do all the logic to get the propper result at line 21-39. And echo the result at line 40-43.
  if(isset($_POST['submit'])){
    $display_form = False;

    if (!is_numeric($_POST['mark1'])) {
      $mark1 = $_POST['mark1'];

      $output = 'De invoer van Getal 1 was foutief: '.$mark1;
    }if(!is_numeric($_POST['mark2'])) {
      $mark2 = $_POST['mark2'];

      $output.= '<br>De invoer van Getal 2 was foutief '.$mark2;
    }elseif(is_numeric($_POST['mark1']) && is_numeric($_POST['mark2'])){
        $mark1 = str_replace(',', '.', $_POST['mark1']);
        $mark2 = str_replace(',', '.', $_POST['mark2']);
        $result = $mark1+$mark2;

        $mark1_2 = str_replace('.', ',', $mark1);
        $mark2_2 = str_replace('.', ',', $mark2);
        $result_2 = str_replace('.', ',', $result);

        $output = $mark1_2.' + '.$mark2_2. ' = '.$result_2.'<br>';
    }?>
    <h1>
      <?php echo $output ?>
    </h1>
    <button type="reset" value="Opnieuw">Opnieuw</button>
    <?php
  }

  //If the form isn't submitted $display_form is True so it should show this HTML
  if($display_form){ ?>
    <h1>Optel rekenmachine</h1>
    <form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
      Getal 1: <input type="text" name="mark1"><br>
      Getal 2: <input type="text" name="mark2"><br>
      <button type="submit" value="Optellen!"class="form">Optellen!</button><button type="reset" value="Leegmaken">Leegmaken</button>
    </form>
<?php }
?>

我希望它显示表单,但是一旦提交,我希望表单隐藏并显示结果。当显示结果时,会有一个“Opnieuw”按钮。单击后,表单应再次显示,并且带有 opnieuw 按钮的结果应隐藏。但是现在表单既不给出结果也不隐藏,只是在提交后重置。

【问题讨论】:

  • 缺少按钮中的name="submit",因此不会设置$_POST['submit'] -> 将其设为&lt;button type="submit" name="submit" value="Optellen!" class="form"&gt;
  • @Jeff Thx 现在隐藏了!但是“Opnieuw”按钮仍然不起作用...

标签: php html forms show-hide


【解决方案1】:

您的提交按钮没有设置名称属性,因此不会发送您可以在 $_POST 中检查的任何内容。所以将name="submit" 添加到该按钮:

<button type="submit" name="submit" value="Optellen!" class="form">

为了让“Opnieuw”按钮做一些事情,把它包装成一个表单到同一个脚本(没有任何值)

【讨论】:

  • 谢谢它现在可以工作了!我需要添加&lt;a href="PO12%20(v.2).php?display_form=true"&gt;Opnieuw&lt;/a&gt; 才能使“Opnieuw”按钮起作用。
  • 很高兴我能帮上忙!如果它解决了您的问题,您可以接受答案。
  • ..?display_form=true 不会做任何事情(现在其余代码的方式),因为您不检查 $_GET['display_form']
【解决方案2】:

您可以使用 $_POST 数组简单地实现此目的,该数组仅在发送数据时才存在。

if (isset($_POST)) {

    //do your stuff with the data from the form so here basically echo $output

else {

    //echo your form

}

无需使用“$display_form”变量。

您也不需要为提交输入设置名称,但由于为每个输入设置名称是一种很好的做法,所以我建议您无论如何都必须这样做。

【讨论】:

    猜你喜欢
    • 2013-08-29
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 2017-08-30
    • 1970-01-01
    • 2014-11-24
    • 2019-05-05
    相关资源
    最近更新 更多