Solved

checkbox in repeat area

Posted on 2010-08-13
4
386 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 19

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
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…

730 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