0

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 class="block">
    <div class="epl_form_g">
    <div class="eplT">
      <label >'.$t1.'</label>
    </div>
      <input type="text" name="t_'.$Inum1.'_score" id="input">
    </div>

    <label class="vs">vs</label>

    <div class="epl_form_g">
    <input type="text" name="t_'.$Inum2.'_score" id="input">
    <div class="eplT">
      <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>';
}
Misa Lazovic
  • 2,805
  • 10
  • 32
  • 38
  • Why do you want to use a `while()` loop to save variables? – Derek Pollard Feb 02 '16 at 15:43
  • a new form for every iteration seems unnecessary and you are reusing the ID ~ twice per form – Professor Abronsius Feb 02 '16 at 15:44
  • hi you can take look of this question [link](http://stackoverflow.com/questions/3314567/how-to-get-form-input-array-into-php-array) – Shailesh Singh Feb 02 '16 at 15:44
  • However, if you have a form per db row then you can probably use standard names without needing numbers ~ ie simply `name='t_1_score'` and `name='t_2_score'` because you only submit one form at a time so whatever processes the form submission need only look for those two variables in the $_GET array – Professor Abronsius Feb 02 '16 at 15:51
  • Ok so how do I collect the values and post to DB when the form is set @RamRaider – Joel Amawhe Feb 02 '16 at 15:53

1 Answers1

0

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 names t_1_score and t_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>";
    }
}
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46