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

 

 

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.