2017-01-30 157 views
0

我想从我所在的对象中检索我的id和类型。php中的变量会话

然后,在一类我做的:

$refType = $_REQUEST['refType']; 

$refId = $_REQUEST['refId']; 

和:

<input id="refType" name="refType" type="hidden" value="<?php echo ****" /> 
    <input id="refId" name="refId" type="hidden" value="<?php echo ****;?>" /> 

当我把我的页面在JS我做的:

url="addNote.php?refType="+refType+"&refId="+refId; 
    window.location=url; 

但在我的网址,当我尝试去我的网页我有:

?refType = undefined & refId = undefined

有人可以帮我吗?谢谢 !

+3

PHP是服务器端,JS是客户端。您无法从客户端访问服务器端变量,因此refType和refId未在您的JS代码中设置。 – Hokascha

+0

哦好吧...我怎么能检索我的refId和refType呢? ... :( – krowry

+0

我认为''document.getElementByID('refType').val()'代替'refType'会让你。这是一个JS问题,但不是PHP。或者如果你有jquery'$ ('#refType).val()'。 – chris85

回答

1

尝试使用document.getElementById

url="addNote.php?refType="+document.getElementById('refType').value+"&refId="+document.getElementById('refId').value; 
window.location=url; 
+0

你没有得到值,只有表单域 – Hokascha

+0

你好,现在我有:refType = null&refId = null – krowry

+1

你必须使用'document.getElementById('refType')。值',否则你只是插入元素,而不是它的值。 – Frxstrem

-1

您可以添加类似的东西:

<script> 
    var refType = <?php echo $refType ?> 
    var refId = <?php echo $refId ?> 
    var url="addNote.php?refType="+refType+"&refId="+refId; 
    window.location=url; 
</script> 
+0

不要直接插入未经转义的PHP变量到JavaScript代码中,有很多方法可能会出错 – Frxstrem

+0

你好,在我的js? – krowry

+0

Yeap。我认为你的问题在“refType”和“refId”。他们没有定义。 –