mardi 7 janvier 2020

Keep checkbox unchecked after click uncheck and checkbox checked after click check

I know this is around the internet but i tried it and it doesn't really work. It works when i check a checkbox but when i uncheck a checkbox it IS CHECKED again after submit? This is my code.

<input type="checkbox" name="services[]" value="Park" <?php

The next line below is for loading the data (works perfect)

echo ($extraServices == 'Park') ? "checked='checked'" : (($extraServices == 'Park,Electricity')  ? "checked='checked'" : "");

Here begins what doesn't work

if(isset($_POST['services'])) {
if(in_array("Park", $_POST['services'])) {
       echo "checked='checked'";
       }
     }
?>> Parkeren

It's in an array because i have 1 more checkbox below

<input style="margin-left:20px;" type="checkbox" name="services[]" value="Electricity" <?php
echo ($extraServices == 'Electricity') ? "checked='checked'" : (($extraServices == 'Park,Electricity')  ? "checked='checked'" : "");

if(isset($_POST['services'])) {
if (in_array("Electricity", $_POST['services'])) {
 echo "checked='checked'";

}}
?>> Elektriciteit

How will it also work with two checkboxes? So Checking works but unchecking after i unchecked it and submit will result in still a checked checkbox. And how will it work with two checkboxes, (in_array("Park AND Electricity)

Thanks for your help




Aucun commentaire:

Enregistrer un commentaire