Solved

checkbox in repeat area

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
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 look for a specific file type in a local or remote server directory using PHP.

705 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

19 Experts available now in Live!

Get 1:1 Help Now