0

I am beginner in PHP and I want to check whether user has filled the input named "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!" 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).

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;
      }
    ?>
1
  • It works for me, what is your error? Commented Feb 26, 2015 at 11:53

2 Answers 2

2

As @jylipaa pointed out you're echoing $chybi before setting it's value. Move your logic above the $zacatek varaible.

<?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;
?>
Sign up to request clarification or add additional context in comments.

Comments

1

you are setting $zacatek in the start of code where $chybi is still empty. It is then handled as a string and setting the value of $chybi later on will not change the content of a string afterwards.

1 Comment

Thank you, so my sollution won't work, then. I can divide my HTML code stored in $zacatek into more variables and insert the error message between them, though I hoped that I can store it in one variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.