2013-07-29 59 views
1

表單提交後我無法獲得$_POST['value']

我已經使用javascript爲輸入字段賦值。 代碼:

function updateValue(pid, value){ 
    // this gets called from the popup window and updates the field with a new value 
    document.getElementById(pid).value = value; 
} 

上述功能是通過一個彈出寡婦叫的值賦給我的輸入字段:pid

這裏是form腳本:

<form action="test.php" method="post"> 
    <input type="text" name="project_name" id="pid" disabled="disabled" /> 
    <input type="submit" id="search" name="search" value="Search" /> 
</form> 

在我的測試。我已經做了:

include 'functions.php'; //Has the connection script 
if (isset($_POST['search'])){ 
    echo $project_id = $_POST['project_name']; 
    $sql = mysql_query("SELECT * from item 
        where category like '$project_id %' 
        "); 
    echo $num = mysql_num_rows($sql); 
} 

但我我得到錯誤:Undefined index: project_name

+0

請將'print_r($ _ POST)'的結果添加到您的PHP腳本的開頭。 –

+0

啊,我沒有看到你的「禁用」屬性... –

回答

4

已禁用的輸入不張貼。使用hidden這樣的輸入:

<form action="test.php" method="post"> 
    <input type="text" name="project" disabled="disabled" /> 
    <input type="hidden" name="project_name" id="pid" /> 
    <input type="submit" id="search" name="search" value="Search" /> 
</form> 
+0

-1:這不適用於當前腳本。使用此代碼時,項目名稱將始終爲空。 –

+0

@Bora我這樣做'' 但是在我的php頁面中,它不是'echoing'。 我的意思是'echo $ _POST ['project_name'];'沒有顯示任何東西。 – aki2all

+0

已更新並添加了'id =「pid」'。你設置ID爲PID的輸入值js – Bora

1

您的project_name輸入字段被禁用。 刪除此屬性,然後它將工作。

0

這是因爲禁用的屬性不會沿着表單請求傳遞。

如果您要設置通過JavaScript那部分作品的價值,並與該替換文本輸入:

<input type="hidden" name="project_name" id="pid" /> 

這將工作

2

嘗試填充隱藏字段,當你設置的值進入禁用字段

<form action="test.php" method="post"> 
     <input type="text" name="project" id="pid" onchange="document.getElementById("pid-new").value=document.getElementById("pid").value" disabled="disabled" /> 
     <input type="hidden" id="pid-new" name="project_name" /> 
     <input type="submit" id="search" name="search" value="Search" /> 
    </form>