Form challenge - Still a newby!

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

Form challenge - Still a newby!

Post by robertb13 »

I created a form to access the database and pull questions.
I only want one submit button. As such, I understand the submit button must be outside the loop.

Each loop creates an "answer" and I want those answers to be included in the database so I can echo them on the page that is accessed by submit.

I'm lost. Any of your veteran coders help me walk through this one?
Or suggest a better way to take a list of questions from a database, allow the user to answer a particular section of them and show the results on submit.
Is there a way to assign a DIFFERENT variable to the "answer" based on each pass? Are the 'answers' stored in an array automatically on each pass? I am still learning and appreciate the help....

BTW, this is the output http://www.robertbartz.com/survey/01Sur.php which is great. I just need to be able to put the answers in a database...

Code: Select all


function getQuestions($dbc) {
	
	$survID = $_SESSION["surveyid"];
	$ansSizS = $_SESSION["answerSize"];
	$query = 'SELECT * from survey_questions WHERE survey_id = ' . $survID . '';
	$result = mysql_query($query);

	$qNum = 1;
									
	if ($result) {
		while ($row = mysql_fetch_array($result, MYSQLI_ASSOC)) {

/* ################################ */
/* #### Define Post Variables ##### */
/* ################################ */

												
			$body = $row['question_body'];
			$ansSiz = $row['answer_length'];		
			
		if ($ansSiz==0) {

			echo '
				<div class="entry">
				<span class="qTitle">'. $qNum . '. ' . $body.'</span><br/>
					<form action="/survey/functions/sandbox2.php" method="post">
						<input type="text" name="answer' . $qNum . '" size="' . $ansSizS . '" /><br/>
				</div>';
			
			} else {
			echo '
				<div class="entry">
				<span class="qTitle">'. $qNum . '. ' . $body.'</span><br/>
					<form action="/survey/functions/sandbox2.php" method="post">
						<input type="text" name="answer' . $qNum . '" size="' . $ansSiz . '" /><br/>
						<input type="hidden" name="questionid" value="' . $qNum . '" />
				</div>';
			}
} //END if
} //END while


			echo '<div class="enrty"><input type = "submit" value="submit" />
					</form>
				  </div>'; 
Last edited by robertb13 on Tue Aug 28, 2012 2:19 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Form challenge - Still a newby!

Post by requinix »

Keep just one form for the whole thing, but use checkboxes and arrays for more control over the fields.

Example:

Code: Select all

<?php

if ($_POST) {
	$checkboxes = (isset($_POST["checkbox"]) && is_array($_POST["checkbox"]) ? $_POST["checkbox"] : array());
	$textboxes = (isset($_POST["textbox"]) && is_array($_POST["textbox"]) ? $_POST["textbox"] : array());
	foreach ($checkboxes as $key => $value) {
		if (isset($textboxes[$key])) {
			echo "<p>Textbox {$key} = {$textbox[$key]}</p>";
		}
	}
}

?>
<form method="post">
<p>Textbox 1: <input type="text" name="textbox[1]" />
	<input type="checkbox" name="checkbox[1]" /> Enable</p>
<p>Textbox 2: <input type="text" name="textbox[2]" />
	<input type="checkbox" name="checkbox[2]" /> Enable</p>
<p>Textbox 3: <input type="text" name="textbox[3]" />
	<input type="checkbox" name="checkbox[3]" /> Enable</p>
</form>
You can enter whatever you want in any of the textboxes but the values will only be displayed if you hit the associated checkbox.
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Re: Form challenge - Still a newby!

Post by robertb13 »

Thanks for the swift reply!

I really want them to be required to answer each question as the bulk of the results show the issue they are having.
Is there any other way than checkboxes? I think a survey with checkboxes would look unprofessional as well.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Form challenge - Still a newby!

Post by requinix »

Then you don't need checkboxes. That was only an example showing how you could ignore some POSTed fields even though they were submitted.

They have to answer everything? Still use an array, but you can go through that array looking for empty responses.
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Re: Form challenge - Still a newby!

Post by robertb13 »

Can you break this code down?

Not sure how it fits to "submit" the data
and be imported into the database...
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Form challenge - Still a newby!

Post by requinix »

Your HTML can look like this:

Code: Select all

<form>

	<boilerplate>
		Question 1: <answer name="answer[1]" />
	</boilerplate>
	<boilerplate>
		Question 2: <answer name="answer[2]" />
	</boilerplate>
	<boilerplate>
		Question 4: <answer name="answer[4]" />
	</boilerplate>

</form>
(with appropriate tags and elements, not actually <boilerplate /> and <answer />)

When you name the inputs as "answer[X]" then $_POST["answer"] is an array of everything. You get it all in the one submit.
Then loop through that array and make sure that there aren't any empty answers. As you do so the array key is the question number (or ID number or anything unique) and the array value is the answer.
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Re: Form challenge - Still a newby!

Post by robertb13 »

If I use "answer[]" without a number, will it auto-create the answer numbers?
Reason being is that I'd like to populate the form questions via WHILE{} from a database.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Form challenge - Still a newby!

Post by requinix »

Yes, but it would be harder to associate an answer with the question it's for. Use the question ID as the array key.
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Re: Form challenge - Still a newby!

Post by robertb13 »

<answer name="answer[1]"

Is the [1] in the part you suggested above supposed to be the questionID?

Sorry to be a pain, but I'm lost.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Form challenge - Still a newby!

Post by requinix »

Yeah, I didn't go into much detail about it...

It's basically the same thing as in actual PHP code, meaning you can use [] to let PHP create sequential items or [X] to put your own key (be it numeric or textual). So

Code: Select all

<input type="text" name="textbox[key][0]" value="Text" />
is like

Code: Select all

$_POST["textbox"] = array("key" => array("Text"));
With something like this it's really easy to keep the unique thing (like question ID) as the array key - like you might do in an array in code.
Post Reply