0

What I need to do is retreive all the values of the input fields and store them in the database after pressing a submit button.

Here is what I'm doing:

I retrieve an unknown number of rows from the query, I created an ID dynamically for every input field that can be filled. (so if I get 5 rows, I have 10 input fields, if I get 10 rows, 20 input fields)

This piece:

    <input class="goals" type="number" id="'.$row[SCHE_WED].'_'.$row[SCHE_LAND_ID1].'" value="" maxlength="3" '.$active.'/>

How do I get all the id="'.$row[SCHE_WED].'_'.$row[SCHE_LAND_ID1].'"'s into an array? I already tried input name="". (but when using an array, while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) it doesn't like name="" so I changed that to id="")

What I used in another form with the name="" for an input field is this:

    $fields = array('','','','',''); // Add all your field names to an array
    $data = array();
    foreach ($fields as $field)
    {
     if (isset($_POST[$field]))
     {
      $data[$field] = $_POST[$field];
      ${$field} = $_POST[$field];
 }
    }

But I don't know how or can find how to put the dynamically generated id's in something similar for my code (see below).

if (sqlsrv_has_rows($stmt))
{
    while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) )
    {
    $dateconvert = $row[SCHE_DATE];
    $converted = date("d-m-yy", strtotime($dateconvert));
    echo    '
<tr>
    <td style="text-align:center"; colspan="15">Wedstrijd en plaats gegevens voor wedstrijd '.$row[SCHE_WED].'</td>
</tr>
<tr>
    <td></td>   
    <td>Stad: '.$row[Stdn_stad].'</td>
    <td></td>
    <td>Inwoners Stad: '.$row[STDN_INWNR_CAP].'</td>
    <td></td>
    <td>Stadion naam: '.$row[STDN_NAAM].'</td>
    <td></td>
    <td>Capaciteit Stadion: '.$row[STDN_CAP].'</td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
</tr>
<tr>
    <td></td>
    <td>Datum: '.$converted.'</td>
    <td></td>
    <td>Tijd: '.$row[SCHE_TIME].'</td>
    <td></td>
    <td style="text-align:right";>Thuis: '.$row[SCHE_LAND1].'</td>
    <td> - </td>
    <td>Uit: '.$row[SCHE_LAND2].'</td>
    <td></td>
    <td><input class="goals" type="number" id="'.$row[SCHE_WED].'_'.$row[SCHE_LAND_ID1].'" value="" maxlength="3" '.$active.'/></td>        
    <td> - </td>
    <td><input class="goals" type="number" id="'.$row[SCHE_WED].'_'.$row[SCHE_LAND_ID1].'" value="" maxlength="3" '.$active.'/></td>
    <td></td>
    <td>Totokruisje: <input class="goals" type="number" id="toto_'.$row[SCHE_WED].'" value="" maxlength="3" '.$active.'/></td>
    <td></td>
</tr>
  ';

    }
}           
  echo '
<tr>
    <td colspan="15"></td>
</tr>
  </table>
  </div>
  ';
  ?>
2
  • What are the differences between one goal and the next? Will you be connecting different parent IDs to different goals? Or do you have a single parent ID for all the goals in the form? Commented Apr 19, 2014 at 14:09
  • the goals fields are input fields for users to predict outcome on soccer games. so goals = a prediction on soccer game outcome. the .$row[SCHE_WED].'_'.$row[SCHE_LAND_ID1]. creates an unique id for SCHE_WED = game number and the SCHE_LAND_ID1 is for the first team and the SCHE_LAND_ID2 is for the second team. Commented Apr 19, 2014 at 14:14

1 Answer 1

1

One idea is you can create two input arrays for the goal+team combo.

You could repeat this combo for as many goals as you need.

<input type="text" name="teams[]" value="" />
<input type="number" name="goals[]" value="" />

Then when your form submits, you have two arrays: $_POST['teams'] and $_POST['goals']. You can use the index to match teams to goals like:

foreach($_POST['teams'] as $key => $team) {
   echo $team;
   $goal = $_POST['goals'][$key];
   echo $goal;
}

And process as needed. You can add the form elements dynamically with jQuery like this: http://jsfiddle.net/6G7yB/6/

Hope this helps

Edit:

You can also do something like this: http://www.php.net/manual/en/reserved.variables.post.php#87650

Sign up to request clarification or add additional context in comments.

2 Comments

can you give me an example how to build the for each array with 3 variables? like, teams,goals and for instance, place? foreach($_POST['teams'] as $key => $team) { $goal = $_POST['goals'][$key]; And thanks :) your solution helped me a great deal, it works for what i'm trying to do :) $place = $_post['place'][and then goals and teams];}? So I can use that combination to store in an insert query... }
Never mind my question, I found the solution how to combine the foreach as $key answer here: stackoverflow.com/questions/22177958/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.