php - How to collect values of input fields from a form displayed with a loop

616

I have a form in which input fields are displayed via a while loop with but I'm having issues with collecting the data from the input fields to save to the DB. What should be the possible php script to collect the form data?

if(isset($_GET['submit'])){
    //collect form data
}


$sql="SELECT * FROM epl";
$result=mysqli_query($db_conx,$sql);
if($nr=mysqli_num_rows($result)>0){

These two variables are initialized as with 1 which is used in the while loop to increase the values will serve as the names of the input fields.

$Inum1=1;
$Inum2=1;
while($row=mysqli_fetch_array($result)){
 $t1=$row['team1'];
 $t2=$row['team2'];
 echo '<form METHOD="get">
  <div >
  <div >
  <div >
   <label >'.$t1.'</label>
  </div>
   <input type="text" name="t_'.$Inum1.'_score" id="input">
  </div>

  <label >vs</label>

  <div >
  <input type="text" name="t_'.$Inum2.'_score" id="input">
  <div >
   <label>'.$t2.'</label>
   </div>

  </div> 
  </div> ';
  $Inum1++;
  $Inum2++;
}
echo ' <center><input name="submit" type="submit" value="ENTER NOW!" 
style="width:30%; background-color:#379BFF; text-align:center;border:none; border-radius:3px; height:41px; color:#FFF; 
font-size:24px; box-shadow:none; margin-top:20PX;">
 </form>';
}
302

Answer

Solution:

As there is a form for every record and each form has it's own submit button they are effectively unique and separate entities on the page which means the field names can be repeated ( the ID however cannot )

The form is submitted and there will only be data for that row sent via the form submission - with predefined field namest_1_score andt_2_scoreso it is simple to get the values from the $_GET array and use them in an update/insert statement. If it is an update statement then I think you will need a hidden field per form with that contains the ID for the db record.

/* process form submission */
if( isset( $_GET['t_1_score'],$_GET['t_2_score'] ) ){

    /* 
       As you submit only 1 form at a time the field names
       can be the same in each form
    */
    $score_team_1=$_GET['t_1_score'];
    $score_team_2=$_GET['t_2_score'];

    /* pseudo database code */
    $sql='insert or update some table';
    $res=$db->query( $sql );
}


/* display your form and the input fields for each row */
if( $result ){
    while( $row=mysqli_fetch_array( $result ) ){
        $t1=$row['team1'];
        $t2=$row['team2'];
        /*$id=$row['id'];*/

        echo "
        <form method='get'>
            <div class='block'>
                <div class='epl_form_g'>
                    <div class='eplT'><label>".$t1."</label></div>
                    <input type='text' name='t_1_score'>
                </div>

                <label class='vs'>vs</label>

                <div class='epl_form_g'>
                    <input type='text' name='t_2_score'>
                    <div class='eplT'><label>".$t2."</label></div>
                </div>
                <!--<input type='hidden' name='id' value='{$id}' />-->
                <input name='sub' type='submit' value='ENTER NOW!' style='width:30%; background-color:#379BFF; text-align:center; border:none; border-radius:3px; height:41px; color:#FFF; font-size:24px; box-shadow:none; margin-top:20px auto 0 auto; float:none;' />
            </div>
        </form>";
    }
}

People are also looking for solutions to the problem: php - Doctrine 2 removes parent row

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.