点击HTML链接触发PHP函数 [英] Trigger PHP function by clicking HTML link
问题描述
只需点击一个链接就可以触发一个PHP函数吗?还是应该坚持使用表单提交方法?如果点击链接将起作用,链接应该指向哪里?
以下是一个示例代码:
<?php
session_start();
函数listMe($ username){
$ username = mysql_real_escape_string($ username);
$ query = mysql_query(INSERT INTO List(Usernames)VALUES('$ username'))或die(mysql_error());
}
?>
< html>
< head>
< title> SAMPLE< / title>
< / head>
< body>
<?php
listMe($ _ SESSION ['Username']);
?>
< / body>
< / html>
也许有人可以指出我正确的方向。感谢!
您可以通过使用表单提交加载整个页面或通过加载直接将特定页面内容放入页面中,而不需要逐页进行。第二种方法称为AJAX(Asynchoronous Javascript和XML)。这里有两个例子,其中每一个都有详细说明。
表单提交方式
form.php
<?php
function get_users(){
)
if(isset($ _ GET ['get_users']))
{
get_users();
}
?>
...
< form method =get>
< input type =hiddenname =get_users>
< input type =submit>
< / form>
AJAX方法
ajax_file.php
<?php
function call_me(){
//你的php代码
}
call_me();
?>
form.html
< html>
< head>
< script type =text / javascript>
函数loadXMLDoc()
{
var xmlhttp;
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4&& xmlhttp.status == 200)
{
//在页面加载成功时做些事
}
}
xmlhttp.open(GET,ajax_file.php,true);
xmlhttp.send();
}
< / script>
< / head>
< body>
< a href =#onclick =loadXMLDoc()>点击呼叫功能< / a>
< / body>
< / html>
Is it possible to trigger a PHP function by just clicking a link? Or should I stick with the form submit method? If clicking the link will work, where should the link refer to?
Here is a sample code:
<?php
session_start();
function listMe($username){
$username = mysql_real_escape_string($username);
$query = mysql_query("INSERT INTO List (Usernames) VALUES ('$username')") or die(mysql_error());
}
?>
<html>
<head>
<title>SAMPLE</title>
</head>
<body>
<a href="**__???___**">Add my username to the list</a>
<?php
listMe($_SESSION['Username']);
?>
</body>
</html>
Maybe someone can point me in the right direction. Thanks!
You can do this by means of loading the entire page over again by the use of form submission, or by loading specific page contents directly into the page without needing to go from page to page. The second method is called "AJAX" (Asynchoronous Javascript and XML). Here are two examples, one of each specified.
Form submission approach
form.php
<?php
function get_users(){
}
if(isset($_GET['get_users']))
{
get_users();
}
?>
...
<form method="get">
<input type="hidden" name="get_users">
<input type="submit">
</form>
AJAX approach
ajax_file.php
<?php
function call_me(){
// your php code
}
call_me();
?>
form.html
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
// do something if the page loaded successfully
}
}
xmlhttp.open("GET","ajax_file.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<a href="#" onclick="loadXMLDoc()">click to call function</a>
</body>
</html>
这篇关于点击HTML链接触发PHP函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!