无法使用PHP $ _POST数组检索输入字段值

时间:2013-07-29 11:42:07

标签: php javascript dom

表单提交后我无法获得$_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>

在我的test.php中,我已经完成了:

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

4 个答案:

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

答案 1 :(得分: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>

答案 2 :(得分:1)

您的project_name输入字段已禁用。 删除此属性然后它将工作。

答案 3 :(得分:0)

这是因为禁用的属性不会沿着表单请求传递。

如果您通过javascript设置值,并且该部分有效,请将此文本输入替换为:

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

这将起作用

相关问题