将js变量传递给php变量 [英] Passing js variable to php variable

查看:82
本文介绍了将js变量传递给php变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将刚刚单击的标签的id值传递给正在使用greybox加载到页面上的php文件.

I want to pass the id value of the tag that has just been clicked to a php file that is being loaded onto the page using greybox.

我将id存储在名为linkID的js变量中,并且可以使用location.href传递正确的ID,但这需要重新加载页面并且不能与greybox一起使用.我可以并且可能如何使用Ajax在后台帮助发送此信息?任何帮助将不胜感激?

I got the id stored in a js variable called linkID and I can get it passing the correct id using location.href but this needs a page reload and doesnt work alongside greybox. Can and maybe how can I use Ajax to help send this info in the background? Any help would be greatly appreciated?

获取点击的ID的Javascript

The Javascript to get the id of the clicked a tag

<script type="text/javascript">
function myCallback(caller)
{
  var linkID =caller.id; 
  location.href="species/butterfly.php?linkID=" + linkID;
  alert(linkID);
  }
</script>

html.index.php中的标记

The html a tag in index.php

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

The butterfly.php page that is trying to recieve the id

<?php
// Retrieve the URL variables (using PHP).
$linkid = $_GET['linkID'];
echo "Number: ".$linkid;
?>

推荐答案

这会将js变量转换为php变量

This will convert js variable to php variable

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

这篇关于将js变量传递给php变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆