SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2008
    0 Post(s)
    0 Thread(s)

    Form fields and variable length tables


    If this has already been covered, I would appreciate links to the threads. I looked but it might be listed differently...

    I am populating a table with fields from a MySQL table...

    PHP Code:
    <form action=update_comm.php method=post>
    <table align=center bordercolor=black cellpadding=10 cellspacing=0 
    border=1 borderwidth=0>
    <th>Sales Person</th>
    <th>End Date</th>
    <th>End Imm</th>

    $query="SELECT * FROM SalesStaff WHERE SKey = '$skey'";

    $name = mysql_result($result, $x, 'Name');

    $query="SELECT * FROM SalesStaffDetails WHERE SalesStaffKey = '$skey'
     and Active = 'Y'";


    while($i < $num) {

    $query1="SELECT * FROM gen_Client WHERE ClientKey='$clientkey'";

    $client=mysql_result($result1, $m, "ClientName");

    <td><? echo $name ?></td>
    <td> <? echo $client ?></td>

    <td align=center><input size=5 type=text name=comm_percent 
    value="<? echo $comm_percent ?>" >%</td>
    <? echo $end_calendar->CreateCalendar(date('d/m/Y'),'end_date',date('d/mY'),1); ?> 
    <td align=center><input type="checkbox" name="endimmed" 


    } // end of while
    <td colspan=9 align=center><input type=submit value="Submit"></td>
    As the fields are filled in the while loop, they will all have the same variable name.

    I need to be able to pass all the possible fields but at the moment the receiving script only gets the last field - I'm sure this is caused by the variable names.

    How do I send multiple fields for date and check boxes from the table to the receiving script?

    (I'm not great with arrays)


  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Philadephia, PA
    1 Post(s)
    0 Thread(s)
    Quote Originally Posted by Egad
    (I'm not great with arrays)
    Here's your chance to use some and improve that

    Having a bunch of inputs in the form with the same name won't work, so you need to use arrays.

    Use the primary key of the row as the index of the arrays for that row, and you'll be able to match them up when you receive the form and do whatever you do with it.

    PHP Code:
    <input size="5" type="text" name="comm_percent[<?php echo mysql_result($result$i"Your PK Column"); ?>]" value="<?php echo $comm_percent?>" />
    Your code would be a lot cleaner if you fetched each row as an array instead of calling mysql_result everywhere:

    PHP Code:
    while ($row = mysql_fetch_array($result)) {

      //all the stuff inside your current loop
      //using $row['column'] instead of calling mysql_result to access values


      <input size="5" type="text" name="comm_percent[<?php echo $row['pk']; ?>]" value="<?php echo $row['Percent']; ?>" />




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts