Checkbox Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ACRMIKE
Forum Newbie
Posts: 7
Joined: Sun Jul 05, 2009 4:41 pm

Checkbox Question

Post by ACRMIKE »

Hello,
I am trying to figure out how to default the "table stored" values of a group of checkboxes to a form, both when the form is initially displayed and after the form is submitted.

I've tried this doing the checkboxes as both arrays and non array "name=" attributes.

The snippits of attached code actually work just fine as far as writing the values to the database table correctly, based on the values that are entered into form prior to submitting it. But once the form is submitted, the form values vanish. Also, I'd like to get the values from the database table to populate the form once the page is initially displayed so that the user knows what they chose in a prior session.

Here is the form code:

Code: Select all

 <form name="pform" id="pform" method="post" action="<?php $_SERVER['PHP_SELF'] ?>"; >
    <table width="100%" border="0" align="center" cellpadding="3" cellspacing="3" class="bluebox">
      <tr> 
    <td > 
      <input type="checkbox" name="votingalerts" value="va">
      <?php echo $votingalertsval; ?>
      Voting Alerts (don't miss your call to vote on your 
      project)<br>
      <input type="checkbox" name="miscmail" value="mm" >
      Ad-hoc urgent news, Sprout Notices, New Sponsorships<br>
      <input type="checkbox" name="monthlyprojectupdates" value="mpu">
      Monthly Project Updates<br>
      <input type="checkbox" name="quarterlyecogeenews" value="qen">
      Quarterly ecogee news<br>
      <input type="checkbox" name="annualreport" value="ar">
      Annual Report <br>
      <input type="checkbox" name="optout" value="oo">
      Opt-out of all notifications<br>
    </td>
      </tr>
      <tr> 
    <td > 
      <div align="left"> 
        <input name="doUpdate" type="submit" id="doUpdate" value="Update">
      </div>
    </td>
      </tr>
    </table>
  </form>
Here is php code:

Code: Select all

    $votingalertsval = 0;
    $miscmailval = 0;
    $monthlyprojectupdatesval = 0;
    $quarterlyecogeenewsval = 0;
    $annualreportval = 0;
    $optoutval = 0;
 
    if (isset($_POST['votingalerts'])) {
        $votingalerts = $_POST['votingalerts'];
 
        if ($votingalerts == 'va') {
            $votingalertsval = 1;
        }
    }
 
    if (isset($_POST['miscmail'])) {
        $miscmail = $_POST['miscmail'];
 
        if ($miscmail == 'mm') {
            $miscmailval = 1;
        }
    }
 
    if (isset($_POST['monthlyprojectupdates'])) {
        $monthlyprojectupdates = $_POST['monthlyprojectupdates'];
 
        if ($monthlyprojectupdates == 'mpu') {
            $monthlyprojectupdatesval = 1;
        }
    }
 
    if (isset($_POST['quarterlyecogeenews'])) {
        $quarterlyecogeenews = $_POST['quarterlyecogeenews'];
 
        if ($quarterlyecogeenews == 'qen') {
            $quarterlyecogeenewsval = 1;
        }
    }
 
    if (isset($_POST['annualreport'])) {
        $annualreport = $_POST['annualreport'];
 
        if ($annualreport == 'ar') {
            $annualreportval = 1;
        }
    }
 
    if (isset($_POST['optout'])) {
        $optout = $_POST['optout'];
 
        if ($optout == 'oo') {
            $optoutval = 1;
        }
    }
The SQL update statement works just fine.
Any suggestions or a link to a good way to do this?

Thanks,
Mike
ACRMIKE
Forum Newbie
Posts: 7
Joined: Sun Jul 05, 2009 4:41 pm

Re: Checkbox Question

Post by ACRMIKE »

FWIW,
I figured out how to do this. I retrieve the results from the SQL select statement before displaying the form, and just evaluate them as shown below. (in the database table i store each value as an int (1 or 0)).

Code: Select all

<input type="checkbox" name="votingalerts" value="va" <?php if($row_mail_options['voting_alerts']==1) {echo checked;} ?> >
Voting Alerts (don't miss your call to vote on your
project)<br>
<input type="checkbox" name="miscmail" value="mm" <?php if($row_mail_options['misc_mail']==1) {echo checked;} ?> >
Ad-hoc urgent news, Sprout Notices, New Sponsorships<br>
<input type="checkbox" name="monthlyprojectupdates" value="mpu" <?php if($row_mail_options['monthly_project_updates']==1) {echo checked;} ?>>
Monthly Project Updates<br>
<input type="checkbox" name="quarterlyecogeenews" value="qen" <?php if($row_mail_options['quarterly_ecogee_news']==1) {echo checked;} ?> >
Quarterly ecogee news<br>
<input type="checkbox" name="annualreport" value="ar" <?php if($row_mail_options['annual_report']==1) {echo checked;} ?> >
Annual Report <br>
<input type="checkbox" name="optout" value="oo" <?php if($row_mail_options['opt_out']==1) {echo checked;} ?> >
Opt-out of all notifications<br>
Post Reply