Loyalist1
Loyalist1

Reputation: 120

PHP POST value from a looped table

Please help me out of this.....

I am designing a table which is inside a form. the table is generated based on while loop. In each row there is a download button. when i click download the POST value should get the same row information. But my POST variable is giving me the last row information only. I tried using input-type as hidden... But it did not work Here is the code for your reference

enter code here

<form name="simpleform" method="post" action="insert.php">
<?php
$data = "environment";
$user_name = $_SESSION['username'];
$serch = mysql_query("SELECT * FROM data WHERE (data_category = '" . $data . "')    ");                 
while ($record=mysql_fetch_assoc($serch))
{?>             
<tr class="warning">
<td >
<input type="text" value=<?php echo $record['data_ID'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_ID'];?> name="dataid" />
</td>
<td >
<input type="text" value=<?php echo $record['data_name'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_name'];?> name="dataname" />
</td>
<td >
<input type="text" value=<?php echo $record['data_downloads'];?> readonly="readonly">
<input type="hidden" value=<?php echo $record['data_downloads'];?> name="datadown" />
</td>
<td >
<input type="text" value="" >
<input type="hidden" value="" name="datause" />
</td>
<td>
<input type="submit" name="simplesubmit" value="Go to download" />
</td>
</tr>           
<?php }
exit;
?>
</tbody>
</form>

Upvotes: 1

Views: 1554

Answers (3)

Loyalist1
Loyalist1

Reputation: 120

Thanks for info... and good response. As you said , i replaced the same and saw the post value is giving me all arguments as array. My purpose is to let the client download file that he clicks. so if the client click the first row button in the table, the post value should get only that Data_name. So that i can run a query to get the URL of that data_name and download

Upvotes: 0

Nick Andriopoulos
Nick Andriopoulos

Reputation: 10643

The problem is that you are using the same name attribute for all your controls. Thus, when PHP receives the form, they get overwritten, and you only see the last value of the form.

The simplest way to avoid that is just appending [] to the end of your names -- eg name=dataid[]. This will make PHP take all arguments as an array, so you don't lose data.

The second problem, is that your submit button also has the same name - you should diversify it by using some row-specific data in its name, such as 'name="submit-'.$record['data_name'].'"'

For more info, more code from you is needed, such as what are the data you are printing like.

Upvotes: 1

Omer
Omer

Reputation: 164

Every post button can have its name and value, so if you change your code to produce a traceable post button name you can just do whatever you want with that.

<table>
   <tr>
      <td>...</td>
      <td>...</td>
      <td><input type="submit" name="submit[1]" value="OK" />
   </tr>
   <tr>
      <td>...</td>
      <td>...</td>
      <td><input type="submit" name="submit[2]" value="OK" />
   </tr>
</table>

When the form is posted its very easy to capture which button is clicked;

if ($_POST["submit"]) {
   $id = key($_POST["submit"]);
}

Upvotes: 0

Related Questions