Jump to content

Need help with Complex Form and Arrays


doubledee

Recommended Posts

I am so over my head right now that I don't even know where to begin asking for help?!  :'(  :'(  :'(

 

(This is regarding a Form which ask 10 Questions and then captures and processed up to 10 Answers.)

 

Originally, I had an answerArray where I captured and process all User Answers to Questions, and all was beautiful in the universe!

 

Then after being 95% done, I got the - not so brilliant - idea of dynamically generating my Q&A Form with Questions being pulled from the question table (along with Answers from the answer table).

 

Based on a recommendation from gizmola (??), I created a SECOND Question & Answer multidimensional array - thoughtsArray - which pulled together the Questions and Answers.  This made sense, because it is easier to populate the Form when the dataset is coming all from one place.

 

The problem is two-fold at this point...

 

1.) Is it a good or bad design to have answerArray to capture and process Form inputs, and then a thoughtsArray for display purposes??

 

2.) How do I design a Form which allows both my multidimensional thoughtsArray and my one-dimesnional answerArray to plug into it, specifically the name attribute??

 

 

Here was my original array...

foreach($_POST['answerArray'] as $q => $a){
	// Copy data from POST to PHP array.
	$answerArray[$q] = trim($a);

	if (strlen($answerArray[$q]) > 1024){
		// Invalid Answer.
		$errors[$q] = 'Answer cannot exceed 1024 characters.';
	}
}//End of VALIDATE FORM DATA

 

 

And here is my new display array...

// Populate PHP array with Questions & Answers.
$x=1;
while (mysqli_stmt_fetch($stmt7)){
	$thoughtsArray[$x] = array('questionID' => $questionID,
						'questionText' => $questionText,
						'answerText' => $answerText);
	$x=$x+1;
}

 

 

Here is how my original - pre automation - Form looked...

<!-- Question 1 -->
<label for="question1">1.) Why did you decide to start your own business?</label>
<textarea id="question1" name="answerArray[1]" cols="60" rows="2"><?php if (isset($answerArray[1])){echo htmlentities($answerArray[1], ENT_QUOTES);} ?></textarea>
<?php
	if (!empty($errors[1])){
		echo '<br /><span class="error">' . $errors[1] . '</span>';
	}
?>

 

 

And here is my new Form - which works for displaying, but is now broken for processing...

<?php
	foreach($thoughtsArray as $questionNo => $qaArray){
		// Build Question.
		echo '<label for="question' . $questionNo . '">' . $questionNo . '.) ' . $qaArray['questionText'] . "\n";

		// Build Answer.
		echo '<textarea id="question' . $questionNo . '" name="' . $qaArray["questionID"] . '" cols="60" rows="2">';
		echo (isset($questionNo) ? htmlentities($qaArray['answerText'], ENT_QUOTES) : '');
		echo "</textarea>\n\n";
	}
?>

 

Hope that all makes sense?!

 

 

Debbie

 

Link to comment
Share on other sites

1) It's not necessarily good or bad design. Its a matter of opinion and preference. You have to weigh out the costs and benefits of either way of doing it. Personally, I keep the variables separate as only the data coming from the database is the accurate / stored data. So displaying it is the best way to ensure data is being saved and not just showing what the user posted in assumption that it was saved.

 

2) Processing the question is simple. You've named the fields "question"+ID. So, you could check the answer values like this:

foreach ($_POST as $key => $value) { 
  if (substr($key, 0,  == "question") {
    $answer[substr($key, ] = $value;
  }
}

This would pull the answers into the answer array and allow you to validate then post to your database.

 

EDIT: I misread the id as the name attribute. Although you could change the name to the way I described to pull your answers. (i'm on my tablet and there are no scrollbars on the code blocks)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.