我希望将刚刚单击的标记的id值传递给一个php文件,该文件使用greybox加载到页面上。
我将id存储在一个名为linkID的js变量中,我可以使用location.href传递正确的id来获取它,但这需要重新加载页面,并且不能与greybox一起使用。我可以使用Ajax在后台发送这些信息吗?如果有任何帮助,我们将不胜感激。
用于获取所单击标记的id的Javascript
<script type="text/javascript">
function myCallback(caller)
{
var linkID =caller.id;
location.href="species/butterfly.php?linkID=" + linkID;
alert(linkID);
}
</script>index.php中的html a标记
<div id="stump">
<a href="#" id="2" class="hedgehog descript" title="Hedgehog"
rel="gb_page_center[1020, 550]" onclick="myCallback(this)"></a>
</div><!--close stump div -->尝试接收id的butterfly.php页面
<?php
// Retrieve the URL variables (using PHP).
$linkid = $_GET['linkID'];
echo "Number: ".$linkid;
?>发布于 2011-04-28 04:36:36
var myCallback = function(caller){
var linkID = caller.id;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST","species/butterfly.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("linkID="+linkID;);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
alert("Request complete. Data: "+xmlhttp.responseText);
}
};
alert(linkID);
}编辑:
纯GreyBox:
var myCallback = function(caller){
var linkID = caller.id;
caller.href = "species/butterfly.php?linkID="+linkID;
}发布于 2013-03-16 01:54:23
这将把js变量转换成php变量
<script>
function sud(){
javavar=document.getElementById("text").value;
document.getElementById("rslt").innerHTML="<?php
$phpvar='"+javavar+"';
echo $phpvar.$phpvar;?>";
}
function sud2(){
document.getElementById("rslt2").innerHTML="<?php
echo $phpvar;?>";
}
</script>
<body>
<div id="rslt">
</div>
<div id="rslt2">
</div>
<input type="text" id="text" />
<button onClick="sud()" >Convert</button>
<button onClick="sud2()">Once Again</button>
</body>https://stackoverflow.com/questions/5810204
复制相似问题