表单提交后无法获取$_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
禁用的输入不会发布。使用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>
当您将值设置为禁用字段时,请尝试填充隐藏字段
<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>
您的project_name输入字段被禁用。删除这个属性,它就会工作。
这是因为禁用的属性不会随表单请求一起传递。
如果您通过javascript设置值,并且该部分有效,请将您的文本输入替换为:
<input type="hidden" name="project_name" id="pid" />
这将工作