Dr3am3rz
Dr3am3rz

Reputation: 563

get checkbox id and value at the same time php

I would like to know how do I get the value of the checkbox that is assigned by an ID to it at the same time.

Here is my brief code:

<table class="data" cellpadding="0" cellspacing="0">
<?php
$select=mysql_query("select * from products_list ORDER BY id ASC");
while($row=mysql_fetch_array($select)){

$class = ' class="new_arrival" ';

echo "<tr>
<td>".$row['id']."</td>
<input type='hidden' name='product_id' value='".$row['id']."' />
<input type='checkbox' name='product_new' checked='checked' onclick='document.product_listing.submit();' /></td></tr>";
?>
</table>

if (isset($_POST['product_new'])) {

$id = $_POST['product_id'];

echo ("<script language='JavaScript'>
    window.alert('".$id."')
    </script>");
}

The result I keep getting for ID is the latest record in database but the value of the checkbox works fine.

I've tried this:

<input type='checkbox' name='product_new[".$row['id']."]' checked='checked' onclick='document.product_listing.submit();' />

foreach($_POST['product_category'] as $id => $value)

Using foreach loop, it did work somehow but it will loop through those checkboxes whether it's checked or uncheck.

Hope you guys understand, been stuck in this for 2 days. My purpose is to click the checkbox be it check or uncheck, it will update the database according to the ID which is assigned by mysql show result.

Any help will be much appreciated. Thanks!!

Upvotes: 0

Views: 3028

Answers (3)

DaveRandom
DaveRandom

Reputation: 88647

Just put the id in the value= attribute of the checkbox.

To generate the HTML:

<table class="data" cellpadding="0" cellspacing="0">
<?php

  $query = "
    SELECT *
    FROM products_list
    ORDER BY id ASC
  ";
  $result = mysql_query($query);

  // What if the query fails? Handle error here!

  while ($row = mysql_fetch_ssoc($result)) {

    // ...

    echo "
    <tr>
      <td>".htmlspecialchars($row['id'])."</td>
      <td><!-- I'm guessing this open td tag was missing and should be added -->
        <input type='checkbox' name='product_new[]' checked='checked' value='".htmlspecialchars($row['id'])."' />
        <!-- You don't want the onclick for the checkbox to submit the form, or you will only ever get one result -->
      </td>
    </tr>";

    // ...

  }

?>
</table>

And when the form is submitted:

if (isset($_POST['product_new'])) {
  foreach ($_POST['product_new'] as $id) {
    // Do stuff with $id here
  }
}

Upvotes: 0

Usman Zaheer
Usman Zaheer

Reputation: 625

I don't quite understand what exactly are you looking to do with checkboxes but here are a couple of things that would help you do whatever you want to do:

  1. HTML form don't submit the value of a checkbox if it is not checked e.g. if you have a single checkbox like and you submit the form with it unchecked you will get null if you try to access chkbox1

  2. If you are looking to get if the clicked checkbox is checked or not you can do it by either making each onclick pass the value of the id like

<input type='checkbox' name='product_new[".$row['id']."]' checked='checked' onclick='setValueAndSubmit(<?=$row[id]?>)' />

and write the function in js which checks the checked attribute of the checkbox and process accordingly

OR

you if you looking to look at all the checkbox values on submit then you can simply iterate over the checkbox group and you will only find the checkbox names of the checkboxes that are checked, unchecked won't be submitted.

EDIT: Just remove the onclick after every checkbox and put a submit button, and once the form submits you will get the names and values of the checked checkboxes so set those, and unset all the others.

Upvotes: 1

meiamsome
meiamsome

Reputation: 2944

If I understand the issue correctly...

This is happening due to having more than one input with the name product_id so referencing by name results in one being taken, but not the one you want.

So, a fix would be to add a single field, product_id to the form outside of the loop and alter that value in the onClick of the checkbox.

Example onclick:

<input type='checkbox' name='product_new' checked='checked' onclick='document.product_listing['product_id'] = ".$row['id'].";document.product_listing.submit();' /></td></tr>";`

Something like that ought to work.

Upvotes: 0

Related Questions