[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

checkbox in repeat area

See my code attached which is in a repeating region.

It should only insert into my database if the checkbox has been checked, but it is inserting everything. Why is this?
<label>
      <input name="checkbox_add" type="checkbox" id="checkbox_add" value="1" />
    </label>
    <?php
	$media_id = $row_rs_media['id'];
	$customer_id = $row_rs_customer_selected['id'];
	$user_id = $row_rs_users['id'];
				  
    if ( $_POST['checkbox_add'] == "1" ) {
        mysql_query("INSERT INTO bespoke_pricelist (media_id, customer_id, user_id) VALUES ('$media_id', '$customer_id', '$user_id')");
    }
?>

Open in new window

0
petewinter
Asked:
petewinter
2 Solutions
 
georgirCommented:
What do you mean by "in repeat area" ?
Do you have multiple such checkboxes for multiple media_ids or customers?
If so, and you use the same checkbox name for all of them, it is normal that if one of them is checked, your code would think they are all checked. You will have to modify the checkbox name to be different for each checkbox. For example make it 'checkbox_add'.$media_id if you have one ofr each media_id, or similarly for customer_id or whatever it that's changing for each checkbox.
The code that you selected does not show if or how your checkboxes repeat though, so I can't be more specific.

If the checkbox does not repeat, then I don't understand your question at all.
0
 
Greg AlexanderLead DeveloperCommented:
Does it do it when you change the if condition to
 if (isset($_POST['checkbox_add'])) {  
        mysql_query("INSERT INTO bespoke_pricelist (media_id, customer_id, user_id) VALUES ('$media_id', '$customer_id', '$user_id')");  
    }  
0
 
petewinterAuthor Commented:
Perfect. Many thanks
0
 
foreverfreshCommented:
change
<input name="checkbox_add" type="checkbox" id="checkbox_add" value="1" />
to
<input name="checkbox_add[]" type="checkbox" id="checkbox_add" value="<?php echo $control_numer_or_id" />

checkbox_add --> checkbox_add[]
1 -- > uniqe control number (for example : $media_id)
when you post form
you will have an array of checkbox_add
$CountChecked=count($_POST['checkbox_add']);
$CountChecked --> how many checkbox has checked
$_POST['checkbox_add'][0] --> first checked box value (which is $media_id for this checkbox)
$_POST['checkbox_add'][1] --> second checked box value
...
...
$_POST['checkbox_add'][$CountChecked-1] --> last checked box value

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now