Home > Back-end >  How to Select PHP Multidimensional Array items with Checkbox Form
How to Select PHP Multidimensional Array items with Checkbox Form

Time:12-11

I'm very new to PHP and just code in general, so my apologizes if my code looks like a mess.

I've created a multidimensional array and when certain checkboxes are selected and submitted through the form, I want only the selected arrays items show. Such as, John Smith's information and image only if just he is selected.

Here is my multidimensional array:

<?php
$characters = array (

  'john' => 
  array (
    'first_name' => 'John',
    'last_name' => 'Smith',
    'age' => '40',
    'image_url' => 'images/john.png',
  ),
  'jane' => 
  array (
    'first_name' => 'Jane',
    'last_name' => 'Doe',
    'age' => '30',
    'image_url' => 'images/jane.png',
  ),
  'sara' => 
  array (
    'first_name' => 'Sara',
    'last_name' => 'Johnson',
    'age' => '10',
    'image_url' => 'images/sara.png',
  )
)
?>

And here is my html form:

<h3 > Select characters to show </h3>
<form method="post">
<ul >

<!--John-->
<li >
<label for="john">John Smith </label>
<input id="john" type="checkbox" name="john">
</li>
                                        
<!--Jane-->
<li >
<label for="jane">Jane Doe </label>
 <input id="jane" type="checkbox" name="jane">
 </li>
                                        
<!--Sara-->
<li >
<label for="sara">Sara Johnson </label>
<input id="sara" type="checkbox" name="sara">                           
</li>
</ul>
                                    
<!--Button-->
<input  type="submit" value="Show Characters">
</form>

I have tried foreach and for loops, but I think I'm just doing it wrong. Any help is greatly appreciated!

CodePudding user response:

I solved your problem, please checkout the script i giving you below

html code will be like this

<form action="index.php" method="post">
<ul >
<!--John-->
<li >
<label for="john">John Smith </label>
<input id="john" type="checkbox" name="characters[]" value="john">
</li>                               
<!--Jane-->
<li >
<label for="jane">Jane Doe </label>
 <input id="jane" type="checkbox" name="characters[]" value="jane">
 </li>                            
<!--Sara-->
<li >
<label for="sara">Sara Johnson </label>
<input id="sara" type="checkbox" name="characters[]" value="sara">                           
</li>
</ul>

and php script will be like this

    <?php
$characters = array (
    'john' => 
    array (
      'first_name' => 'John',
      'last_name' => 'Smith',
      'age' => '40',
      'image_url' => 'images/john.png',
    ),
    'jane' => 
    array (
      'first_name' => 'Jane',
      'last_name' => 'Doe',
      'age' => '30',
      'image_url' => 'images/jane.png',
    ),
    'sara' => 
    array (
      'first_name' => 'Sara',
      'last_name' => 'Johnson',
      'age' => '10',
      'image_url' => 'images/sara.png',
    )
    );

if(isset($_POST['submit'])){
    $selectedCharacter = $_POST['characters'];
    foreach ($selectedCharacter as $Character) {
        echo "first_name:- ".$characters[$Character]['first_name'].'<br>';
        echo "last_name:- ".$characters[$Character]['last_name'].'<br>';
        echo "age:- ".$characters[$Character]['age'].'<br>';
        echo "image_url:- ".$characters[$Character]['image_url'].'<br><br>';
    }
}
?>

refer to this channel https://www.youtube.com/channel/UCnqWFcMpFA_dsxEzt7lKxJA

  • Related