Solved

checkbox in repeat area

Posted on 2010-08-13
4
383 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
html input type 3 22
PHP: concatenate query 13 44
Problem sending file attachments 8 23
PHP: Insert Data into MySQL 5 17
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now