使用onclick执行PHP函数 [英] Execute PHP function with onclick

查看:84
本文介绍了使用onclick执行PHP函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种仅单击 a-tag 即可调用 PHP函数的简单解决方案.

I am searching for a simple solution to call a PHP function only when a-tag is clicked.

PHP:

function removeday() { ... }

HTML:

<a href="" onclick="removeday()" class="deletebtn">Delete</a>

更新:html和PHP代码位于同一PHP文件中

UPDATE: the html and PHP code are in the same PHP file

推荐答案

首先,请了解您有三种语言可以共同工作:

First, understand that you have three languages working together:

  • PHP:它仅由服务器运行,并响应诸如单击链接(GET)或提交表单(POST)之类的请求.

  • PHP: It only runs by the server and responds to requests like clicking on a link (GET) or submitting a form (POST).

HTML& JavaScript:它只能在某人的浏览器(不包括NodeJS)中运行.

HTML & JavaScript: It only runs in someone's browser (excluding NodeJS).

我假设您的文件如下所示:

I'm assuming your file looks something like:

<!DOCTYPE HTML>
<html>
<?php
  function runMyFunction() {
    echo 'I just ran a php function';
  }

  if (isset($_GET['hello'])) {
    runMyFunction();
  }
?>

Hello there!
<a href='index.php?hello=true'>Run PHP Function</a>
</html>

因为PHP仅响应请求(通过$ _REQUEST进行的GET,POST,PUT,PATCH和DELETE),所以即使它们位于同一文件中,这也是您必须运行PHP函数的方式.这为您提供了一定程度的安全性,我是否应该为此用户运行此脚本?".

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 they're in the same file. This gives you a level of security, "Should I run this script for this user or not?".

如果您不想刷新页面,则可以通过一种称为异步JavaScript和XML(AJAX)的方法来向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).

这是您可以在YouTube上查找的内容.只需搜索"jquery ajax"

That is something you can look up on YouTube though. Just search "jquery ajax"

我向所有新手推荐Laravel,以使其开始: http://laravel.com/

I recommend Laravel to anyone new to start off right: http://laravel.com/

这篇关于使用onclick执行PHP函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆