Hi all,
this is what I've built:
A dynamically built form which selects the questions depending on the section of the form it's in.
The problem:
Each form input is called answer[]
passing the selected value to an update query
$updateSQL = sprintf("UPDATE tblsite SET sitetypeid=%s, pctid=%s, contactname=%s, address=%s, postcode=%s, email=%s, phone=%s, sitename=%s WHERE siteid=%s",
GetSQLValueString($_POST['sitetype'], "int"),
GetSQLValueString($_POST['pct'], "int"),
GetSQLValueString($_POST['contactname'], "text"),
GetSQLValueString($_POST['siteaddress'], "text"),
GetSQLValueString($_POST['postcode'], "text"),
GetSQLValueString($_POST['email'], "text"),
GetSQLValueString($_POST['contactphone'], "text"),
GetSQLValueString($_POST['sitename'], "text"),
GetSQLValueString($_SESSION['siteid'], "int"));
mysql_select_db($database_auditing, $auditing);
$Resultupdatesite = mysql_query($updateSQL, $auditing) or die(mysql_error());
$auditid = $_SESSION['auditid'];
$saved = "1";
$savemeonce = "True";
$siteaudit1 = $_SESSION['siteid'];
$answernum = $_POST['answerid'];
$response = $_POST['answer'];
$num = count($_POST['answerid']);
$numb = $num-1;
for ($q=0;$q<=$numb;$q++)
{
$auditnumber = $auditid;
$answer33 = $response[$q];
$answerid = $answernum[$q];
$queryupdateanswer = sprintf("UPDATE tblanswer SET auditid = %s, answer = %s WHERE answerid = %s",
GetSQLValueString($auditnumber, "int"),
GetSQLValueString($answer33, "int"),
GetSQLValueString($answerid, "int"));
mysql_select_db($database_auditing, $auditing);
$resultupdateanswer = mysql_query($queryupdateanswer, $auditing) or die(mysql_error());
}
The problem I've got is that the form builds itself by querying the database within each section to select the relevant questions. Because of the way these are selected the array key is reset to 0 within each form tab thus stopping the update loop above from working.
Can anyone suggest a way around this (or a better way to build the form - which has to stay in sections and needs to be dynamic).
The current form code:
<?php
while ($row_sections=mysql_fetch_assoc($sections)) {?>
<div class="AccordionPanel">
<div class="AccordionPanelTab"><?php echo $row_sections['sectionname'];?></div>
<div class="AccordionPanelContent">
<br />
<h5>Please answer the questions below to the best of your ability. For help, hover over the help icon, help text will appear in the box to the right of the screen.</h5>
<?php $questionsection = $row_sections['sectionid'];
mysql_select_db($database_auditing, $auditing);
$query_sections3 = sprintf("SELECT tblanswer.questionid AS answerqid, tblanswer.answerid AS answerid, tblanswer.answer AS answer, tblquestion.questionid AS questionid, tblquestion.questiontext AS questiontext, tblquestion.questionid AS quesid, tblquestion.questiontypeid AS questiontype, tblscore.score1 AS score1, tblscore.score2 AS score2, tblscore.score3 AS score3, tblscore.score3 AS score3, tblscore.score4 AS score4, tblhelptext.helptext AS help FROM tblanswer LEFT JOIN tblquestion ON tblanswer.questionid = tblquestion.questionid LEFT JOIN tblscore ON tblquestion.questionid = tblscore.questionid LEFT JOIN tblhelptext ON tblquestion.questionid = tblhelptext.questionid WHERE tblquestion.sectionid = %s AND tblanswer.auditid=%s ",
GetSQLValueString($questionsection, "int"),
GetSQLValueString($_SESSION['auditid'], "int"));
$sections3 = mysql_query($query_sections3, $auditing) or die(mysql_error());
$row_sections3=mysql_fetch_assoc($sections3);
$totalRows_sections3 = mysql_num_rows($sections3);
?>
<table class="table2" align="center">
<?php $purpleronnie=0; do {
?><tr>
<th><?php echo $row_sections3['questiontext'];?> <input type="hidden" name="qnumber[]" value="<?php echo $row_sections3['questionid'];?>" />
</th>
<td>
<input type="hidden" name="answerid[]" value="<?php echo $row_sections3['answerid'];?>" />
<?php if ($row_sections3['questiontype']=="1") { ?>
<input type="text" size="25" name="answer[]" value="<?php echo $row_sections3['answer'];?>" />
<?php }
else if ($row_sections3['questiontype']=="2") {?>
Yes: <input type="radio" name="answer[<?php echo $purpleronnie;?>]" value="<?php echo $row_sections3['score1'];?>" <?php if (!(strcmp(htmlentities($row_sections3['score1'], ENT_COMPAT, 'utf-8'),$row_sections3['answer']))) {echo "checked=\"checked\"";} ?>/>
No:<input type="radio" name="answer[<?php echo $purpleronnie;?>]" value="<?php $row_sections3['score2'];?>" <?php if (!(strcmp(htmlentities($row_sections3['answer'], ENT_COMPAT, 'utf-8'),$row_sections3['score2']))) {echo "checked=\"checked\"";} ?>/><?php }
else if ($row_sections3['questiontype']=="3") {?>
All: <input type="radio" name="answer<?php echo $questionsection;?>[<?php echo $purpleronnie;?>]" value="<?php $row_sections3['score1'];?>" <?php if (!(strcmp(htmlentities($row_sections3['answer'], ENT_COMPAT, 'utf-8'),$row_sections3['score1']))) {echo "checked=\"checked\"";} ?>/>
Some: <input type="radio" name="answer[<?php echo $purpleronnie;?>]" value="<?php $row_sections3['score2'];?>" <?php if (!(strcmp(htmlentities($row_sections3['answer'], ENT_COMPAT, 'utf-8'),$row_sections3['score2']))) {echo "checked=\"checked\"";} ?>/>
None: <input type="radio" name="answer[<?php echo $purpleronnie;?>]" value="<?php $row_sections3['score3'];?>" <?php if (!(strcmp(htmlentities($row_sections3['answer'], ENT_COMPAT, 'utf-8'),$row_sections3['score3']))) {echo "checked=\"checked\"";} ?>/> <?php }
else if ($row_sections3['questiontype']=="4") {?>
<input type="checkbox" name="answer[]" value="<?php $row_sections3['score1'];?>" <?php if (!(strcmp($row_sections3['answer'],$row_sections3['score1']))) {echo "checked=\"checked\"";} ?>/><?php }
else if ($row_sections3['questiontype']=="5") {?>
<input type="text" name="answer[]" value="<?php echo $row_sections3['answer'];?>" size="3"/><?php }
else if ($row_sections3['questiontype']=="6") {?>
<input type="date" name="answer[]" value="<?php $row_sections3['answer'];?>" size="25"/><?php }
else if ($row_sections3['questiontype']=="7") {
$currentq = $row_sections3['questionid'];
mysql_select_db($database_auditing, $auditing);
$query_lists = sprintf("SELECT optioning, optionscore FROM tbllistoptions WHERE questionid = %s", GetSQLValueString($currentq, "int"));
$lists = mysql_query($query_lists, $auditing) or die(mysql_error());
$row_lists = mysql_fetch_assoc($lists);
$totalRows_lists = mysql_num_rows($lists);
?>
<select name="answer[]"><option value="0" <?php if (!(strcmp($row_sections3['answer'], ""))) {echo "selected=\"selected\"";} ?>>Please Select....</option>
<?php
do { ?>
<option value="<?php echo $row_lists['optionscore']?>"<?php if (!(strcmp($row_sections3['answer'], $row_lists['optionscore']))) {echo "selected=\"selected\"";} ?>><?php echo $row_lists['optioning'];?></option>
<?php
} while ($row_lists = mysql_fetch_assoc($lists));
$rows = mysql_num_rows($lists);
if($rows > 0) {
mysql_data_seek($lists, 0);
$row_lists = mysql_fetch_assoc($lists);
}
?>
</select><?php }
else if ($row_sections3['questiontype']=="8") {?> <textarea name="answer[]" cols="40" rows="5"><?php echo $row_sections3['answer'];?></textarea><?php } ?></td>
<td><a class="info" href="#"><img src="../images/question.png" class="img" longdesc="" onmouseover="MM_setTextOfTextfield('helptext','','<?php echo $row_sections3['help'];?>')" style="cursor:help;" /></a></td>
</tr>
<?php $purpleronnie++;
}
while($row_sections3=mysql_fetch_assoc($sections3));?>
</table>
</div>
</div>
<?php }?>
<input type="hidden" name="firsttime" value="<?php echo $row_saved['firstsave'];?>" /> <p> </p>
</div>
</form>
Thanks in advance!
Dave