I have a table here fetched from SQL with
I have printed a button along with each row of SQL via PHP
output:
--------------------------------------------------------------
|----date-------| -- button(email)-- | Sno. |
------------------------------------------------------------
| 25/12/2014 | -- [some button] -- | 1. |
-----------------------------------------------------------
| 29/11/2014 | -- [some button] -- | 2. |
--------------------------------------------------------------
| 15/10/2014 | -- [some button] -- |.... and so on. |
--------------------------------------------------------------
So, here is my code for the above table:
$result = mysqli_query($con,"select date_format(cmpstartdatetime, '%d/%m/%Y') as
campdate,cmpname from Campaigns group by campdate order by campdate DESC");
//echo $result;
$datesappend = "";
if(mysqli_num_rows($result)>0){
echo "<br/><table id='example1' class='table table-bordered table-striped display'>";
echo "<thead>
<tr>
<th># </th>
<th>Campaign Date</th>
<th>Email Report</th>
</tr>
</thead>
<tbody>";
//echo "<th><td>Total Mesages</td><td>Delivered</td></th>";
$i=1;
while($row = mysqli_fetch_array($result)) {
$datesappend.=$row;
echo "<tr>";
echo "<td>".$row['campdate']."</td>";
echo "<td> <input type='button' text='venkatesh' value='Email' name='testmesssage'
id='testmesssage' class='btn btn-primary'/></td>";
echo "<td>".$i."</td>";
$i++;
echo "</tr>";
}
echo "</tbody></table>";
}
else{
echo "No campaign was done so far.";
}
}
What I need is every row has a button it has to get the content of that row if a button is pressed need to get date assigned to a variable.
actually the whole functionality is send mails by buttons. That dates related reports have to sent via mails an I have the reports.
I need to fetch data of the same row when the respective button is clicked.
try doing somthing like this when you are add the results
echo "<td id='t".$i."'>".$row['campdate']."</td>";
echo "<td> <input type='button' text='venkatesh' value='Email' name='testmesssage' id='testmesssage' class='btn btn-primary' onclick='findresult(".'"t'.$i.'"'.")' /></td>";
echo "<td>".$i."</td>";
you could even use the onclick event on the table row so that you don't have to click a button just the row.
as for the javascript try
<script>
function findresult(id) {
alert(document.getElementById(id).innerHTML);
}
</script>
You can bring back the Id field from the tables in your select statement, and then change the button HTML like this. (assuming sno is a integer)
echo "<td><input type='button' text='venkatesh' value='Email' name='testmesssage' id='testmesssage' class='btn btn-primary' onclick='getDetail(".$row['Sno'].")'"/></td>";
you can then add a client side function to your HTML which will be called when the button is clicked. sno will contain the id of the row that was clicked on. You can then use this id in further calls to retrieve the data you want.
<script>
function getDetail(sno){
alert(sno);
}
</script>
Is that what you wanted?