How to get an error message on both field responses: "0"

I’m trying to understand that if the user selects “0” in both fields in order to get a pop-up error message and say that at least one of them must have a filled amount.

As you can see below: the form fields have one error associated with it, that is, $maleError $femaleErr...

I wrote something like this:

   if (($_POST["male"] === "") && ($_POST["female"] === "")) {
  $quantityErr = "Please fill at least one of the fields, either male or female."

}

And he added $quantityErrto the form fields similar to this one: (it does not work for what it costs. The page simply crosses out.

    <div class="field">
      <label>* Number of People</label>
      <select class="ui dropdown" name="female">
        <option value="">Gender Female</option>
        <option <?php if ($female === 0 ) echo 'selected' ; ?> value="0">0</option>
        <option <?php if ($female == 1 ) echo 'selected' ; ?> value="1">1</option>
        <option <?php if ($female == 2 ) echo 'selected' ; ?> value="2">2</option>
        <option <?php if ($female == 3 ) echo 'selected' ; ?> value="3">3</option>
        <option <?php if ($female == 4 ) echo 'selected' ; ?> value="4">4</option>
        <option <?php if ($female == 5 ) echo 'selected' ; ?> value="5">5</option>
        <option <?php if ($female == 6 ) echo 'selected' ; ?> value="6">6</option>
        <option <?php if ($female == 7 ) echo 'selected' ; ?> value="7">7</option>
        <option <?php if ($female == 8 ) echo 'selected' ; ?> value="8">8</option>
        <option <?php if ($female == 9 ) echo 'selected' ; ?> value="9">9</option>
        <option <?php if ($female == 10 ) echo 'selected' ; ?> value="10">10</option>
      </select>
      <?php if(isset($femaleErr)) print ('<span class="error">* ' . $femaleErr || $quantityErr . '</span>'); ?>
    </div>

Fields of the current form unchanged as described above

        <div class="field">
          <label>* Number of People</label>
          <select class="ui dropdown" name="male">
            <option value="">Gender Male</option>
            <option <?php if ($male === 0 ) echo 'selected' ; ?> value="0">0</option>
            <option <?php if ($male == 1 ) echo 'selected' ; ?> value="1">1</option>
            <option <?php if ($male == 2 ) echo 'selected' ; ?> value="2">2</option>
            <option <?php if ($male == 3 ) echo 'selected' ; ?> value="3">3</option>
            <option <?php if ($male == 4 ) echo 'selected' ; ?> value="4">4</option>
            <option <?php if ($male == 5 ) echo 'selected' ; ?> value="5">5</option>
            <option <?php if ($male == 6 ) echo 'selected' ; ?> value="6">6</option>
            <option <?php if ($male == 7 ) echo 'selected' ; ?> value="7">7</option>
            <option <?php if ($male == 8 ) echo 'selected' ; ?> value="8">8</option>
            <option <?php if ($male == 9 ) echo 'selected' ; ?> value="9">9</option>
            <option <?php if ($male == 10 ) echo 'selected' ; ?> value="10">10</option>
          </select>
          <?php if(isset($maleErr)) print ('<span class="error">* ' . $maleErr . '</span>'); ?>
        </div>


        <div class="field">
          <label>* Number of People</label>
          <select class="ui dropdown" name="female">
            <option value="">Gender Female</option>
            <option <?php if ($female === 0 ) echo 'selected' ; ?> value="0">0</option>
            <option <?php if ($female == 1 ) echo 'selected' ; ?> value="1">1</option>
            <option <?php if ($female == 2 ) echo 'selected' ; ?> value="2">2</option>
            <option <?php if ($female == 3 ) echo 'selected' ; ?> value="3">3</option>
            <option <?php if ($female == 4 ) echo 'selected' ; ?> value="4">4</option>
            <option <?php if ($female == 5 ) echo 'selected' ; ?> value="5">5</option>
            <option <?php if ($female == 6 ) echo 'selected' ; ?> value="6">6</option>
            <option <?php if ($female == 7 ) echo 'selected' ; ?> value="7">7</option>
            <option <?php if ($female == 8 ) echo 'selected' ; ?> value="8">8</option>
            <option <?php if ($female == 9 ) echo 'selected' ; ?> value="9">9</option>
            <option <?php if ($female == 10 ) echo 'selected' ; ?> value="10">10</option>
          </select>
          <?php if(isset($femaleErr)) print ('<span class="error">* ' . $femaleErr . '</span>'); ?>
        </div>

Ongoing error checking unchanged as above

   if ($_POST["male"] === "") {
     $maleErr = "# of people (gender male) required";
   } else {
     $male = test_input($_POST["male"]);
   }

    if ($_POST["female"] === "") {
     $femaleErr = "# of people (gender female) required";
   } else {
     $female = test_input($_POST["female"]);
   }

Advanced Editing

? , , , , , .

  if (($_POST["male"] == 0) && ($_POST["female"] == 0)) {
      $quantityErr = "Please fill at least one of the fields, either male or female.";
  }

, : ( )

<?php if(isset($femaleErr)) print ('<span class="error">* ' . $femaleErr . '</span>'); ?>
<?php if(isset($quantityErr)) print ('<span class="error">* ' . $quantityErr . '</span>'); ?>
+4
2

-, , . , , , . $femaleErr $quantityErr isset :

<?php if(isset($femaleErr)) print ('<span class="error">* ' . $femaleErr || $quantityErr . '</span>'); ?>

$femaleErr - undefined, , . ( , ).

, , , ||. print() . , 1 0 (true false), , , : print('stuff'. ($varone || $vartwo) .'otherstuff') // prints stuff1otherstuff.

, :

$msg = array();
if( isset($femaleErr) )
    $msg[] = $femaleErr;

if( isset($quantityErr ) )
    $msg[] = $quantityErr ;

foreach($msg as $msg)
{
   echo '<span class="error">* '.$msg.'</span>';
}

, , , . foreach . , . foreach ( ).

0
  <?php if(isset($femaleErr)) print ('<span class="error">* ' . $femaleErr || $quantityErr . '</span>'); ?>

$quantityErr, $femaleErr. .

, ,

  <?php if(isset($femaleErr)) print ('<span class="error">* ' . $femaleErr . '</span>'); ?>
  <?php if(isset($quantityErr)) print ('<span class="error">* ' . $quantityErr . '</span>'); ?>
0

Source: https://habr.com/ru/post/1612834/


All Articles