如何在 html 按钮单击上调用 php 脚本/函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27716499/
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
How to call a php script/function on a html button click
提问by dspacejs
Before someone has a go at me or marks this down, I have looked all over the internet to find out how to do this (including the same question on stackoverflow). I'm new, and I am finding it very hard to learn new concepts so please be easy on me.
在有人对我进行攻击或将其标记下来之前,我已经浏览了整个互联网以了解如何执行此操作(包括有关 stackoverflow 的相同问题)。我是新手,我发现学习新概念非常困难,所以请对我放轻松。
What I want to do is call a php script/function on a button click. I have this running in WAMP if that helps. Here's my code:
我想要做的是在单击按钮时调用 php 脚本/函数。如果有帮助,我会在 WAMP 中运行它。这是我的代码:
<?php include 'the_script.php'; ?>
<button type="button" onclick="the_function()">Click Me</button>
the_script.php has this in it:
the_script.php 中有这个:
the_function() {
echo "You win";
}
Why isn't this working? I've heard about the button being client side etc. and PHP being server-side, which means that you cannot link the two together. I know that you have to use AJAX to make this work, however I legitimately have absolutely no clue how to do it. I've tried googling it etc., however I can't find anything. I know how to use AJAX and call events with it, however I still have no idea how to make it call a PHP script.
为什么这不起作用?我听说按钮是客户端等,而 PHP 是服务器端,这意味着您不能将两者链接在一起。我知道你必须使用 AJAX 来完成这项工作,但是我完全不知道如何去做。我试过谷歌搜索等,但我找不到任何东西。我知道如何使用 AJAX 并用它调用事件,但是我仍然不知道如何让它调用 PHP 脚本。
Can you please make your answers as clear and simple as possible, I'm new to this
你能不能让你的答案尽可能简单明了,我是新手
Thanks for the help :)
谢谢您的帮助 :)
EDIT ***
编辑 ***
For some reason wherever I go everyone's code is different. The way I have been taught AJAX looks completely different. Can you please write it this way so I can understand? Thanks, here's an example:
出于某种原因,无论我走到哪里,每个人的代码都不同。我学习 AJAX 的方式看起来完全不同。你能不能这样写,让我明白?谢谢,这是一个例子:
var request;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.open('GET', 'file.php', true);
request.onreadystatechange = function() {
if (request.readyState===4 && request.status===200) {
do stuff
}
}
request.send();
回答by Priyank
Just try this:
试试这个:
<button type="button">Click Me</button>
<p></p>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type: 'POST',
url: 'script.php',
success: function(data) {
alert(data);
$("p").text(data);
}
});
});
});
</script>
In script.php
在脚本.php
<?php
echo "You win";
?>
回答by akhil.cs
Of course AJAX is the solution,
AJAX当然是解决方案,
To perform an AJAX request (for easiness we can use jQuery library).
执行 AJAX 请求(为方便起见,我们可以使用 jQuery 库)。
Step1.
第1步。
Include jQuery library in your web page
在您的网页中包含 jQuery 库
a. you can download jQuery library from jquery.com and keep it locally.
一种。您可以从 jquery.com 下载 jQuery 库并将其保存在本地。
b. or simply paste the following code,
湾 或者简单地粘贴以下代码,
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
Step 2.
第2步。
Call a javascript function on button click
单击按钮时调用 javascript 函数
<button type="button" onclick="foo()">Click Me</button>
Step 3.
第 3 步。
and finally the function
最后是函数
function foo () {
$.ajax({
url:"test.php", //the page containing php script
type: "POST", //request type
success:function(result){
alert(result);
}
});
}
it will make an AJAX request to test.php when ever you clicks the button and alert the response.
当您单击按钮并提醒响应时,它会向 test.php 发出 AJAX 请求。
For example your code in test.php is,
例如你在 test.php 中的代码是,
<?php echo 'hello'; ?>
then it will alert "hello" when ever you clicks the button.
然后它会在你点击按钮时提醒“你好”。
回答by Kumar
You can also use
你也可以使用
$(document).ready(function() {
//some even that will run ajax request - for example click on a button
var uname = $('#username').val();
$.ajax({
type: 'POST',
url: 'func.php', //this should be url to your PHP file
dataType: 'html',
data: {func: 'toptable', user_id: uname},
beforeSend: function() {
$('#right').html('checking');
},
complete: function() {},
success: function(html) {
$('#right').html(html);
}
});
});
And your func.php:
function toptable()
{
echo 'something happens in here';
}
Hope it helps somebody
希望它可以帮助某人
回答by Sougata Bose
Use jQuery
.In the HTML page -
使用jQuery
. 在 HTML 页面中 -
<button type="button">Click Me</button>
<script>
$(document).ready(function() {
$("button").click(function(){
$.ajax({
url:"php_page.php", //the page containing php script
type: "POST", //request type
success:function(result){
alert(result);
}
});
});
})
</script>
Php page -
Php页面-
echo "Hello";
回答by Mansoorkhan Cherupuzha
Modify the_script.phplike this.
像这样修改the_script.php。
<script>
the_function() {
alert("You win");
}
</script>
回答by Priyank
First understand that you have three languages working together.
PHP: Is only run by the server and responds to requests like clicking on a link (GET) or submitting a form (POST). HTML & Javascript: Is only run in someone's browser (excluding NodeJS) I'm assuming your file looks something like:
PHP:仅由服务器运行并响应诸如单击链接 (GET) 或提交表单 (POST) 之类的请求。HTML & Javascript:仅在某人的浏览器中运行(不包括 NodeJS)我假设您的文件如下所示:
<?php
function the_function() {
echo 'I just ran a php function';
}
if (isset($_GET['hello'])) {
the_function();
}
?>
<html>
<a href='the_script.php?hello=true'>Run PHP Function</a>
</html>
Because PHP only responds to requests (GET, POST, PUT, PATCH, and DELETE via $_REQUEST) this is how you have to run a php function even though their in the same file. This gives you a level of security, "Should I run this script for this user or not?".
因为 PHP 只响应请求(通过 $_REQUEST 获取、POST、PUT、PATCH 和 DELETE),这就是您必须运行 php 函数的方式,即使它们在同一个文件中。这为您提供了一定程度的安全性,“我是否应该为此用户运行此脚本?”。
If you don't want to refresh the page you can make a request to PHP without refreshing via a method called Asynchronous Javascript and XML (AJAX).
如果您不想刷新页面,则可以通过称为异步 Javascript 和 XML (AJAX) 的方法向 PHP 发出请求而无需刷新。
回答by Kiren Siva
the_function() {
$.ajax({url:"demo_test.php",success:function(result){
alert(result); // will alert 1
}});
}
// demo_test.php
// demo_test.php
<?php echo 1; ?>
Notes
笔记
- Include jquery library for using the jquery Ajax
- Keep the demo_test.php file in the same folder where your javascript file resides
- 包含 jquery 库以使用 jquery Ajax
- 将 demo_test.php 文件保存在 javascript 文件所在的同一文件夹中