运行PHP code,当用户点击链接并传递变量 [英] Run PHP code when user clicks link and pass variables
问题描述
我需要从外部服务器时,用户点击一个链接运行PHP code。链接无法直接导致PHP文件,所以我想我需要使用AJAX / jQuery来运行PHP?但是,我怎么能做到这一点,我怎么能传递一个变量的链接?
I need to run a PHP code from external server when user clicks a link. Link can't lead directly to PHP file so I guess I need to use AJAX/jQuery to run the PHP? But how can I do it and how can I pass a variable to the link?
这样的事情?
< A HREF =运行code.html ID ='+ ID +'?>
然后运行code.html会有一个AJAX / jQuery的code,它会发送变量到PHP?
<a href="runcode.html?id=' + ID + '">
and then runcode.html will have an AJAX/jQuery code that will send that variable to PHP?
推荐答案
使用这样的事情在你页面的链接
use something like this in you page with link
某些文本
在上面放这个地方在同一个页面
in the same page put this somewhere on top
<script language='javascript'>
$(function(){
$('.myClass').click(function(){
var data1 = 'someString';
var data2 = 5;//some integer
var data3 = "<?php echo $somephpVariable?>";
$.ajax({
url : "phpfile.php (where you want to pass datas or run some php code)",
data: "d1="+data1+"&d2="+data2+"&d3="+data3,
type : "post",//can be get or post
success: function(){
alert('success');//do something
}
});
return false;
});
});
</script>
在URL中的网址:阿贾克斯提交 你可以通过获取这些DATAS 对于examlple
on the url mentioned in url: in ajax submission you can fetch those datas passed for examlple
<?php
$data1 =$_POST['d1'];
$data2 =$_POST['d2'];
$data3 =$_POST['d3'];
//now you can perform actions as you wish
?>
希望帮助
这篇关于运行PHP code,当用户点击链接并传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!