2017-01-30 133 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」。他們沒有定義。 –