It seems like a solved problem but I've done through research on this for hours but all the solutions that I tried didn't work for me. Please help me I am imploding and tired...I'd like to keep ONLY the checked ones checked after submitting the form or if any errors occur. Here is my problem.
<input name="toppings" type="checkbox" value="pepperoni" id="top1"><label
for="top1">Pepperoni</label>
<input name="toppings" type="checkbox" value="bacon" id="top2"><label
for="top2">Canadian Bacon</label>
<input name="toppings" type="checkbox" value="sausage" id="top3"><label
for="top3">Sausage</label>
<input name="toppings" type="checkbox" value="mushrooms" id="top4"><label
for="top4">Mushrooms</label>
<input name="toppings" type="checkbox" value="pineapple" id="top5"><label
for="top5">Pineapple</label>
<input name="toppings" type="checkbox" value="peppers" id="top6"><label
for="top6">Peppers</label>
And This is what I tried
$toppingArr=array();
if(!empty($_GET["toppings"]))
{
foreach($_GET['toppings'] as $tops)
{
array_push($toppingArr,$tops);
}
}
<input name="toppings[]" type="checkbox" value="pepperoni" id="top1"
<?= (in_array("pepperoni", $toppingArr)) ? 'checked' : ''; ?>>
<label for="top1">Pepperoni</label>
<input name="toppings[]" type="checkbox" value="bacon" id="top2"
<?= (in_array("bacon", $toppingArr)) ? 'checked' : ''; ?>>
<label for="top2">Canadian Bacon</label>
<input name="toppings[]" type="checkbox" value="sausage" id="top3">
<?= (in_array("sausage", $toppingArr)) ? 'checked' : ''; ?>>
<label for="top3">Sausage</label>
<input name="toppings[]" type="checkbox" value="mushrooms" id="top4"
<?= (in_array("mushrooms", $toppingArr)) ? 'checked' : ''; ?>>
<label for="top4">Mushrooms</label>
<input name="toppings[]" type="checkbox" value="pineapple" id="top5"
<?= (in_array("pineapple", $toppingArr)) ? 'checked' : ''; ?>>
<label for="top5">Pineapple</label>
<input name="toppings[]" type="checkbox" value="peppers" id="top6"
<?= (in_array("peppers", $toppingArr)) ? 'checked' : ''; ?>>
<label for="top6">Peppers</label>
Try this
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
$toppingArr=array();
if(!empty($_GET["toppings"]))
{
foreach($_GET['toppings'] as $tops)
{
array_push($toppingArr,$tops);
}
}
?>
<form>
<input name="toppings[]" type="checkbox" value="pepperoni" id="top1" <?php if(in_array('pepperoni', $toppingArr)) echo 'checked'; ?> ><label
for="top1">Pepperoni</label>
<input name="toppings[]" type="checkbox" value="bacon" id="top2" <?php if(in_array('bacon', $toppingArr)) echo 'checked'; ?>><label
for="top2">Canadian Bacon</label>
<input name="toppings[]" type="checkbox" value="sausage" id="top3" <?php if(in_array('sausage', $toppingArr)) echo 'checked'; ?>><label
for="top3">Sausage</label>
<input name="toppings[]" type="checkbox" value="mushrooms" id="top4" <?php if(in_array('mushrooms', $toppingArr)) echo 'checked'; ?>><label
for="top4">Mushrooms</label>
<input name="toppings[]" type="checkbox" value="pineapple" id="top5" <?php if(in_array('pineapple', $toppingArr)) echo 'checked'; ?>><label
for="top5">Pineapple</label>
<input name="toppings[]" type="checkbox" value="peppers" id="top6" <?php if(in_array('peppers', $toppingArr)) echo 'checked'; ?>><label
for="top6">Peppers</label>
<input type='submit' value='Submit' />
</form>
</body>
</html>
There is an extra >
at id="top3">
preventing your code from working properly. It should be reading id="top3"
.
With the error corrected, your code could work when you are posting on the same page. If you are moving away from the page and coming back, you would use $_SESSION
variables instead of $_GET
.