Basically, I am able to display the DIVs based on the selection of drop box. now my problem is : I am able to insert data when there is only single text field. When there are 2 text fields, the submission is a success, but the data cannot be found in the database. 1 row in the database is taken though.
<?php
//database
$connection = mysql_connect("localhost", "root", "") or die (mysql_error());
mysql_select_db("survey" , $connection) or die (mysql_error());
if(isset($_POST['submit'])){
$surveyID =$_POST['surveyCategory'];
for($i=0; $i<count($_POST['id']); $i++){
$questionID = $_POST['id'][$i];
$answer = mysql_real_escape_string(htmlspecialchars($_POST['answer'][$i]));
mysql_query("INSERT INTO answers(survey_id, question_id, answer_body) VALUES ('$surveyID', '$questionID', '$answer')") or die (mysql_error());
}
}
?>
<style>
div{
display: none;
}
</style>
<html>
<body>
<form name=displayQuestion method="post">
Survey Categories :
<select name="surveyCategory" id="surveyCategory">
<option> Choose Survey Category </option>
<?php
$surveyQuery = "SELECT survey_id, survey_name FROM surveys";
$result = mysql_query($surveyQuery) or die (mysql_error());
while($menu=mysql_fetch_assoc($result)){
echo "<option value=$menu[survey_id]>$menu[survey_name]</option>";
}
?>
</select>
<!-- if selection is auction -->
<div id="1" style="display:none">
<?php
$auctionSurvey = "SELECT question_id, survey_id, question_body FROM questions
WHERE survey_id='1'";
$aucResult = mysql_query($auctionSurvey) or die (mysql_error());
while($auctionRow = mysql_fetch_assoc($aucResult)){
echo $auctionRow['question_body'] . "<input type=text name=answer[]><BR>";?>
<input type="hidden" name="id[]" value="<?php echo $auctionRow['question_id']?>">
<?php
}
?>
<input type="submit" name="submit" value="Submit">
</div>
<!-- if selection is Competition -->
<div id="2">
<?php
$compSurvey = "SELECT survey_id, question_body FROM questions
WHERE survey_id='2'";
$compResult = mysql_query($compSurvey) or die (mysql_error());
while($compRow = mysql_fetch_assoc($compResult)){
echo "$compRow[question_body]" . "<input type=text name=><BR>";
}
?>
<input type="hidden" name="id" value="<?php echo "$compRow [question_id]"?>">
<input type="submit" name="submit" value="Submit">
</div>
<!-- if selection is Gallery -->
<div id="3">
<?php
$gallerySurvey = "SELECT survey_id, question_body FROM questions
WHERE survey_id='3'";
$galleryResult = mysql_query($gallerySurvey) or die (mysql_error());
while($galleryRow = mysql_fetch_assoc($galleryResult)){
echo " $galleryRow[question_body]" . "<input type=text name=answer><BR>";
}
?>
<input type="hidden" name="id" value="<?php echo "$galleryRow [question_id]"?>">
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<script>
document.getElementById('surveyCategory').onchange = function() {
var i = 1;
var myDiv = document.getElementById(i);
while(myDiv) {
myDiv.style.display = 'none';
myDiv = document.getElementById(++i);
}
document.getElementById(this.value).style.display = 'block';
};
</script>