0

I have an html code as shown below. The following html code displays list of checkboxes as shown in the screenshot below the code.

 <?php      
        $output['toggle_multi_tiles']=$_POST['toggle_multi_tiles'];  

        $output['episode_status']=$_POST['episode_status'];

        $fp = fopen('../feeds/ptp-ess_landing.json', 'w');
        fwrite($fp, json_encode($output));
        fclose($fp);
        logActivity();

        if(file_exists('../feeds/ptp-ess_landing.json')){
        $data = json_decode(file_get_contents('../feeds/ptp-ess_landing.json'));
        }
 ?>

 <?php if($data){
 ?>
    <fieldset style="background-color:darkseagreen;">
       <input type="checkbox" id="ptp" value="0" name="toggle_multi_tiles[]" <?php if($data->{"toggle_multi_tiles[]"}==0){echo
          'checked';}?>>
       <label for="toggle-multi-off">PTP</label>
       <input type="checkbox" id="l'e" value="1" name="toggle_multi_tiles[]" <?php if($data->{"toggle_multi_tiles[]"}==1){echo
          'checked';}?>>
       <label for="position-one">L'E</label>
       <div>
          <button type="submit">Save</button>   //Line A
       </div>
    </fieldset>
<?php }  ?>                 

enter image description here

On hitting save button at Line A after selecting the first 2 check-boxes from the screenshot above , everything get save in JSON as shown below:

{"toggle_multi_tiles":["0","1"]}


Problem Statement:

The issue which I am having right now is after saving the 2 check-boxes, the only check-box which display on page refresh is the 1st one (not both).

($data->{"toggle_multi_tiles[]"}==0) from the html code is reading from JSON.

($data->{"toggle_multi_tiles[]"}==1) from the html code is reading from JSON.

enter image description here

13
  • What do you think $data->{"toggle_multi_tiles[]"}==1 does? Commented Jul 31, 2019 at 21:21
  • Check the problem statement. It reads from JSON. Commented Jul 31, 2019 at 21:21
  • I would like you to explain the logic of this statement. It does not read JSON. It compares a property of an object to an integer 1. Rubber duck debugging is usually a very good method. Commented Jul 31, 2019 at 21:24
  • Where do you see the values {"toggle_multi_tiles":["0","1"]}? In browser's console or in PHP? Commented Jul 31, 2019 at 21:26
  • On page refresh, why only first checkbox remains selected ? Why not both (1st and 2nd) ? Commented Jul 31, 2019 at 21:26

1 Answer 1

1

The key in your object is toggle_multi_tiles, not toggle_multi_tiles[], so $data->{"toggle_multi_tiles[]"} should be $data->toggle_multi_tiles.

The value of this property is an array of strings, you can use in_array() to test whether a value is in it.

<input type="checkbox" id="ptp" value="0" name="toggle_multi_tiles[]" <?php if(in_array("0", $data->toggle_multi_tiles)){echo
  'checked';}?>>
<label for="toggle-multi-off">PTP</label>
<input type="checkbox" id="l'e" value="1" name="toggle_multi_tiles[]" <?php if(in_array("1", $data->toggle_multi_tiles)){echo
  'checked';}?>>
Sign up to request clarification or add additional context in comments.

5 Comments

That won't work. Only the values of the checked boxes are sent, so they won't be in specific indexes in the array.
If you only check box 1, the array will be ["1"].
Actually its working. But I wanted to check if its the correct approach.
It's not the correct approach, I doubt it's really working in all cases.
You are right. Its not working in all case. Whereas your code is working in all cases.

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.