简体   繁体   English

PHP echo按钮onclick功能

[英]PHP echo button onclick function

For a little "webshop project" I create a table with PHP and echo"..." function. 对于一个小“网上商店项目”,我创建一个PHP和echo“...”函数的表。 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. 我使用“购买ID”来找出哪些行必须删除,如果我只是实现函数本身就可以正常工作。 The problem is that I can't get the function working as "onclick" event for the button. 问题是我无法将该功能作为按钮的“onclick”事件。

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. 这是删除行的PHP函数,很容易。

$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. 调用一个名为delete_purchase的Javascript函数,它在您的代码中似乎不存在。 You only have a PHP function with that name. 您只有一个具有该名称的PHP函数。

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. 由于所有PHP都在服务器端执行,因此HTML将在客户端看到代码之前很久就构建,因此您永远无法从客户端调用delete_purchase PHP函数。

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. 解决这个问题的唯一两种方法是: - 创建一个delete_purchase JS函数,然后通过使用AJAX调用PHP文件。 - 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. - 根本不要调用onclick JS函数,并使按钮成为常规表单提交,然后在服务器端捕获以删除购买。 This however would involve a complete page refresh. 然而,这将涉及完整的页面刷新。

Your delete_purchase() function is defined in server-side which is not available in client side. 您的delete_purchase()函数在服务器端定义,在客户端不可用。 You need to send a request to server and send the id, for example: 您需要向服务器发送请求并发送ID,例如:

?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) 你试图直接从HTML(从浏览器)调用PHP函数

this is impossible! 这是不可能的!

you may call it using 2 ways: 你可以用两种方式来称呼它:

1) AJAX-call of php-script which will delete the purchase 1)AJAX调用php脚本将删除购买

2) redirect browser to php-script which will delete the purchase and then redirects you back 2)将浏览器重定向到php-script,这将删除购买,然后重定向回来

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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