[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 723
  • Last Modified:

insert checkbox group into database.

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
petewinter
Asked:
petewinter
  • 8
  • 6
1 Solution
 
Loganathan NatarajanLAMP DeveloperCommented:
0
 
Loganathan NatarajanLAMP DeveloperCommented:
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
 
petewinterAuthor Commented:
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
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
petewinterAuthor Commented:
Sorry just to be clear...

How would I get the post the result of this checkbox id="CheckboxGroup1_1"
0
 
Loganathan NatarajanLAMP DeveloperCommented:
it will be posted as array... you have to take the value of  array [1]
something like that ..
0
 
Loganathan NatarajanLAMP DeveloperCommented:
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
 
petewinterAuthor Commented:
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
 
petewinterAuthor Commented:
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
 
Loganathan NatarajanLAMP DeveloperCommented:
You can handle this with javascript.
0
 
Loganathan NatarajanLAMP DeveloperCommented:
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
 
Loganathan NatarajanLAMP DeveloperCommented:
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
 
petewinterAuthor Commented:
Thanks, but that selects all the checkboxes not just the ones in the relevant group?
0
 
petewinterAuthor Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now