Solved

checkbox in repeat area

Posted on 2010-08-13
4
384 Views
Last Modified: 2013-12-13
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
Comment
Question by:petewinter
4 Comments
 
LVL 2

Accepted Solution

by:
georgir earned 450 total points
ID: 33429490
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
 
LVL 18

Assisted Solution

by:Greg Alexander
Greg Alexander earned 50 total points
ID: 33429507
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
 

Author Closing Comment

by:petewinter
ID: 33429640
Perfect. Many thanks
0
 
LVL 2

Expert Comment

by:foreverfresh
ID: 33429722
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question