Viewed   496 times

Basically my question is the following, how can i select on "Checked" checkbox's while doing a $_POST request in PHP, currently i have the checkbox's doing an array as shown below.

<input type="checkbox" value="1" name="checkbox[]">
<input type="checkbox" value="2" name="checkbox[]">
<input type="checkbox" value="2" name="checkbox[]">
<input type="checkbox" value="3" name="checkbox[]">

I want to be able to do something like this


I've tried doing similar to that and it's not echoing anything.


foreach($_POST['checkbox'] as $value) {


Note that $_POST['checkbox'] will only exist if at least one checkbox is checked. So you must add an isset($_POST['checkbox']) check before that loop. The easiest way would be like this:

$checkboxes = isset($_POST['checkbox']) ? $_POST['checkbox'] : array();
foreach($checkboxes as $value) {
    // here you can use $value
Wednesday, December 14, 2022

No, but you can break the loop when your condition is met:

foreach ($array as $value){
  if($condition != true)
Monday, November 7, 2022

Untested, but should give you an idea of what you should do.


$markers = str_repeat('?, ', count($_POST['check_list']) - 1) . '?';

$query = $conn->prepare('SELECT * FROM `people` WHERE `p_location` IN (' . $markers . ')');
Sunday, October 30, 2022

Doing this should solve the problem, try:

<div id="etc" class="page">
    <form action="<?php $_SERVER['PHP_SELF']?>" method="POST">
        <textarea name="textPost" id="textPost"></textarea>
        <input type="submit" name="btnPost" id="btnPost" value="Post" />
        <div id="tae">
           <input type="text" name="display" id="display" value="0" />

        <!-- END FORM REMOVED HERE -->

        <!-- FORM REMOVED HERE -->

     <div id="delButtonDiv"> <input type="submit" value="Delete" name="btnDelete" /> </div>


You have two submit button in your form, then you need to know what exactly action you are taking, POST or DELETE, right? then you can create an basic controller to your post in your own php script, something like this:

Here is the form -> <form action="<?php $_SERVER['PHP_SELF']?>" method="POST">

and here are the submit buttons

<input type="submit" name="btnPost" id="btnPost" value="Post" />
<input type="submit" value="Delete" name="btnDelete" />

$_SERVER['PHP_SELF'] tells you that it will send the form data to itself, if so, then do something like this in the begin of your script:

if ($_POST[btnPost] == "Post") 
// do some action to post the data

if ($_POST[btnDelete] == "Delete") 
// do some action to delete the data

clear now?

Friday, November 18, 2022
    $ownership = nl2br($_POST['product-types-owned']); 

product-types-owned is going to be an array of the checkbox values that were selected. You'll need to implode that before you do anything else:

$ownership = nl2br(implode(',', $_POST['product-types-owned']));

Right now you're trying to nl2br on an array, which won't work. nl2br expects a string, so php will type cast the array into its default string representation, which is literally the word Array.

Saturday, December 10, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :