I have a HTML table with a column that contains a button at the end of each row:
echo "<td><button type='button' class='btn btn-primary'>View Order </button></td>";
How can I make this button link to viewOrder.php
and also pass the order number from the first column on the same row for e.g. viewOrder.php?id=<data from column 1 here>
.
echo "<td><button type='button' class='btn btn-primary' onClick='window.location.href='viewOrder.php?id='+orderNumber'>View Order </button></td>";
Unable to provide more, as need more code.
If your order number is some div
or span
, etc that have id="orderNumer"
, use document.getElementById("orderNumber").innerHTML
.
Assuming you have a PHP variable $orderNumber:
echo "<td><button type='button' class='btn btn-primary' onClick='doThing({$orderNumber});'>View Order </button></td>";
Now you need to define the method doThing in javascript:
function doThing(ordernum)
{
document.location.href = "viewOrder.php?id=" + ordernum;
}
Since your button is taking you to a new page, and you are asking for a get request, I think you should use a link. Optionally you could style the link to make it look like a button.
This way you avoid the use of java-script to do the get Request by appending the orderNumber
in the URL.
Since it seems you are using BootStrap, you can use the btn btn-primary
classes to make the link look like a button
Try this:
echo "<td><a href="viewOrder.php?id=$ordernumber" class="btn btn-primary">View Order</a></td>";