如何隐藏在提交表格,并显示在同一时间,结果呢?

问题描述 投票:0回答:2

我想提出一个形式,但我不喜欢的工作,我希望它。这是代码:

<?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按钮的结果应该隐藏。但现在形式不给出结果,也不躲,但一旦它被提交刚刚复位。

php html5 forms show-hide
2个回答
0
投票

您的提交按钮没有name属性集,因此不会发送任何东西,你可以在$ _ POST检查。所以加name="submit"到该按钮:

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

为了使“Opnieuw”按钮做一些事情的形式包裹,相同的脚本(不包含任何值)


0
投票

你可以简单地实现这一点使用$ _POST数组,只要有发送的数据它存在。

if (isset($_POST)) {

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

else {

    //echo your form

}

没必要用一个“$ display_form”变量。

你也不需要设置一个名称为提交的输入,但因为它是一个很好的做法,对于每个输入的话,我建议你无论如何都要做到这一点设置一个名称。

© www.soinside.com 2019 - 2024. All rights reserved.