Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

insert checkbox group into database.

Posted on 2010-08-16
14
Medium Priority
?
718 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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: 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 1500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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‚Ķ
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

824 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