简体   繁体   中英

PHP echo button onclick function

For a little "webshop project" I create a table with PHP and echo"..." function. The table displays some values and in the last cells, there shall be a button which enables the user to delete the corresponding row (or better said, purchase). The data is held in a database and read out while the page loads and than displayed in the table.

I use a "purchase id" to find out which rows have to be deleted, and it works fine if I just implement the function itself. The problem is that I can't get the function working as "onclick" event for the button.

So, some code:

function delete_purchase($purchase_id){
mysql_query("DELETE FROM purchase WHERE purch_id = '$purchase_id'");};

That's the PHP function which deletes the rows, easy enough.

$result = mysql_query("SELECT purchase.purch_id, item.name, purchase.amount, purchase.purch_date, delivery.meaning, item.weight FROM purchase, item, delivery WHERE purchase.cust_id='$cust_id' AND delivery.del_id = purchase.delivered AND purchase.item_id = item.item_id");

while($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['amount'] . "</td>";
    echo "<td>" . $row['weight'] * $row['amount'] . "</td>";
    echo "<td>" . $row['purch_date'] . "</td>";
    echo "<td>" . $row['meaning'] . "</td>";
    echo "<td><button onclick=\"delete_purchase('" . $row['purch_id'] . "')\">Kill</button></td>";
    echo "</tr>";
    }

And this is the part which doesn't seem to work. I get the variable and some other values from the database and insert them into my table as long as there are values. Everything is displayed, even the buttons; but clicking on them doesn't do anything.

Source code of the website seems fine:

<td><button onclick="delete_purchase('138')">Kill</button></td>

Hope everything's clear, and you guys have some ideas what's wrong. If you need to know additional stuff, just ask and I'll see what I can do.

onclick="delete_purchase('138')"

calls a Javascript function called delete_purchase , which doesn't seem to exist in your code. You only have a PHP function with that name.

Since all PHP is executed on the server side, the HTML will be built long before the client ever sees the code and therefore you will never be able to call the delete_purchase PHP function from the client side.

The only two ways to get around this are: - Create a delete_purchase JS function that then calls a PHP file through the use of AJAX. - Don't call the onclick JS function at all and make the button a regular form submit that you then catch on the server side to delete the purchase. This however would involve a complete page refresh.

Your delete_purchase() function is defined in server-side which is not available in client side. You need to send a request to server and send the id, for example:

?action=delete&id=1

Then you can validate it on server side and call the function

<?php
if(isset($_GET['action']) && $_GET['action'] == 'delete'){
 //do some staff
}
?>

you try to call a PHP-function directly from HTML (from browser)

this is impossible!

you may call it using 2 ways:

1) AJAX-call of php-script which will delete the purchase

2) redirect browser to php-script which will delete the purchase and then redirects you back

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM