0

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 class="form__heading"> Select characters to show </h3>
<form method="post">
<ul class="form__items">

<!--John-->
<li class="form__item">
<label for="john">John Smith </label>
<input id="john" type="checkbox" name="john">
</li>
                                        
<!--Jane-->
<li class="form__item">
<label for="jane">Jane Doe </label>
 <input id="jane" type="checkbox" name="jane">
 </li>
                                        
<!--Sara-->
<li class="form__item">
<label for="sara">Sara Johnson </label>
<input id="sara" type="checkbox" name="sara">                           
</li>
</ul>
                                    
<!--Button-->
<input class="form__button" 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!

5
  • So if you select two list items, only those two's info should be shown? Commented Dec 8, 2021 at 5:05
  • Yes! That is what I am hoping to do. Commented Dec 8, 2021 at 5:11
  • So what's the issue that you are facing with that? Commented Dec 8, 2021 at 5:17
  • I can't figure out the PHP to make the selected list items show when submitting the form. Commented Dec 8, 2021 at 5:20
  • Please post your PHP script (apart from the array) - at least show what you have attempted - you mentioned you have tried foreach /looping Commented Dec 8, 2021 at 5:37

1 Answer 1

1

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 class="form_items">
<!--John-->
<li class="form_item">
<label for="john">John Smith </label>
<input id="john" type="checkbox" name="characters[]" value="john">
</li>                               
<!--Jane-->
<li class="form__item">
<label for="jane">Jane Doe </label>
 <input id="jane" type="checkbox" name="characters[]" value="jane">
 </li>                            
<!--Sara-->
<li class="form_item">
<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

Sign up to request clarification or add additional context in comments.

3 Comments

Hey, this is a good answer! As OP is new to coding in general, I figured I would explain a bit about what you have done. The names of the checkboxes have been changed to 'characters[]' this means the posted data will be treated as an array. CodeAlgo has then set the character name as a value in that array. In the PHP script, the foreach loop is stepping through that array, using the value as a key to select data from your pre existing $characters array.
Thank you both so much! This worked for me, I appreciate the help setting me in the right direction. My little project may not look the greatest but at least now it works! :)
@n1021 please mark accepted if this works for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.