Forums

Updating a table

abasel 09 Aug, 2010
Its been a while since I've done database work so need a little help. After doing some Googling, I have come up with the following code which will check to see if the user has already submitted a form and if so will populate the checkboxes with the data initially submitted. (I have removed all but one CheckBox option so that the code is smaller
<?
$user =& JFactory::getUser();
$username=($user->username);
if ($user->gid == 0) {
  echo "<p>You are not authorized to view this page!</p><p>Please logon.</p>";
  return;
}
$db =& JFactory::getDBO(); 
$db->setQuery('SELECT Art,Drama,Music,ESOL,Numeracy,Moari,French,Japanese,Literacy,Technology,FoodTechnology,Graphics FROM jos_chronoforms_form_Year8To9_options WHERE Username="'.$username.'"');
$Moari=($db->Moari);
?>

<p>
  <label for="Moari">Moari</label>
  <?php if ($Moari==1) 
    {echo "<input name='Moari' type='checkbox' id='Moari' value='1' checked='checked>'";}
		else
    {echo "<input name='Moari' type='checkbox' id='Moari' value='1'>";} ?>
</p>
<p>
  <input type="submit" name="Submit" id="Submit" value="Submit" />
</p>


$Moari (the Moari field does have data in the table for the user in question) never displays any info which means that my data is not being retrieved from the database.

I have run the following SQL query directly on the database, and it workd

SELECT Art,Drama,Music,ESOL,Numeracy,Moari,French,Japanese,Literacy,Technology,FoodTechnology,Graphics FROM jos_chronoforms_form_Year8To9_options WHERE Username="abasel"



I'm not sure how to see what $db holds (as I don't seem able to echo it out). I am also not sure how to extract data from it, should it contain any.
abasel 11 Aug, 2010
Ok so after reading some of the documentation, and a little help from one of my students, I am almost there. The problem I have though is that I can't seem to unselect a checkbox. The reason for this is that when a box is unticked, no information is sent through for that field. This means that when submit is pressed, if the a field had been unselected it won't be updated as such as nothing is passed back from that field.... not sure if I am making any sense.
<?
$user =& JFactory::getUser();
$username=($user->username);
if ($user->gid == 0) {
  echo "<p>You are not authorized to view this page!</p><p>Please logon.</p>";
  return;
}
$db =& JFactory::getDBO(); 
$query="SELECT cf_id,Art,Drama,Music,ESOLa,Numeracya,ESOLb,Numeracyb,ESOLc,Numeracyc,Moari,French,Japanese,Literacy,Technology,FoodTechnology,Graphics 
           FROM ".$db->nameQuote('#__chronoforms_form_Year8To9_options')."
		   WHERE ".$db->nameQuote('Username')."=".$db->quote($username).";
	   ";	   
$db->setQuery($query);
$result=$db->loadObject();
$Art=($result->Art);
$Drama=($result->Drama);
$Music=($result->Music);
$ESOLa=($result->ESOLa);
$Numeracya=($result->Numeracya);
$ESOLb=($result->ESOLb);
$Numeracyb=($result->Numeracyb);
$ESOLc=($result->ESOLc);
$Numeracyc=($result->Numeracyc);
$Moari=($result->Moari);
$French=($result->French);
$Japanese=($result->Japanese);
$Literacy=($result->Literacy);
$Technology=($result->Technology);
$FoodTechnology=($result->FoodTechnology);
$Graphics=($result->Graphics);

$cf_id=($result->cf_id);
if ($cf_id!=0) {echo '<input type="hidden" name="cf_id" id="cf_id" value="'.$cf_id.'">';}

if ($Art==1) $HTML['Art']='Checked="Checked"';
if ($Drama==1) $HTML['Drama']='Checked="Checked"';
if ($Music==1) $HTML['Music']='Checked="Checked"';
if ($ESOLa==1) $HTML['ESOLa']='Checked="Checked"';
if ($Numeracya==1) $HTML['Numeracya']='Checked="Checked"';
if ($Moari==1) $HTML['Moari']='Checked="Checked"';
if ($French==1) $HTML['French']='Checked="Checked"';
if ($Japanese==1) $HTML['Japanese']='Checked="Checked"';
if ($ESOLb==1) $HTML['ESOLb']='Checked="Checked"';
if ($Literacy==1) $HTML['Literacy']='Checked="Checked"';
if ($Numeracyb==1) $HTML['Numeracyb']='Checked="Checked"';
if ($Technology==1) $HTML['Technology']='Checked="Checked"';
if ($FoodTechnology==1) $HTML['FoodTechnology']='Checked="Checked"';
if ($Graphics==1) $HTML['Graphics']='Checked="Checked"';
if ($ESOLc==1) $HTML['ESOLc']='Checked="Checked"';
if ($Numeracyc==1) $HTML['Numeracyc']='Checked="Checked"';
?>



<p>Hi <? echo $user->name; ?> </p>
<p> </p>
<p>Students may <strong>NOT</strong> choose more than <strong>TWO</strong> subjects from any one line.</p>
<p> </p>
<fieldset>
  <legend>Student Details</legend>
  <p>
    <label for="Name">Name</label>
    <?=$user-> name?>
    <input type="hidden" name="Name" id="Name" value="<?=$user-> name?>"/>
  </p>
  <p>
    <label for="Username">Username</label>
    <?=$user-> username?>
    <input type="hidden" name="Username" id="Username" value="<?=$user-> username?>"/>
  </p>
  <p>
    <label for="SchoolEmail">School Email</label>
    <?=$user-> email?>
    <input type="hidden" name="SchoolEmail" id="SchoolEmail" value="<?=$user-> email?>"/>
  </p>
</fieldset>
<table  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="150"><fieldset>
        <legend>Line A</legend>
        <p>
          <label for="Art">Art</label>
          <input name="Art" type="checkbox" id="Art" value="1" <?=$HTML['Art']?> />
        </p>
        <p>
          <label for="Drama">Drama</label>
          <input name="Drama" type="checkbox" id="Drama" value="1" <?=$HTML['Drama']?> />
        </p>
        <p>
          <label for="Music">Music</label>
          <input name="Music" type="checkbox" id="Music" value="1" <?=$HTML['Music']?> />
        </p>
        <p>
          <label for="ESOL">ESOL</label>
          <input name="ESOLa" type="checkbox" id="ESOLa" value="1" <?=$HTML['ESOLa']?> />
        </p>
        <p>
          <label for="Numeracy">Numeracy</label>
          <input name="Numeracya" type="checkbox" id="Numeracya" value="1" <?=$HTML['Numeracya']?> />
        </p>
        <p> </p>
      </fieldset></td>
    <td width="15"> </td>
    <td width="150"><fieldset>
        <legend>Line B</legend>
        <p>
          <label for="Moari">Moari</label>
          <input name="Moari" type="checkbox" id="Moari" value="1" <?=$HTML['Moari']?> />
        </p>
        <p>
          <label for="French">French</label>
          <input name="French" type="checkbox" id="French" value="1" <?=$HTML['French']?> />
        </p>
        <p>
          <label for="Japanese">Japanese</label>
          <input name="Japanese" type="checkbox" id="Japanese" value="1" <?=$HTML['Japanese']?> />
        </p>
        <p>
          <label for="ESOL">ESOL</label>
          <input name="ESOLb" type="checkbox" id="ESOLb" value="1" <?=$HTML['ESOLb']?> />
        </p>
        <p>
          <label for="Literacy">Literacy</label>
          <input name="Literacy" type="checkbox" id="Literacy" value="1" <?=$HTML['Literacy']?> />
        </p>
        <p>
          <label for="Numeracy">Numeracy</label>
          <input name="Numeracyb" type="checkbox" id="Numeracyb" value="1" <?=$HTML['Numeracyb']?> />
        </p>
      </fieldset></td>
    <td width="15"> </td>
    <td width="150"><fieldset>
        <legend>Line C</legend>
        <p>
          <label for="Technology">Technology</label>
          <input name="Technology" type="checkbox" id="Technology" value="1" <?=$HTML['Technology']?> />
        </p>
        <p>
          <label for="FoodTechnology">Food Technology</label>
          <input name="FoodTechnology" type="checkbox" id="FoodTechnology" value="1" <?=$HTML['FoodTechnology']?> />
        </p>
        <p>
          <label for="Graphics">Graphics</label>
          <input name="Graphics" type="checkbox" id="Graphics" value="1" <?=$HTML['Graphics']?> />
        </p>
        <p>
          <label for="ESOL">ESOL</label>
          <input name="ESOLc" type="checkbox" id="ESOLc" value="1" <?=$HTML['ESOLc']?> />
        <p>
          <label for="Numeracy">Numeracy</label>
          <input name="Numeracyc" type="checkbox" id="Numeracyc" value="1" <?=$HTML['Numeracyc']?> />
        </p>
        <p> </p>
      </fieldset></td>
  </tr>
</table>
<p><strong>Notes:</strong></p>
<ol>
  <li>ESOL students must choose ESOL from one line.</li>
  <li>Students currtently doing Literacy should choose it.</li>
  <li>The choices you make in Year 9 do <strong>NOT</strong> preculde you from doing a different subject in Year 10.</li>
  <li>The college reserves the right to withdraw any of the subjects offered above if insufficent numbers do not warrant offering the subject.</li>
</ol>
<p>
  <input type="submit" name="Submit" id="Submit" value="Submit" />
</p>


I realise that this is not a Chronoform issue but it is the sort of issue that other Chronoform users may have run into.
GreyHead 11 Aug, 2010
Hi abasel ,

That's been much discussed here. The usual answer is to add code to the OnSubmit Before box to check the checkboxes:
<?php
$Art = JRequest::getString('Art', '', 'post');
JRequest::setVar('Art', $Art);
?>
Though in practice I'd probably use a loop to automate it a bit
<?php
$checkboxes = array('Art', 'Drama', 'Music', . . .);
foreach ($checkboxes as $v ) {
  JRequest::setVar($v, JRequest::getString($v, '', 'post'));
}
?>


Bob
abasel 11 Aug, 2010
Excellent... it works like a dream now... many thanks :-)
abasel 12 Aug, 2010
One last question.

I have decided not to e-mail the results. Since I've disabled "Email the results" the above script doesn't work ie. I can once again no longer untick the selections.
GreyHead 12 Aug, 2010
Hi abasel ,

The OnSubmit Before Code is not executed if you disable 'Email The Results'. Leave that enabled and disable the Email Setup in the Properties box instead.

It's a historic quirk of ChronoForms from a time when all it did was email the results.

Bob
abasel 12 Aug, 2010
Got a strange one, the properties box won't open up... tried two different browsers
GreyHead 12 Aug, 2010
Hi abasel ,

Not sure which form this is but the Properties boxes open up OK for me in FireFox. You have to click the 'yellow' setup area first.

Bob
abasel 12 Aug, 2010
Aaagh.... its that simple... didn't realise that it needed to be selected first. Don't usually change the properties once its been created.

Once again thanks for the patient support
This topic is locked and no more replies can be posted.