pironix
pironix

Reputation: 13

How to submit an HTML form into a PHP array

I've looked through some posts around here, but am still struggling to get my code to work correctly....

I've got a form that is being dynamically created so I never know how many rows it will have, the number of columns is static though. Here is my current code...

echo '<form name = "confirmPlayers" enctype="multipart/form-data" action="page.php" method="POST">';
    echo '<input type="hidden" name="confirm" value="1"/>';
    echo '<input type="hidden" name="pg_function" value="match"/>';
    echo '<table border = "1">';
    echo '<tr> <td colspan = "7"> <b>Matched Members</b></td> </tr>';
    echo '<tr> <td> Match </td> <td>Division</td> <td>Number</td> <td>Upload Number</td> <td>KDGA Name</td>  <td>Upload Name</td> <td>Score</td></tr>';

    while($row = mysql_fetch_array($result)){
        echo "<tr>";
        echo '<td> <input type="checkbox" name="match_chkBox[]" value="1" checked>';
        echo '<td> ' . $row['division'] . ' </td>';
        echo '<td> ' . $row['mplayer_number'] . ' </td>';
        echo '<td> ' . $row['tplayer_number'] . ' </td>';   
        echo '<td> ' . $row['mfirst_name'] . ' ' . $row['mlast_name'] . ' </td>';
        echo '<td> ' . $row['tfirst_name'] . ' ' . $row['tlast_name'] . ' </td>';
        echo '<td> ' . $row['score'] . ' </td>';
        echo "</tr>";
    }
    echo '<tr> <td>';
    echo '<input type="submit" value="Submit" />';
    echo '</td> </tr>';
    echo '</table> </form>';

I've tried several variations on creating the array but just seem to be creating a mess. I basically need all of the information from each cell returned in order to pass on to the database for further processing. I was hoping to return an array with a row for each corresponding row in the generated table, and then keys/indexes for each column displayed. Right now if I use a print_r($_POST['match_chkBox']); then it is returning correctly with the number of rows I left checked before submitting, but any other changes.. not so pretty.

Would appreciate any help you can give... this is driving me nuts!

Upvotes: 0

Views: 168

Answers (3)

Naveen Kumar
Naveen Kumar

Reputation: 4591

Set the value of the checkbox to the unique key of that row, there by when the form is posted, match_chkBox[] will contain the unique keys.

   echo"<input type='checkbox' name='match_chkBox[]' value=\"$row[unique_id]\" />";

Use view source to see if each rows value is set to the unique_id of that row

Upvotes: 0

zzlalani
zzlalani

Reputation: 24344

if i were you i will post $row[id] (as defined by angel) only and fetch respective data at my php code from the database.

but if you want to take all data with you you can simply do the following..

<pre>
 $i=0;
 while($row = mysql_fetch_array($result)){
    $i++;
    echo "<tr>";
    echo '<td> <input type="checkbox" name="match_chkBox[$i]" value="'.$row['id'].'" checked>';
    echo '<td> ' . $row['division'] . ' <input type="hidden" name="division['.$i.']"> </td>';
    echo '<td> ' . $row['mplayer_number'] . ' <input type="hidden" name="mplayer_number['.$i.']"> </td>';
// and so on....
    echo "</tr>";
 }
</pre>

in since html form sent only checked checkbox to the server you can have the following code to check user selections

<pre>
   foreach($_POST['match_chkBox'] as $k=>$value) {
      $divi  = division[$k];
      $mplay = mplayer_number[$k];
      //now insert into your database.
   }
</pre> 

hope this will help

Upvotes: 0

Ajeet  Sinha
Ajeet Sinha

Reputation: 2345

try giving value unique id

echo '<td> <input type="checkbox" name="match_chkBox[]" value=value=\"".$row['id']."\"checked>';

Upvotes: 1

Related Questions