2013-01-06 126 views
0

我有一個隱藏的輸入字段,我使用jQuery這樣的一些任意值填寫:PHP POST調用沒有返回變量

function foo() { 
    // Obtains the contents of a div within the current page 
    var $elem = $("#something").html(); 
    // Places the contents of the div into a hidden input field 
    $("#hidden").val($elem); 
} 

在調試的東西,我可以看到ELEM變量從獲得的HTML在所需的div內,而不是該變量是否傳遞給隱藏的輸入域值。

我有一個表格:

<form method="POST" action="file.php" target="_blank"> 
    <input id="hidden" type="hidden" value=""/> 
    <input type="submit" onmouseover="foo()" value="Download"/> 
</form> 

這在提交執行file.php:

<?php 
    $html = $_POST["hidden"]; 
    echo $html; 
?> 

回聲調用返回什麼,我得到的是一個空白頁。我想知道爲什麼隱藏的輸入字段的值沒有被更改,或者爲什麼在POST調用期間沒有被傳遞。

一些進一步的實驗表明,即使我設置隱藏字段的值與一些隨機值:

<input id="hidden" type="hidden" value="Some Value"/> 

現在還沒有PHP文件的執行過程中獲得的。回聲調用不返回任何內容。我獲得這個隱藏的輸入字段的值有什麼問題?我一直非常謹慎地使用PHP,但過去在POST提交時沒有從表單獲取值的問題。

回答

6

你需要爲了與$ _ POST

<input id="hidden" name="hidden" type="hidden" value=""/> 
+0

哇訪問它在PHP中有一個名稱爲您的輸入字段,那麼容易,感謝 –

+0

沒問題。你做得正確,你只是忘記了name屬性。 – Boundless

3

你應該只表明name屬性

<form method="POST" action="file.php" target="_blank"> 
    <input name="hidden" id="hidden" type="hidden" value=""/> 
    <input type="submit" onmouseover="foo()" value="Download"/> 
</form>