简体   繁体   English

在<a >标记锚标记</a>内调用PHP函数

[英]Calling a PHP function within an <a > tag, anchor tag

Is it possible to call a PHP function within an anchor tag. 是否可以在定位标记内调用PHP函数。 I have a PHP function called logout(); 我有一个名为logout();的PHP函数logout();

Now I want something similar to this. 现在我想要类似的东西。

<a href="logout();" >Logout</a>

I know with Javascript this works but what is the best approach using PHP function? 我知道使用Javascript可以工作,但是使用PHP函数的最佳方法是什么?

No; 没有; PHP is a server side scripting language , so it is inaccessible to the HTML like this. PHP是服务器端脚本语言 ,因此,这样的HTML无法访问它。 JavaScript can do this, as it is a client-side scripting language . JavaScript是可以做到的,因为它是一种客户端脚本语言

Since PHP is a server side language, a message (in the form of an HTTP request) must be sent to the server from the browser (the client) for any PHP to be executed - including your PHP function logout . 由于PHP是一种服务器端语言,因此必须从浏览器(客户端)向服务器发送一条消息(以HTTP请求的形式),以执行任何PHP-包括您的PHP函数logout

You have a few options... 您有几种选择...

Option 1 选项1

Follow the hyperlink to a script which executes the logout() function... 跟随超链接到执行logout()函数的脚本...

HTML 的HTML

<a href="theLogOutScript.php">Logout</a>

PHP 的PHP

<?php
   // ...
   logout();
   // ...
?>

Option 2 选项2

Submit a form to a script which executes the logout() function... 将表单提交给执行logout()函数的脚本...

HTML 的HTML

<form method="POST" action="theLogOutScript.php">
    <input type="submit" value="Logout" />
</form>

Option 3 选项3

Use an XMLHttpRequest /AJAX request to communicate with the server. 使用XMLHttpRequest / AJAX请求与服务器通信。 (no sample code provided) (未提供示例代码)

No, in order to call PHP you will have to make a request back to the server. 不,要调用PHP,您将必须向服务器发出一个请求。 You will either need to link to another PHP page: 您将需要链接到另一个PHP页面:

<a href="/logout" >Logout</a>

Or you will have to make a JavaScript AJAX call to a PHP page ("web service") that will perform the logout. 或者,您将必须对将执行注销的PHP页面(“ Web服务”)进行JavaScript AJAX调用。

<a href ='logout.php'>Logout</a>

logout.php logout.php

<?php
  session_start();
  ...... your code....      
?>

You didn't specify the format of your document. 您未指定文档格式。 If your document format is HTML you'll need to load an external file but if it's .php You can do it by setting your function inside a variable like so: 如果您的文档格式是HTML ,则需要加载一个外部文件,但是如果它是.php,则可以通过在变量中设置函数来实现,例如:

<?php $myLogout  = logout();?>
<a href="<?php echo $myLogout; ?>">Logout</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM