Re: Submitting Dynamic Form
| From: | Chris MacKenzie | Date: | Wed, 03 Apr 2002 14:46:41 +0000 |
| Subject: | Re: Submitting Dynamic Form | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-18164@lists.php.net to get a copy of this message | ||
Hi Rick,
Here's a code snippet (less error checking). Basically there could be as
many as twnety questions listed with their associated listboxes of
possible answers. Also be aware that ms-sql identity type is similar to
mysqls autoincrement type except that once a record is deleted, that
identity value is never used again.
My problem is that since the question_id field is never guaranteed to be
sequential and seeing that I have named the answer listboxes with the
question_id field, what is a good way to extract the values from the
form and perform an update query ?
How can I do this if I don't know what the question_id values will be ?
-- CUT
$sql= "SELECT * FROM tbl_exam_questions ORDER BY question_id;";
$result=mssql_query($sql,$conn1);
$numrows = mssql_num_rows($result);
print '<form method="POST" action="submit_exam_answers.php"
name="F1">';
## List All Questions and load the possible answers into list box.
for($cnt = 0; $cnt < $numrows; $cnt++){
$row = mssql_fetch_array($result);
print '<p>'.$row["question_text"].'</p>';
print 'My Answer is: <select size="1"
name="'.$row["question_id"].'"><option selected
value="0">- SELECT ONE
-</option>';
## Now fetch the possible answers and load it into the list box.
$pa_sql = "SELECT * FROM tbl_exam_answers WHERE question_id =
".$row["question_id"].";";
$pa_result = mssql_query($pa_sql,$conn1);
$pa_numrows = mssql_num_rows($pa_result);
for($pa_cnt = 0; $pa_cnt < $pa_numrows; $pa_cnt++) {
$pa_row = mssql_fetch_array($pa_result);
print '<option
value="'.$pa_row["id"].'">'.$pa_row["p_answers"].'</option>';
}
print '</select><br><hr><br>';
## Finished Loading Possible Answers into list box.
}
print '<p><input type="submit" value="Save Answer"
name="Submit"><input
type="reset" value="Reset Form"
name="Reset"></p></form>';
-- CUT
Oh, and before I forget tbl_exam_questions.correct_answer holds the
relevent id entry from tbl_exam_answers
> what happened when you extracted the form field names from the DB?
>
>> Hi All,
>>
>> I'm pretty new to the whole php thing and I'm currently making an
>> multiple choice exam type of thing with php/mssql.
>>
>> The two tables concerned are called tbl_exam_questions and
>> tbl_exam_questions which are defined like so.
>>
>> [tbl_exam_questions] (
>> [question_id] [int] IDENTITY (1, 1) NOT NULL ,
>> [question_text] [varchar] (355) NOT NULL ,
>> [correct_answer] [int] NOT NULL
>> ) ON [PRIMARY]
>>
>> [tbl_exam_answers] (
>> [id] [int] IDENTITY (1, 1) NOT NULL ,
>> [question_id] [int] NOT NULL ,
>> [p_answers] [varchar] (255) NOT NULL
>> ) ON [PRIMARY]
>>
>> What I'm trying to do is to pull all the questions out and display them
>> with the possible answer in a html form and dropbox.
>> I'm displaying the entire exam on one page, with one submit button on
>> the bottom so that the student can review the answers before finally
>> submitting them.
>>
>> The problem I have is that the form field names are dynamic in that they
>> are set up the value held in tbl_exam_answers.question_id and with
>> ms-sql the identity type is not guarenteed to be incremental, so how can
>> I reference them to send a query back to the DB to mark the student ?
>>
>> Have I painted myself into a corner ? :-/