Need help passing survey responses from HTML form to MySQL table

patje

New Member
I'm a bit stumped here...I've got a web page that displays a list of survey questions that are stored in a db table, and I'm trying to gather ratings responses on each question (strongly disagree -> strongly agree) and pass the values back to another table in my db.Here's a snippet of the form code:\[code\]<form action="submitAnswers.php" name="subAns" method="post"> <fieldset> <legend>Survey Questions</legend> <input type="hidden" name="survey_id" value="http://stackoverflow.com/questions/13864458/<?php echo ($survey_id); ?>"> <table width=90% border=0 cellpadding='2' cellspacing='2'?> <tr bgcolor=#D0D0D0> <th>Question</th> <th>Strongly Disagree</th> <th>Somewhat Disagree</th> <th>Neither Disagree nor Agree</th> <th>Somewhat Agree</th> <th>Strongly Agree</th> </tr> <tr> <?php while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) { echo "<td bgcolor='#E8E8E8'><font size='-1'>$row[quest_order]) $row[quest_text]</font></td>"; for ($i = 0; $i < 5; $i++) { //echo "<input type='hidden' name='qID' value='http://stackoverflow.com/questions/13864458/$quest_id'>"; echo "<td bgcolor='#E8E8E8'><input type='radio' name='$row[quest_id]' value='http://stackoverflow.com/questions/13864458/$i'></td>"; } echo "</tr>"; } ?> <br> </table><br> <input type="submit" name="submitAnswers" value="http://stackoverflow.com/questions/13864458/Submit Survey"></fieldset> </form>\[/code\]And here's the PHP in the 'submitAnswers.php' file:\[code\]$survey_id=$_POST['survey_id']; $sql = "INSERT INTO survey_responses (survey_id) VALUES ('$survey_id')"; $res = send_sql($sql, $link, $db); foreach($_POST['quest_id'] as $id=>$value) { $sql = "INSERT INTO survey_answers (response_id, quest_id, response_value) VALUES (LAST_INSERT_ID(), $id, '$value')"; $res = send_sql($sql, $link, $db) or die("Cannot add survey"); }\[/code\]My insert statement into the 'survey_responses' table works just fine, but I don't know how to correct my radio buttons used to obtain the response score for each question. The values are being passed with POST (I confirmed this by looking at \[code\]print_r($_POST);\[/code\], which returned, for example: \[quote\] Array ( [survey_id] => 4 [6] => 0 [5] => 1 [4] => 2 [3] => 3 [2] => 4 [1] => 3 [submitAnswers] => Submit Survey )\[/quote\]but clearly I'm going about this wrong, as I'm not able to successfully iterate through and insert the question id# and response value for each one. I'm sure this is relatively simple, but I'm still new to programming and have been going over this for hours with little to no luck.Just hoping someone can help me fix this, and hopefully in the process better explain, or point me to a good resource covering how to handle dynamically sized forms that are retrieving database records and passing various amounts of information back to the db.Thanks!
 
Back
Top