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
robertb13
Forum Newbie
Posts: 22 Joined: Thu Aug 16, 2012 5:04 pm
Post
by robertb13 » Thu Aug 16, 2012 5:11 pm
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.
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Thu Aug 16, 2012 5:18 pm
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
Post
by robertb13 » Thu Aug 16, 2012 5:37 pm
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>'
?>
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Fri Aug 17, 2012 1:24 am
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
Post
by robertb13 » Fri Aug 17, 2012 3:04 am
That worked! Thanks!
robertb13
Forum Newbie
Posts: 22 Joined: Thu Aug 16, 2012 5:04 pm
Post
by robertb13 » Fri Aug 17, 2012 3:16 am
1st post here. Question: Am I supposed to mark this as "resolved" somewhere?
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Fri Aug 17, 2012 3:20 am
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
Post
by robertb13 » Fri Aug 17, 2012 3:20 pm
[solved] doesn't show in the forum post, only on the 1st post...
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Sat Aug 18, 2012 4:07 am
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