php PHP回显按钮onclick函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15751685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:53:19  来源:igfitidea点击:

PHP echo button onclick function

phpbuttononclickhtml-tableecho

提问by Phil

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.

对于一个小小的“网上商店项目”,我创建了一个带有 PHP 和 echo“...” 函数的表格。表格显示一些值,在最后一个单元格中,应该有一个按钮,使用户能够删除相应的行(或者更好地说,购买)。数据保存在数据库中,并在页面加载时读出,然后显示在表中。

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.

我使用“购买 ID”来找出哪些行必须被删除,如果我只是实现函数本身,它就可以正常工作。问题是我无法将该功能作为按钮的“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.

希望一切都清楚,你们有一些想法是什么问题。如果您需要了解其他内容,请询问,我会看看我能做些什么。

回答by Steve

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.

调用一个名为 的 Javascript 函数delete_purchase,它似乎不存在于您的代码中。您只有一个具有该名称的 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_purchasePHP function from the client side.

由于所有 PHP 都是在服务器端执行的,HTML 将在客户端看到代码之前很久就被构建,因此您将永远无法delete_purchase从客户端调用PHP 函数。

The only two ways to get around this are: - Create a delete_purchaseJS function that then calls a PHP file through the use of AJAX. - Don't call the onclickJS 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.

解决这个问题的仅有两种方法是: - 创建一个delete_purchaseJS 函数,然后通过使用 AJAX 调用 PHP 文件。- 根本不要调用onclickJS 函数,并使按钮成为常规表单提交,然后在服务器端捕获以删除购买。然而,这将涉及完整的页面刷新。

回答by Phil

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:

您的delete_purchase()函数是在服务器端定义的,在客户端不可用。您需要向服务器发送请求并发送 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
}
?>

回答by dimaninc

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) php-script 的 AJAX 调用将删除购买

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

2) 将浏览器重定向到 php-script,这将删除购买,然后将您重定向回来