Solution 1 :

Boolean doesn’t display anything, you I’ll need to :

$boolean === true ? “True”:”false”

In order to display text instead of your Boolean.

Edit : my bad I half read the code, I saw checkbox type so I expected value to be true/false. So you’re right the text should display correctly with your code.

Solution 2 :

Try This

if (isset($_POST['sports'])){
     $sports = $_POST['sports'];
  foreach($sports as $sport)
  {
  echo $sport;
  }
  }

Problem :

This is my first time posting so any feedback on my question would be appreciated.

I am trying to echo out the value of a text box form using php.

This is my html code:

<form action="exercise-3.php" method="POST">

    <div class="input-container">
         <label><input type="checkbox" name="sports[]" id="sports" value="soccer"> Soccer</label>
         <label><input type="checkbox" name="sports[]" id="sports" value="football"> Football</label>
         <label><input type="checkbox" name="sports[]" id="sports" value="tennis"> Tennis</label>
         <label><input type="checkbox" name="sports[]" id="sports" value="swimming"> Swimming</label>
    </div>

Above is only the relevant part of my form code.

Below is my php code to echo out the values:

if (isset($_POST['sport'])){
     $sport = $_POST['sport'];
}

<p>
     <strong>sports:</strong> 
     <?php if(!empty($_POST['sport'])){
         foreach($_POST['sport'] as $selected){
             echo $selected."</br>";
         }
     }?>
</p>

When I debug it, it throws no errors but nothing comes up. not sure what I am doing wrong.

Comments

Comment posted by PHPology

In your html you called it’s sports, but in PHP you are referring to sport.

Comment posted by M. Eriksson

Side note:

Comment posted by SPlatten

Try print_r($_POST[‘sports’]); that will dump the entire content.

Comment posted by M. Eriksson

Why would they be booleans when the checkboxes has values?

By