简体   繁体   中英

How can I set individual variables in a PHP loop from form?

For example: I have a basic HTML form as shown below:

<div class="form-group">
    <label class="control-label" for="checkboxes" name="industry">How would you classify your business?</label>

    <div class="checkbox">
        <label for="checkboxes-0">
            <input type="checkbox" name="checkboxes" id="checkboxes-0">
            Nonprofit
        </label>
    </div>
    <div class="checkbox">
        <label for="checkboxes-1">
            <input type="checkbox" name="checkboxes" id="checkboxes-1">
            Service
        </label>
    </div>

This is just a snippet it's not the complete form

I want to parse the multi-select form into variables in PHP. For each name in the form such as "Nonprofit", "Service", if the user selects "Nonprofit" for example, I want to create a variable for nonprofit and set it to 1 and have the variable for "service" equal to 0.

I understand that I have to use a loop like this

if(isset($_POST['submit'])) {
    $industry = $_POST['checkboxes'];
}

But how do I loop through the labels and set the variables that the user selects to 1 and have the rest equal to 0?

One way. First define a hidden input for each that can be overridden by the checkbox and each needs a name and value :

    <input type="hidden" name="Nonprofit" value="0">
    <input type="checkbox" name="Nonprofit" id="checkboxes-0" value="1">
    <input type="hidden" name="Service" value="0">
    <input type="checkbox" name="Service" id="checkboxes-1" value="1">

Then you can access $_POST['Nonprofit'] which will have a value of 0 or 1 etc...

Another way, knowing what can be submitted is to submit an array:

    <input type="checkbox" name="check[Nonprofit]" id="checkboxes-0" value="1">
    <input type="checkbox" name="check[Service]" id="checkboxes-1" value="1">

Then merge what is submitted with an array of 0 :

$checks = ['Nonprofit' => 0, 'Service' => 0];
$checks = array_merge($checks, $_POST['check']);

Then you can access $checks['Nonprofit'] which will have a value of 0 or 1 etc...

You can achieve the desired output by changing few things in your html and making an array of user selected options by which we can iterate it.

HTML

<form method="post">
<div class="form-group">
 <label class="control-label" for="checkboxes" name="industry">How would you classify your business?</label>
<div class="checkbox">
 <label for="checkboxes-0">
     <input type="checkbox" name="nonprofit" id="checkboxes-0" value="nonprofit">
     Nonprofit
 </label>
</div>
<div class="checkbox">
<label for="checkboxes-1">
   <input type="checkbox" name="service" id="checkboxes-1" value="service">
     Service
</label>
</div>
   <button type="submit" name="submit">Submit</button>
</div>
</form>

PHP

if (isset($_POST['submit'])) {
 foreach ($_POST as $key => $value) {
  if ($value != null) {
   $result[] = $value; 
   }
  }
   foreach ($result as $value1) {
    $values[] = $value1; 
   }
 
 foreach ($values as $value2) {
  echo '$'.$value2.'= 1<br>';
 }
}

Output

If user selects Nonprofit

$nonprofit= 1

If Service is selected

$service= 1

If both are selected

$nonprofit= 1
$service= 1

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