Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

javascript - 使用onclick执行PHP函数(Execute PHP function with onclick)

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

(我正在寻找一个简单的解决方案,只有在单击a-tag时调用PHP函数 。)

PHP:

(PHP:)

function removeday() { ... }

HTML:

(HTML:)

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

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

(更新: html和PHP代码在同一个PHP文件中)

  ask by Mike translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).

    (PHP:仅由服务器运行并响应请求,例如单击链接(GET)或提交表单(POST)。)

  • HTML & JavaScript: Is only run in someone's browser (excluding NodeJS).

    (HTML和JavaScript:仅在某人的浏览器中运行(不包括NodeJS)。)

I'm assuming your file looks something like:

(我假设你的文件看起来像:)

<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>

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.

(因为PHP只响应请求(通过$ _REQUEST获取GET,POST,PUT,PATCH和DELETE),所以即使它们在同一个文件中,也必须运行PHP函数。)

This gives you a level of security, "Should I run this script for this user or not?".

(这为您提供了一定程度的安全性,“我是否应该为此用户运行此脚本?”。)

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).

(如果您不想刷新页面,可以通过名为Asynchronous JavaScript and XML(AJAX)的方法向PHP发出请求,而无需刷新。)

That is something you can look up on YouTube though.

(这是你可以在YouTube上查找的内容。)

Just search "jquery ajax"

(只需搜索“jquery ajax”)

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

(我推荐Laravel给任何新人开始吧: http ://laravel.com/)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...