Online Survey Form loop question [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Online Survey Form loop question [SOLVED]

Post 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
?>
Last edited by robertb13 on Tue Aug 28, 2012 1:52 pm, edited 3 times in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Online Survey Form loop question

Post 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>'
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Re: Online Survey Form loop question

Post 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>'
?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Online Survey Form loop question

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Re: Online Survey Form loop question

Post by robertb13 »

That worked! Thanks!
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Re: Online Survey Form loop question

Post by robertb13 »

1st post here. Question: Am I supposed to mark this as "resolved" somewhere?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Online Survey Form loop question

Post by social_experiment »

You can add [SOLVED] (or something similar) to the subject line of the 1st post of the thread :)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Re: Online Survey Form loop question

Post by robertb13 »

[solved] doesn't show in the forum post, only on the 1st post...
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Online Survey Form loop question

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply