Jump to content

Storing multiple Form fields in an Array


doubledee

Recommended Posts

I have a Form that has 10 questions.

 

When the User completes the Form, each Response needs to be a separate record.

 

(My database has a many-to-many relationship with a "member" and "question" table joined by a "answer" table in the middle.  And each Form field will go into its own unique record to make the many-to-many work.)

 

At the top of my Form I have...

if ($_SERVER['REQUEST_METHOD']=='POST'){
	// Form was Submitted (Post).

	// Initialize Errors Array.
	$errors = array();

	// Trim all form data.
	$trimmed = array_map('trim', $_POST);


	// ************************
	// Validate Form Data.		*
	// ************************

	// Validate Answer1.
	if (strlen($trimmed['answer01']) >= 2 && strlen($trimmed['answer01']) <= 1024){
			// Valid Answer1.
			$answerArray[0] = $trimmed['answer01'];
	}else{
			// Invalid Answer1.
			$errors['question01'] = 'Answer must be 2-1024 characters.';
	}//End of VALIDATE ANSWER1


	// Validate Answer2.
	// :
	// :
	// Validate Answer10.

 

 

And down farther in my code, I have each Field in my Form set up like this...

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

 

 

So if my Form works like I am thinking, all 10 Form Fields would end up here...

$answerArray[0]
$answerArray[1]
$answerArray[2]
$answerArray[3]
$answerArray[4]
$answerArray[5]
$answerArray[6]
$answerArray[7]
$answerArray[8]
$answerArray[9]

 

 

First of all, does that sound correct?

 

 

If that part is correct, then how do I take the $answerArray[] and INSERT each value into a different record in my "answer" table??

 

Hope that makes sense?!

 

Thanks,

 

 

Debbie

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.