Saptarshi
Saptarshi

Reputation: 62

Get value of a table data on button click

I am building a online exam application, here paper name and no. of papers are retrieved from database. Now I want to get the paper code of that paper for which I clicked the start button. Code for the table is here:

          <form method="post" action="exam_page.php" >
           <table >
                <tr style="background-color: #7F859E;color:white; height:50px;">
                     <th style="padding-left:140px; width:550px;">Paper</th> 
                     <th style="padding-left:40px;">Time</th>
                     <th style="padding-left:40px;">Duration</th>
                     <th style="padding-left:40px; width:250px;"></th>
                      
                </tr>
                <?php
                     $i=1;
                     while($row=mysql_fetch_array($rs)){?>
                     <tr style="height:80px; background-color: #CCCCCC;">
                          <td style="padding-left:40px;"> 
                               <input type="text" value="<?=$row['paper_code']?>" name="paper_code<?=$i?>" readonly><?=$row['paper_name']?>
                          </td>
                          <td style="padding-left:40px;">
                               <input type="text" value="<?=$row['time']?>" readonly style="width:90px;">
                          </td>
                          <td style="padding-left:40px;">
                               <input type="text" value="<?=$row['duration']?> Min" readonly style="width:90px;">
                          </td>
                          <td style="padding-left:40px;"><button style="width:100px;">Start</button></td>
                     </tr>
                <?php $i++; } $_SESSION['exam']=$i; ?>
            </table>
          </form>

Upvotes: 1

Views: 4149

Answers (3)

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

Name your submit button, (also make it a submit type) and assign the paper code to its value attribute.

<button type="submit" style="width:100px;" name="clicked" value="<?=$row['paper_code']?>">
    Start
</button>

Now, in exam_page.php you can get the value of the clicked button from $_POST['clicked']. (Or whatever you decide to name it.)


To get the values from the other inputs associated with the button you clicked, you can add the paper code to their names instead of using $i.

<input type="text" value="<?=$row['time']?>" name="time[<?=$row['paper_code']?>]">

and in exam_page.php you can get the value from $_POST['time'][$_POST['clicked']], etc.

If they aren't intended to be editable in your form, though, I would recommend using something else to display them and just loading them from the database in exam_page.php instead. Otherwise, your users will be able to override the readonly attribute and submit different values.

Upvotes: 1

Team Work
Team Work

Reputation: 35

passed your unique id value or examcode via hidden field like this

<input type="hidden" name="id" value="<?=$row['eid']?>">

and on button click perform query like

$id=$_POST['id'];
select * from table_name where id='$id';

Upvotes: 0

Eyy
Eyy

Reputation: 135

Try using javascript onclick functions:

<script>
   function getPaperCode(paperCode)
   {
      alert(paperCode);
   }

</script>

Then edit your input add onclick event:

<input type="text" value="<?php echo $row['paper_code']; ?>" onclick="getPaperCode('<?php echo $row["paper_code"]; ?>');" name="paper_code<?php echo $i; ?>" readonly><?=$row['paper_name']?>

Once you click. it will alert the value of the button

Upvotes: 0

Related Questions