简体   繁体   English

用PHP进行表单验证-不显示错误消息

[英]Form validating in PHP - error message not displayed

I am beginner in PHP and I want to check whether user has filled the input named "jmeno". 我是PHP的初学者,我想检查用户是否填写了名为“ jmeno”的输入。 Name of the corresponding variable is the same. 相应变量的名称相同。 If the input is not entered, then the variable "chybi" should be expanded by text "Zadej jméno!" 如果未输入输入,则变量“ chybi”应扩展为文本“ Zadejjméno!”。 and that text should appear above the form. 该文本应显示在表格上方。

I am getting no errors. 我没有任何错误。 If the input is filled, then the form proceeds. 如果输入内容已填写,则表单继续进行。 If not, then the form doesn't proceed - that works how it's supposed to. 如果没有,则表单不会继续进行-可以按照预期进行。 But the error message in variable "chybi" doesn't display for some unknown reason in case that the variable "jmeno" is empty (second if). 但是,如果变量“ jmeno”为空(第二个if),则出于某些未知原因,不会显示变量“ chybi”中的错误消息。

I have tried many things. 我已经尝试了很多东西。 It's strange that such a simple script doesn't work. 如此简单的脚本无法正常工作很奇怪。 Any ideas? 有任何想法吗? Thank you. 谢谢。

    <?php
      $chybi = '';
      $zacatek = '
        <p>some long text</p>
        <form action="index.php" method="post" class="akce">
          <p>' .$chybi.
          '<input type="text" name="jmeno" placeholder="Zadej své jméno," /></p>
          <p>
            vyber pohlaví<br />
            <input type="radio" name="pohlavi" value="žena" /> žena<br />
            <input type="radio" name="pohlavi" value="muž" />  muž
          </p>
          <p>a pokud se nebojíš, <input type="submit" value="vstup!" /></p>
        </form>
      ';
      if ( isset($_POST['jmeno']) && isset($_POST['pohlavi']) ) {
        $jmeno = $_POST['jmeno'];
        $pohlavi = $_POST['pohlavi'];
        if ( empty($jmeno) ) {
          $chybi .= 'Zadej jméno!<br />';
          echo $zacatek;
        }
        else {
          echo "Jmenuješ se $jmeno a jsi $pohlavi.";
        }          
      }
      else {
        echo $zacatek;
      }
    ?>

As @jylipaa pointed out you're echoing $chybi before setting it's value. 正如@jylipaa指出的那样,您在设置值之前回显了$ chybi。 Move your logic above the $zacatek varaible. 将逻辑移到$ zacatek变量上方。

<?php
  $chybi = '';
  if ( isset($_POST['jmeno']) && isset($_POST['pohlavi']) ) {
    $jmeno = $_POST['jmeno'];
    $pohlavi = $_POST['pohlavi'];
    if ( empty($jmeno) ) {
      $chybi .= 'Zadej jméno!<br />';
    }
    else {
      echo "Jmenuješ se $jmeno a jsi $pohlavi.";
    }          
  }
  $zacatek = '
    <p>some long text</p>
    <form action="index.php" method="post" class="akce">
      <p>' .$chybi.
      '<input type="text" name="jmeno" placeholder="Zadej své jméno," /></p>
      <p>
        vyber pohlaví<br />
        <input type="radio" name="pohlavi" value="žena" /> žena<br />
        <input type="radio" name="pohlavi" value="muž" />  muž
      </p>
      <p>a pokud se nebojíš, <input type="submit" value="vstup!" /></p>
    </form>
  ';

echo $zacatek;
?>

you are setting $zacatek in the start of code where $chybi is still empty. 您在$ chybi仍然为空的代码开头设置$ zacatek。 It is then handled as a string and setting the value of $chybi later on will not change the content of a string afterwards. 然后将其作为字符串处理,稍后设置$ chybi的值将不会在以后更改字符串的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM