简体   繁体   中英

echo out multiple text box values Using php

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.

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.

Try This

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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