Solved

insert checkbox group into database.

Posted on 2010-08-16
14
697 Views
Last Modified: 2013-12-13
See my checkbox group attached.

I need to insert the value of each checkbox into my database.

Normally I would post the field name. i.e. $_POST['CheckboxGroup1']

, but within a checkbox group the name is for each checkbox. It's only the id that changes.

How would post the individual check box instead of the group?
<label>
      <input type="checkbox" name="CheckboxGroup1" value="checkbox" id="CheckboxGroup1_0" />
      Checkbox</label>
    <br />
    <label>
      <input type="checkbox" name="CheckboxGroup1" value="checkbox" id="CheckboxGroup1_1" />
      Checkbox</label>
    <br />
    <label>
      <input type="checkbox" name="CheckboxGroup1" value="checkbox" id="CheckboxGroup1_2" />
      Checkbox</label>
    <br />
  </p>
  <input type="hidden" name="MM_insert" value="form1" />

Open in new window

0
Comment
Question by:petewinter
  • 8
  • 6
14 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 33444528
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 33444536
You need to define the variable as array
<label>
      <input type="checkbox" name="CheckboxGroup1[]" value="checkbox" id="CheckboxGroup1_0" />
      Checkbox</label>
    <br />
    <label>
      <input type="checkbox" name="CheckboxGroup1[]" value="checkbox" id="CheckboxGroup1_1" />
      Checkbox</label>
    <br />
    <label>
      <input type="checkbox" name="CheckboxGroup1[]" value="checkbox" id="CheckboxGroup1_2" />
      Checkbox</label>
    <br />
  </p>
  <input type="hidden" name="MM_insert" value="form1" />

Open in new window

0
 

Author Comment

by:petewinter
ID: 33444581
Thanks, but still not sure.

If I want to post the result of the say the second check box how would I do it?
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

Author Comment

by:petewinter
ID: 33444597
Sorry just to be clear...

How would I get the post the result of this checkbox id="CheckboxGroup1_1"
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 33444616
it will be posted as array... you have to take the value of  array [1]
something like that ..
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 33444637
something like this,
<form action="test9.php" method="post">
<label>
      <input type="checkbox" name="CheckboxGroup1[]" value="1"  />
      Checkbox</label>
    <br />
    <label>
      <input type="checkbox" name="CheckboxGroup1[]" value="2" />
      Checkbox</label>
    <br />
    <label>
      <input type="checkbox" name="CheckboxGroup1[]" value="3" />
      Checkbox</label>
    <p>
      <label>
      <input type="submit" name="Submit" value="Submit">
      </label>
    </p>
  <p>&nbsp;</p>
    <p><br />
      </p>
      <input type="hidden" name="MM_insert" value="form1" />
            </p>
</form>

<?php
$box=$_POST['CheckboxGroup1'];

while (list ($key,$val) = @each ($box)) { 
echo "$val,"; 
} 
?>

Open in new window

0
 

Author Comment

by:petewinter
ID: 33446233
Still struggling. I will explain the issue in more details in my next post.
<div class="MediaName2"><a href="media_details.php?id=<?php echo $row_rs_media_group['id']; ?>"><?php echo $row_rs_media_brand['brand']; ?> <?php echo $row_rs_media_group['name']; ?></a></div>

<div class="GroupSelect">

<script>
  function checkall<?php echo $row_rs_media['media_group_id']; ?>()
  {
      var groupa= document.getElementsByName("checkbox_add<?php echo $row_rs_media['media_group_id']; ?>");
      for(i=0;i<groupa.length;i++)
      groupa[i].checked=true;

  }
   function uncheckall<?php echo $row_rs_media['media_group_id']; ?>()
  {
      var groupa= document.getElementsByName("checkbox_add<?php echo $row_rs_media['media_group_id']; ?>");
      for(i=0;i<groupa.length;i++)
      groupa[i].checked=false;

  }
  </script>
  
<a href="javascript:checkall<?php echo $row_rs_media['media_group_id']; ?>()">Select Group</a> &nbsp;&nbsp;|&nbsp;&nbsp; <a href="javascript:uncheckall<?php echo $row_rs_media['media_group_id']; ?>()">Deselect Group</a>

</div>

<div class="Clear"></div>

  <?php if(!empty($row_rs_media_group['description'])) { ?>
  
  <p style="margin-top:15px; margin-bottom:15px;"><?php echo $row_rs_media_group['description']; ?></p>
  <?php } ?>
    
<table width="100%" cellspacing="1" cellpadding="5">
  <tr>
    <td bgcolor="#424856" class="whitebold">Code</td>
    <td bgcolor="#424856" class="whitebold">Width</td>
    <td bgcolor="#424856" class="whitebold">Length</td>
    <td bgcolor="#424856" class="whitebold">
    <?php if(!empty($row_rs_media['weight'])) { echo "Weight"; } else if(!empty($row_rs_media['thickness'])) { echo "Thickness"; } else { echo "Weight"; } ?>
    </td>
    <td bgcolor="#424856" class="whitebold">Price</td>
    <td bgcolor="#424856" class="whitebold">Add to Price List</td>
  </tr>
  <tr>
    <td bgcolor="#DDDDDD"><a href="media_roll_modify.php?id=<?php echo $row_rs_media['id']; ?>"><?php echo $row_rs_media['selectech_group_code']; ?>-<?php echo $row_rs_media['width']; ?>-<?php echo $row_rs_media['length']; ?></a></td>
    <td bgcolor="#DDDDDD"><?php echo $row_rs_media['width']; ?>mm</td>
    <td bgcolor="#DDDDDD"><?php echo $row_rs_media['length']; ?>m</td>
    <td bgcolor="#DDDDDD"><?php if(empty($row_rs_media['weight'])) { echo ""; } else { echo $row_rs_media['weight']. "gsm"; } ?><?php if(empty($row_rs_media['thickness'])) { echo ""; } else { echo $row_rs_media['thickness']. "mic"; } ?></td>
    <td bgcolor="#DDDDDD"><?php echo utf8_encode($symbol); ?><?php echo number_format(round($single_price_converted, 2), 2); ?></td>
    <td bgcolor="#DDDDDD">
    
    <label>
      <input name="checkbox_add<?php echo $row_rs_media['media_group_id']; ?>" type="checkbox" />
    </label>
    <?php
	
	mysql_select_db($database_conn_selectech, $conn_selectech);
	$query_rs_bespoke_pricelist_selected = "SELECT * FROM bespoke_pricelist WHERE media_id = " . $row_rs_media['id'];
	$rs_bespoke_pricelist_selected = mysql_query($query_rs_bespoke_pricelist_selected, $conn_selectech) or die(mysql_error());
	$row_rs_bespoke_pricelist_selected = mysql_fetch_assoc($rs_bespoke_pricelist_selected);
	$totalRows_rs_bespoke_pricelist_selected = mysql_num_rows($rs_bespoke_pricelist_selected);
			
	if ($row_rs_bespoke_pricelist_selected['media_id'] != $row_rs_media['id']) {
		
	$media_id = $row_rs_media['id'];
	$customer_id = $row_rs_customer_selected['id'];
	$user_id = $row_rs_users['id'];
				  
    if (isset($_POST['checkbox_add'.$row_rs_media['media_group_id']])) {  
       mysql_query("INSERT INTO bespoke_pricelist (media_id, customer_id, user_id) VALUES ('$media_id', '$customer_id', '$user_id')");  
   }
   }
?>
    
    </td>
  </tr>
  <?php } while ($row_rs_media = mysql_fetch_assoc($rs_media)); ?>
</table>

Open in new window

0
 

Author Comment

by:petewinter
ID: 33446406
See lines 5 to 20. This is javascript to selects the checkboxes within the related group.

My checkbox which gets repeated for each row is on line 53.

The name is the script need to be the same as the checkbox name to work.

On line 69 I have a command that says if the checkbox is selected insert into a database.

My problem is line 53 and 69 needs the checkbox name to be unique, but my javascript needs this to be the same.

i.e. <input name="checkbox_add<?php echo $row_rs_media['id']; ?>" type="checkbox" />

The above is what I require for the database insert to work correctly, but stops the javascript working.

How can I solve this problem? Thanks


0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 33446718
You can handle this with javascript.
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 33446727
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 33446746
another simple script to apply.
<script>
function select(a) {
    var theForm = document.myForm;
    for (i=0; i<theForm.elements.length; i++) {
        if (theForm.elements[i].name=='mycheckbox[]')
            theForm.elements[i].checked = a;
    }
}
</script>

<form name="myForm">
<input type="checkbox" name="mycheckbox[]" value="somevalue1" /><br/>
<input type="checkbox" name="mycheckbox[]" value="somevalue2" /><br/>
<input type="checkbox" name="mycheckbox[]" value="somevalue3" />
</form>

<a href="javascript:select(1)">Check all</a> |
<a href="javascript:select(0)">Uncheck all</a>

Open in new window

0
 
LVL 36

Accepted Solution

by:
Loganathan Natarajan earned 500 total points
ID: 33446799
apply your code,
<div class="MediaName2"><a href="media_details.php?id=<?php echo $row_rs_media_group['id']; ?>"><?php echo $row_rs_media_brand['brand']; ?> <?php echo $row_rs_media_group['name']; ?></a></div>

<div class="GroupSelect">
  
  
  <script>
function select(a) {
    var theForm = document.myForm;
    for (i=0; i<theForm.elements.length; i++) {
        if (theForm.elements[i].name=='mycheckbox[]')
            theForm.elements[i].checked = a;
    }
}
</script>
  
<a href="javascript:select(1)">Select Group</a> &nbsp;&nbsp;|&nbsp;&nbsp; <a href="javascript:select(0)">Deselect Group</a>

</div>

<div class="Clear"></div>

  <?php if(!empty($row_rs_media_group['description'])) { ?>
  
  <p style="margin-top:15px; margin-bottom:15px;"><?php echo $row_rs_media_group['description']; ?></p>
  <?php } ?>
    
<table width="100%" cellspacing="1" cellpadding="5">
  <tr>
    <td bgcolor="#424856" class="whitebold">Code</td>
    <td bgcolor="#424856" class="whitebold">Width</td>
    <td bgcolor="#424856" class="whitebold">Length</td>
    <td bgcolor="#424856" class="whitebold">
    <?php if(!empty($row_rs_media['weight'])) { echo "Weight"; } else if(!empty($row_rs_media['thickness'])) { echo "Thickness"; } else { echo "Weight"; } ?>
    </td>
    <td bgcolor="#424856" class="whitebold">Price</td>
    <td bgcolor="#424856" class="whitebold">Add to Price List</td>
  </tr>
  <tr>
    <td bgcolor="#DDDDDD"><a href="media_roll_modify.php?id=<?php echo $row_rs_media['id']; ?>"><?php echo $row_rs_media['selectech_group_code']; ?>-<?php echo $row_rs_media['width']; ?>-<?php echo $row_rs_media['length']; ?></a></td>
    <td bgcolor="#DDDDDD"><?php echo $row_rs_media['width']; ?>mm</td>
    <td bgcolor="#DDDDDD"><?php echo $row_rs_media['length']; ?>m</td>
    <td bgcolor="#DDDDDD"><?php if(empty($row_rs_media['weight'])) { echo ""; } else { echo $row_rs_media['weight']. "gsm"; } ?><?php if(empty($row_rs_media['thickness'])) { echo ""; } else { echo $row_rs_media['thickness']. "mic"; } ?></td>
    <td bgcolor="#DDDDDD"><?php echo utf8_encode($symbol); ?><?php echo number_format(round($single_price_converted, 2), 2); ?></td>
    <td bgcolor="#DDDDDD">
    
    <label>
      <input name="checkbox_add<?php echo $row_rs_media['media_group_id']; ?>" type="checkbox" />
    </label>
    <?php
	
	mysql_select_db($database_conn_selectech, $conn_selectech);
	$query_rs_bespoke_pricelist_selected = "SELECT * FROM bespoke_pricelist WHERE media_id = " . $row_rs_media['id'];
	$rs_bespoke_pricelist_selected = mysql_query($query_rs_bespoke_pricelist_selected, $conn_selectech) or die(mysql_error());
	$row_rs_bespoke_pricelist_selected = mysql_fetch_assoc($rs_bespoke_pricelist_selected);
	$totalRows_rs_bespoke_pricelist_selected = mysql_num_rows($rs_bespoke_pricelist_selected);
			
	if ($row_rs_bespoke_pricelist_selected['media_id'] != $row_rs_media['id']) {
		
	$media_id = $row_rs_media['id'];
	$customer_id = $row_rs_customer_selected['id'];
	$user_id = $row_rs_users['id'];
				  
    if (isset($_POST['checkbox_add'.$row_rs_media['media_group_id']])) {  
       mysql_query("INSERT INTO bespoke_pricelist (media_id, customer_id, user_id) VALUES ('$media_id', '$customer_id', '$user_id')");  
   }
   }
?>
    
    </td>
  </tr>
  <?php } while ($row_rs_media = mysql_fetch_assoc($rs_media)); ?>
</table>

Open in new window

0
 

Author Comment

by:petewinter
ID: 33447182
Thanks, but that selects all the checkboxes not just the ones in the relevant group?
0
 

Author Closing Comment

by:petewinter
ID: 33483310
Thanks for your help, but I have solved the issue using javascript: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26405901.html#a33482959
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Selecting SUM data from mysql table with PHP 4 20
php string detection problem 7 34
send post values 7 26
How has this flight information been added to this website? 7 25
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

831 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