使用php创建一个调用php函数的按钮 [英] Using php to create a button that calls a php function
问题描述
目前我有以下但它不太喜欢我:
1. index.php:
Currently I have the following but it doesn't like me very much:
1. index.php:
<!DOCTYPE html>
<html>
<body>
<div id = "bookList">
<?php
include("list.php");
?>
</div>
</body>
</html>
2。 list.php:
2. list.php:
<?php
echo '<button id = "read">Read</button><br><br>';
echo
"<script type=\"text/javascript\">
$(\"#read\").click(function()
{
alert(\"<?php display(); ?>\");
});
</script>";
function display()
{
echo "hello";
}
?>
正如我上面发布的代码所希望的那样,我尝试使用php创建一个按钮当点击时会依次调用一个php函数。截至目前我还没有成功。任何建议,将不胜感激,但我希望我的代码保持尽可能接近我目前拥有的。
As is hopefully obvious from the code I posted above, I am attempting to create a button using php which when clicked on will in turn call a php function. I have not been successful as of yet. Any advice will be appreciated but I'd like my code to stay as close to what I currently have as possible.
基本的误解在这里。 PHP和Javascript并不像那样交互
Some basic misunderstandings here. PHP and Javascript does not interact like that
让我给出一个相当愚蠢的比喻:
$ b
Let me give a rather silly analogy:
- 你去咖啡店点点咖啡。
- 店主给你咖啡。你喝了一口,发现你需要更多的糖
- 你告诉店主将糖加入咖啡中。让我们称之为
addSugar()
。
- You go to the coffee shop and order some coffee.
- The shop keeper gives you the coffee. You take a sip and find you need more sugar
- You tell the shopkeeper who adds the sugar to your coffee. Lets call this action
addSugar()
.
这里一切都很酷。现在在第二种情况下会发生什么:
Everything is cool here. Now what happens in this second scenario:
- 您前往咖啡店点咖啡。
<李>店主给你咖啡。你带着咖啡走回家 - 你喝了一口,发现你需要更多的糖
- 你告诉店主
addSugar()
...? erm ...
- You go to the coffee shop and order some coffee.
- The shop keeper gives you the coffee. You take the coffee with you and walk home
- You take a sip and find you need more sugar
- You tell the shopkeeper to
addSugar()
...? erm...
那么店主不在那里。同样,当一个页面到达你的浏览器时,它就离开了咖啡店。没有PHP / shopkeeper了
Well the shopkeeper aint there. In the same way, when a page reaches your browser, it has left the coffee shop. There is no PHP/shopkeeper around anymore
您尝试点击 addSugar()
Javascript不起作用。
Your request to addSugar()
that you are trying on button click using Javascript will not work.
您需要做的是使用名为 AJAX
的东西,这是一种快速跑到coffeshop只是为了增加一点糖。它是一个更广泛的话题,你将需要阅读它,但有大量的资源在那里..
What you will need to do is use something called AJAX
which is a way to quickly run to the coffeshop just for adding a little sugar.. Its a broader topic and you will need to read about it, but there are tons of resources out there..
这篇关于使用php创建一个调用php函数的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!