/    Sign up×
Community /Pin to ProfileBookmark

HTML/PHP Assignment: how can i add and delete data from multiple checkboxes?

hi all,

I have a php assignment for school and I don’t understand much about it. I have to make a shopping list (apples, bread, milk etc.) where I can tick different products and add them using the send button.

In the meantime I have figured out how that works. But I also need to be able to remove the products that have been ticked and added to the shopping list via a second send button.
So the data has to be saved in a session.

Does anyone have any idea how this works?I don’t get it.

This is my html code: (is it good or do i need to change some things?)
`<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>Grocery list</title>
</head>
<body>
<h1>GROCERY LIST</h1>
<form action=”hoi.php” method=”post”>
<input type=”checkbox” name=”chips” value=”j”> Potato chips<br><br/>
<input type=”checkbox” name=”bread” value=”j”> Bread<br><br/>
<input type=”checkbox” name=”cola” value=”j”> Coca Cola<br><br/>
<input type=”checkbox” name=”milk” value=”j”> Milk<br><br/>
<input type=”checkbox” name=”apples” value=”j”> Apples<br><br/>
<input type=”checkbox” name=”tomatoes” value=”j”> Tomatoes<br><br/>
<input type=”checkbox” name=”cheese” value=”j”> Cheese<br><br/>
<input type=”checkbox” name=”lemon” value=”j”> Lemon<br><br/>
<input type=”submit” name=”send1″ value=”Add to grocery list”>
<input type=”submit” name=”send2″ value=”Delete from grocery list”>
</form>
<br><br>
<p1>My grocery list:</p1>
</body>
</html>
`

Does anyone has any clue how to make the php code for this one:
I would really appreciate it if someone could help me with this 😁

to post a comment

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 29.2020 — I might suggest setting up your checkboxes to use "array naming":
<i>
</i>&lt;input type="checkbox" name="item[]" value="chips"&gt; Potato Chips
&lt;input type="checkbox" name="item[]" value="bread"&gt; Bread

Then in the PHP form-handler, you could just loop through the $_POST['item'] array, which will be populated by any checked boxes.
<i>
</i>$list = []; // For now just assume we're going to populate an array

if(!empty($_POST['item'])) {
foreach($_POST['item'] as $item) {
$list[] = $item;
}
}

// for test purposes only:
echo "&lt;pre&gt;".print_r($list, true)."&lt;/pre&gt;";
Copy linkTweet thisAlerts:
@Raoul0301authorOct 31.2020 — @NogDog#1624702

Okay thank you very much :)
×

Success!

Help @Raoul0301 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...