Jump to content

Formatting checkboxes on forms


nascarjunky

Recommended Posts

I would appreciate any help with this. I have a form which includes checkboxes. The descriptions for the checkboxes are loaded from fields in a database table. This part is working well. Please click www.legendnascar.com/alldrivercheckboxes.php to see the form.

 

What I want to do is display the form with the driver name and the values in separate columns so they appear as a table versus the unorganized way they do now.

 

Please see the partial code below:

 

 include("nascarconnect.php");

   echo "<html>
      <head><title>All Drivers</title></head>
      <body>";


  $query = "SELECT * FROM AllDrivers 
                ORDER BY salary DESC";
  $result = mysql_query($query)
       or die ("Couldn't execute query.");

  echo "<div style='margin-left: .5in'>
  <p> 
  <p><b>Please Select 15 Drivers From The List Below.</b>
  <p>Be Sure And Select 15:\n";
  

  /* create form containing checkboxes */
  echo "<form action='15manrosterselection.php' method='POST'>\n";

  while ($row = mysql_fetch_array($result))
  {
     extract($row);
     echo "<input type='checkbox' name='driver'value='$drid'>$name $salary";
     echo "<br>\n";
  }
echo "<p><input type='submit' value='Submit 15 Man Roster'>
        </form>\n";
?>
</div></body></html>

 

I would greatly appreciate any help.

Link to comment
https://forums.phpfreaks.com/topic/38055-formatting-checkboxes-on-forms/
Share on other sites

OK well its good prectice to give your form inputs a label so lets use that to do your check boxes.

 

change this line

 

echo "<input type='checkbox' name='driver'value='$drid'>$name $salary";

 

to this

 

echo '<label class="chkbxlbl"><input type="checkbox2 name="driver" value="$drid"><span>' . $name . ' ' . $salary . '</span></label>';

 

(APOLOGOES for switching single and double quotes but I like to see double quotes in html...)

 

in you css...

 


label.chbxlbl
{
display: block;
width: 100%;
}

label.span
{
display: block;
width: 90%;
text-align: right;
}

Thanks again so much. I am still having problems. I am assuming I did something incorrect with the placement of the code.

 

<?php
/*  Program name: alldrivercheckboxes.php
*  Description:  Program displays a list of 
*                checkboxes from database info for selecting 12 man roster.
*/

   //session_start();
   //if (@_SESSION['auth'] != "yes")
   //{
   //  header("Location: Login.php");
   //  exit();
   // }

   include("nascarconnect.php");

   echo "<html>
      <head><title>All Drivers</title>
      <style> label.chbxlbl
              {
               display: block;
               width: 100%;
              }
              label.span
              {
               display: block;
               width: 90%;
               text-align: right;
              }</style>      
      </head>
      <body>";


  $query = "SELECT * FROM AllDrivers 
                ORDER BY salary DESC";
  $result = mysql_query($query)
       or die ("Couldn't execute query.");

  echo "<div style='margin-left: .5in'>
  <p> 
  <p><b>Please Select 15 Drivers From The List Below.</b>
  <p>Be Sure And Select 15:\n";
  

  /* create form containing checkboxes */
  echo "<form action='15manrosterselection.php' method='POST'>\n";

  while ($row = mysql_fetch_array($result))
  {
     extract($row);
//    echo "<input type='checkbox' name='driver'value='$drid'>$name $salary";
       echo'<label class="chkbxlbl"><input type="checkbox2" name="driver"                        
       value="$drid"><span>' . $name . ' ' . $salary . '</span></label>';
       echo "<br>\n";
  }
echo "<p><input type='submit' value='Submit 15 Man Roster'>
        </form>\n";
?>
</div></body></html>

 

Here is the result www.legendnascar.com/alldrivercheckboxes.php

 

Thanks again.

OK 1st I made a type-o!!!

 

change 'label.span' to 'label span'

 

The second is your bad - type="checkbox2" no such type so get rid of the 2.

 

the last step will be alter the width of the label.chbxlbl to suit your needs...

 

 

Oh and value="$drid" needs to be value="' . $drid . '"

 

Oh and label.chbxlbl should be label.chkbxlbl in the style tag...

  • 2 weeks later...

play with this.....

 

<?php

include("nascarconnect.php");

 

  echo "<html><head><title>All Drivers</title></head><body>";

 

$query = "SELECT * FROM AllDrivers ORDER BY salary DESC";

  $result = mysql_query($query) or die ("Couldn't execute query.");

 

?>

<div align=center>

<table width=100% cellpadding=0 cellspacing=0 border=0>

<tr>

<td width=10%></td>

<td width=80%>

<form action="15manrosterselection.php" method="post">

<fieldset><legend>15 Man Roster</legend>

<b>Please Select 15 Drivers From The List Below.<br><br>Be Shure and Select 15:</b>

<table width=100% cellpadding=0 cellspacing=0 border=0>

<tr bgcolor=f1f1f1>

<td width=33%></td>

<td width=33%><b>Name</b></td>

<td width=33%><b>Salary</b></td>

</tr>

<?php

  while ($row = mysql_fetch_array($result)) {

echo'

<tr>

<td width=33%><input type="checkbox" name="driver" value="'.$row['drid'].'"></td>

<td width=33%>'.$row['name'].'</td>

<td width=33%>'.$row['salary'].'</td>

</tr>

';

}

?>

</table>

<br><input type='submit' value='Submit 15 Man Roster'>

</fieldset>

</form>

</td>

<td width=10%></td>

</tr>

</table>

</div>

</body>

</html>

just a note... the input name for your checkboxes should change so you can isolate the values. example...

 

<input type="checkbox" name="driver1" value="some value">

<input type="checkbox" name="driver2" value="some value">

<input type="checkbox" name="driver3" value="some value">

<input type="checkbox" name="driver4" value="some value">

<input type="checkbox" name="driver5" value="some value">

 

That way you can have.

$value1 = $_POST['driver1'];

$value2 = $_POST['driver2'];

$value3 = $_POST['driver3'];

etc....

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.