Page 1 of 1

Online Survey Form loop question [SOLVED]

Posted: Thu Aug 16, 2012 5:11 pm
by robertb13
I am a super newby...

I created an online survey. The questions are stored in the database. I created the form below, but cannot figure out how to get the loop to run and have ONE SUBMIT button. Can figure out how to take it outside of the loop...

HELP!


Code: Select all

<?php

/* ################################ */
/* ###### Sandbox Document ######## */
/* ################################ */



/* ################################ */
/* #### Get Survey Questions ###### */
/* ################################ */

function getQuestions($dbc) {

	$query = "SELECT * FROM survey_questions";
	$result = mysqli_query($dbc, $query);
	
	if ($result) {
		while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
			$qNum = $row['question_id'];
			$body = $row['question_body'];
			
			echo '
<div class="entry">
<span class="qTitle">'. $qNum . '. ' . $body.'</span>
<form action="index.php" method="post">
<input type="text" name="answer" size="5" />


<input type="submit" value="Submit" name="submit" />
<input type="hidden" name="questionid" value="questionid" />
<input type="hidden" name="submitted" value="1" />
</form>
</div>'
	
} //END if
} //END while
} //END function getQuestions
?>

Re: Online Survey Form loop question

Posted: Thu Aug 16, 2012 5:18 pm
by social_experiment
robertb13 wrote: but cannot figure out how to get the loop to run and have ONE SUBMIT button.
You only need to move the code creating the button out of the loop code.

Code: Select all

 // rest of code


} //END if
} //END while
} //END function getQuestions
 // move button code to here
<input type="submit" value="Submit" name="submit" />
<input type="hidden" name="questionid" value="questionid" />
<input type="hidden" name="submitted" value="1" />
</form>
</div>'
?>

Re: Online Survey Form loop question

Posted: Thu Aug 16, 2012 5:37 pm
by robertb13
This is what I did...
I added a single ' to the end the first part of the form & added single quotes to each side of the second part so they would be strings.
Getting a syntax error... on the } //END if line.

function getQuestions($dbc) {

$query = "SELECT * FROM survey_questions";
$result = mysqli_query($dbc, $query);

if ($result) {
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$qNum = $row['question_id'];
$body = $row['question_body'];

echo '
<div class="entry">
<span class="qTitle">'. $qNum . '. ' . $body.'</span>
<form action="index.php" method="post">
<input type="text" name="answer" size="5" />'

} //END if
} //END while
} //END function getQuestions

'<input type="submit" value="Submit" name="submit" />
<input type="hidden" name="questionid" value="questionid" />
<input type="hidden" name="submitted" value="1" />
</form>
</div>'
?>

Re: Online Survey Form loop question

Posted: Fri Aug 17, 2012 1:24 am
by social_experiment

Code: Select all

echo '
<div class="entry">
<span class="qTitle">'. $qNum . '. ' . $body.'</span>
<form action="index.php" method="post">
<input type="text" name="answer" size="5" />'; // <- missing a semi-colon here

} //END if
} //END while
} //END function getQuestions

// add 'echo' to print the statement
echo '<input type="submit" value="Submit" name="submit" /> 
<input type="hidden" name="questionid" value="questionid" />
<input type="hidden" name="submitted" value="1" />
</form>
</div>'; // missing a semi-colon here
hth

Re: Online Survey Form loop question

Posted: Fri Aug 17, 2012 3:04 am
by robertb13
That worked! Thanks!

Re: Online Survey Form loop question

Posted: Fri Aug 17, 2012 3:16 am
by robertb13
1st post here. Question: Am I supposed to mark this as "resolved" somewhere?

Re: Online Survey Form loop question

Posted: Fri Aug 17, 2012 3:20 am
by social_experiment
You can add [SOLVED] (or something similar) to the subject line of the 1st post of the thread :)

Re: Online Survey Form loop question

Posted: Fri Aug 17, 2012 3:20 pm
by robertb13
[solved] doesn't show in the forum post, only on the 1st post...

Re: Online Survey Form loop question

Posted: Sat Aug 18, 2012 4:07 am
by social_experiment
viewtopic.php?f=1&t=136443
An example ^. By changing the initial post title you change it so the [solved] is visible to those viewing a specific section of the forum, Board Index for example which informs other posters that the problem has been resolved