0

I have a page where:

  1. Users register to a page (all data stored in a database)
  2. They log in with their username

When they have logged in they are supposed to fill a HTML form with 48 selects and 5 input fields. The select boxes looks as the examples below.

<td>Team 1</td> 
    <select class="combobox" id="m0">
       <option>Not chosen</option>
       <option>1</option>
       <option>X</option>
       <option>2</option>
    </select>
<td>Team 2</td>

<td>Team 3</td> 
    <select class="combobox" id="m1">
      <option>Not chosen</option>
      <option>1</option>
      <option>X</option>
      <option>2</option>
    </select>
<td>Team 4</td>

etc..

What I do today is when the user presses the Done button I call:

onclick="getOptionValues()"

And I loop through all selects and get the values from my input fields and then send out a mail with all the answers, BUT I would like to save all this data to a database instead and connect it to the unique username so when a user login I can populate all the data they have selected before. I have a table in my database called "results" and I have created columns for each answer (54 columns now...), but is there a smarter way to do this and how do you in the code INSERT these to the database? So my question is:

  1. How do you in a smart way create the database so that you can add 48 checkbox answers and 5 input fields (is it the way that I have done?)
  2. How do I in the code add them to the database (is it possible to make a loop?)?
3
  • 1
    Those aren't checkboxes, they're selects. There's a difference. Commented Jun 8, 2014 at 8:55
  • I think you can store this question and answers in json type and you can access them in easy way . Commented Jun 8, 2014 at 8:55
  • @AmirHabibzadeh What? Why? Just submit the form normally. Commented Jun 8, 2014 at 8:56

2 Answers 2

1

Actually, you don't need JavaScript for this at all.

Surround your form with proper <form> tags, make sure to add a method= and action= attributes properly.

Give all your inputs, selects and textareas name= attributes. If you have repeating and related inputs, name them all somename[] and it will display as an iterable array in PHP.

Remove the JavaScript from your form. You don't need it... yet (we'll get there).

On the PHP side, make sure you have a page that matches the URL in the action= attribute of the form, which accepts GET/POST requests as dictated by the method= of the form.

Do whatever processing you want in PHP, including validation, database operations and mailing with the data. You'll see it in the $_POST superglobal.


When would you need JavaScript?

  • Validate the form inputs on the client side, to help the user. This should not be instead of server side validation, but in addition!
  • Use AJAX to submit the post without leaving the page.
Sign up to request clarification or add additional context in comments.

Comments

0

Please pardon as i just typed this up on the fly but If you know your fields are static and match the field names in the database you can dynamically build your MySQL query with a script something akin to this:

<?PHP
if ($_POST) {
    $selections = array();
    for ($1 = 1; $i <= 48; $i++) {
        if (isset($_POST['m'.$i]) && !empty($_POST['m'.$i])) {
            $selections['m'.$i] = 1; // presumably "checked" so probably binary in the DB
        }
    }
    foreach ($selections as $key => $value) {
        $fieldNames  .= " ".$key.",";
        $fieldValues .= " '".$value."',";
    }
    if (isset($_POST['field1'] && !empty($_POST['field1'])) {
       $fieldNames  .= " field1,";
       $fieldValues .= " '".$_POST['field1']."',";
    }
    if (isset($_POST['field2'] && !empty($_POST['field2'])) {
       $fieldNames  .= " field2,";
       $fieldValues .= " '".$_POST['field2']."',";
    }
    if (isset($_POST['field3'] && !empty($_POST['field3'])) {
       $fieldNames  .= " field3,";
       $fieldValues .= " '".$_POST['field3']."',";
    }
    if (isset($_POST['field4'] && !empty($_POST['field4'])) {
       $fieldNames  .= " field4,";
       $fieldValues .= " '".$_POST['field4']."',";
    }
    if (isset($_POST['field5'] && !empty($_POST['field5'])) {
       $fieldNames  .= " field5,";
       $fieldValues .= " '".$_POST['field5']."',";
    }
    $fieldNames  = substr($fieldNames ,0,(strlen($fieldNames) -1)); // remove trailing commas
    $fieldValues = substr($fieldValues,0,(strlen($fieldValues)-1)); // remove trailing commas
    $query = "INSERT INTO table_name (";
    $query .= " ".$fieldNames." ";
    $query .= ") values (";
    $query .= " ".$fieldValues." ";
    $query .= ")";
    // Now you use your DB connection to execute the MySQL query.. 
    // Mind you, this changes a bit to be arrays if you're using PDO, but this can work.
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.