Jump to content

Need help with HTML Form to Array to Database


Recommended Posts

I've had an online form that would be processed in this way:

 

Insert the User's information - name, email, school - into a database. 

Then it would email me that information as well as the responses to 64 questions.

I'd like to now put those answers in a data table.

 

Here is how those responses would be sent to me, with their form (drop down items) item name:

 

$message .= $_POST['1'] . "\n";
$message .= $_POST['2'] . "\n";
$message .= $_POST['3'] . "\n";
$message .= $_POST['4'] . "\n";
....
......... all the way down to 64
....
$message .= $_POST['64'] . "\n\n";

 

I have a data table with the following columns: (user_info)

ID

nameFirst

nameLast

email

school

 

I have another table with the following columns:  (user_answer)  -->  this is the table I need help on

ID

uID

section --> that corresponds with the form item name

answer --> actual answer chosen

 

 

For each uID, I'll have 64 ID's, so it will be for example:

 

1  1  1  Munster

2  1  2  Valparaiso

......

64  1  64  Tecumseh

65  2  1  Lake Central

66  2  2  Merrillville

 

 

 

The question I have (it may be a simple one) is how do I get those 64 answers from a User into an Array that I can start building a Loop to Insert?

 

 

You would use an array for the form name='...' attribute.

 

name='question[1]'

name='question[2]'

...

 

Then $_POST['question'] will be an array of the submitted values -

 

$_POST['question'][1]

$_POST['question'][2]

...

Here's some sample code that shows how to form a multi-value insert query -

 

<?php
// fake for testing
$uid = 123; // get the user id from wherever you have it at

// validate/filter/escape the data here... (use array_map() or array_walk() to operate on the entire $_POST['question'] array at one time)

// multi-value insert query with uID, section (1-64), answer
foreach($_POST['question'] as $key=>$value){
$data[] = "$uid,$key,'$value'";
}
$query = "INSERT INTO user_answer (uID,section,answer) VALUES (". implode('),(',$data) .")";

echo $query;


// fake some options for testing
$options = "<option value='none'>Select Answer</option>\n<option value='a'>a</option>\n<option value='b'>b</option>\n<option value='c'>c</option>\n";

$form = "<form action='' method='post'>\n";
$questions = range(1,64);
foreach($questions as $question){
$form .= "Question $question: <select name='question[$question]'>$options</select><br />\n";
}
$form .= "<input type='submit' name='submit'></form>";
echo $form;

Do I need to re-write the form in PHP? 

 

Here is a sample drop down with the choices.  The "name" is section number I need to insert.  I figured I could duplicate that by counting instances in my foreach loop.  But sticking to the question at hand, will I need to wrap all 64 of my drop downs in PHP?

 

 

              <select name="1">
		  <option selected="selected">____select team</option>
		  <option>1. Hammond Morton, 7-13</option>
		  <option>2. Highland, 10-11</option>
		  <option>3. Gary West, 11-10*</option>
		  <option>4. Munster, 20-0</option>
		  <option>5. Lake Central, 12-8</option>
		  <option>6. Lowell, 12-8</option>
		  <option>7. East Chicago Central, 13-7</option>
              </select>

Aren't you already using php to produce the form, so that you can easily change it by simply changing the data structure (array/database table) that defines the list of questions? I'm pretty sure that no one here would even want to type out or copy/paste/alter and then debug the html needed to make 64 similar form elements. Let the computer do that work for you.

 

You don't have to do anything that is suggested, but what I posted above is the optimum way of doing what you asked. It takes the minimum amount of code and executes the fastest.

I think I was editing while you were responding.  Sorry about that. 

 

The form is set up purely in HTML (.php page) and posts Action to a .php page.  I've had the form for about five years.  Only now and going forward am I wanting to put the results from Users into a database.  This year I'm wanting to put the User's choices in a data table. 

Since you are listing the team record as part of the displayed information in the form, you would want to dynamically produce the form using php code so the information would be current for every season (gotten out of an array/database table that defines/list the teams) without you manually editing the html in the page.

Since you are listing the team record as part of the displayed information in the form, you would want to dynamically produce the form using php code so the information would be current for every season (gotten out of an array/database table that defines/list the teams) without you manually editing the html in the page.

 

True  :)

...but when I set up the form, I wasn't really into databases.  I copied entries on to a spreadsheet as they came in.  (It's a week long contest.)  Their team number and record were just visually part of the answer.  Their record is more of an aid to the User, and the team number is also just a reference, indicating where they are in a bracket.

 

I'm OK with that all being part of the answer for now.  I'm not really displaying anything beyond the number of correct choices after it's over, and perhaps the number of Users who picked certain teams.  Going forward, I'd create a more dynamic form, but for now it's in HTML. 

 

Do I need to convert it to php to create the array need to insert into a database?

 

 

 

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.