Jump to content

Check Boxs


Lamez

Recommended Posts

How do I handle checkboxes in PHP?

 

Say I select 3 boxs out of 4, how do I get it to display those three checkboxs?

 

here is my php code I have so far: (does not work)

 

<?php
if(isset($_POST['Submit']))

{

    for ($i=0; $i<count($_POST['checkbox']);$i++) {

       echo "<br />value $i = ".$_POST['checkbox'][$i];

    }

}
?>

 

here is my html

<?php
include ("../../style/include/session.php");
include ("../../style/include/cons/head_2.php");
print '<div class="box"><h2>Select Paid</h2>';
if($session->isAdmin()){
?>
<form action="pay_do.php" method="post">

<table width="100%" border="0">
  <tr>
    <td width="419"><strong>First Name </strong></td>
    <td width="487"><strong>Last Name </strong></td>
    <td width="257"><strong>Paid (username) </strong></td>
  </tr>
  <? $qr=mysql_query("select * from users order by first");
while ($rs=mysql_fetch_assoc($qr)) { ?>
  <tr>
    <td><? echo $rs['first'];?></td>
    <td><? echo $rs['last'];?></td>
    <td><input type="checkbox" name="checkbox[]"  value="<?=$rs['username'] ?>" /> (<? echo $rs['username']; ?>)</td>
    <?
} 
?>
  </tr>
</table>

<br />
<input name="submit" type="submit" value="Mark Paid"  />
</form>

<?php
}else{
header("Location: ../../index.php");
}
print '</div>';
include ("../../style/include/cons/foot.php");
?>

 

-Thanks!

Link to comment
https://forums.phpfreaks.com/topic/89317-check-boxs/
Share on other sites

now how do I remove the users that I did not select? If I did not select user1, but I selected user2 it inserts a blank for user1 and adds the value for user2?

 

I have this so far:

<?php
include ("../../style/include/session.php");
include ("../../style/include/cons/head_2.php");
print '<div class="box">';
if($session->isAdmin()){

if(isset($_POST['Submit'])){
$boxes=$_POST['checkbox'];
if (empty($boxes)) {
//somthing
}

$i=0;
while ($i<=count($boxes)) {
mysql_query("insert into paid (user) values ('$boxes[$i]')");
$i++;
    }
echo "<h2>Processed</h2>";
echo "Selected users have been processed";
}else{
echo "<h2>Error!</h2>";
echo 'No data to process<br><a href="paid.php">Select Paid/Non-Paid</a>';
  }
}else{
header("Location: ../../index.php");
}
print '</div>';
include ("../../style/include/cons/foot.php");
?>

Link to comment
https://forums.phpfreaks.com/topic/89317-check-boxs/#findComment-457387
Share on other sites

<input type="checkbox" name="checkbox[]" onclick="if(this.value=='on') {  this.value='<?=$rs['username'] ?>'; } if(this.value=='<?=$rs['username'] ?>') { this.value=''; }" />

 

 

 

try changing your input field to that and then the other thing to this:

 

$boxes=$_POST['checkbox'];

 

 

$i=0;

while ($i<=count($boxes)) {

if($boxes[$i]!=='') {

mysql_query("insert into paid (user) values ('$boxes[$i]')");

}

$i++;

    }

Link to comment
https://forums.phpfreaks.com/topic/89317-check-boxs/#findComment-457396
Share on other sites

sure did, I just copied and pasted

 

paid.php

<?php
include ("../../style/include/session.php");
include ("../../style/include/cons/head_2.php");
print '<div class="box"><h2>Select Paid</h2>';
if($session->isAdmin()){
?>
<form action="pay_do.php" method="post">

<table width="100%" border="0">
  <tr>
    <td width="419"><strong>First Name </strong></td>
    <td width="487"><strong>Last Name </strong></td>
    <td width="257"><strong>Paid (username) </strong></td>
  </tr>
  <? $qr=mysql_query("select * from users order by first");
while ($rs=mysql_fetch_assoc($qr)) { ?>
  <tr>
    <td><? echo $rs['first'];?></td>
    <td><? echo $rs['last'];?></td>
    <td><input type="checkbox" name="checkbox[]" onclick="if(this.value=='on') {  this.value='<?=$rs['username'] ?>'; } if(this.value=='<?=$rs['username'] ?>') { this.value=''; }" /> (<? echo $rs['username']; ?>)</td>
    <?
} 
?>
  </tr>
</table>



<br />
<input name="Submit" type="Submit" value="Mark Paid"  />
</form>

<?php
}else{
header("Location: ../../index.php");
}
print '</div>';
include ("../../style/include/cons/foot.php");
?>

 

pay_do.php:

<?php
include ("../../style/include/session.php");
include ("../../style/include/cons/head_2.php");
print '<div class="box">';
if($session->isAdmin()){

if(isset($_POST['Submit'])){
$boxes=$_POST['checkbox'];


$i=0;
while ($i<=count($boxes)) {
if($boxes[$i]!=='') {
mysql_query("insert into paid (user) values ('$boxes[$i]')");
}
$i++;
    }
echo "<h2>Processed</h2>";
echo "Selected users have been processed";
}else{
echo "<h2>Error!</h2>";
echo 'No data to process<br><a href="paid.php">Select Paid/Non-Paid</a>';
  }
}else{
header("Location: ../../index.php");
}
print '</div>';
include ("../../style/include/cons/foot.php");
?>

Link to comment
https://forums.phpfreaks.com/topic/89317-check-boxs/#findComment-457406
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.