$ _POST数组中缺少值

时间:2018-12-21 17:12:40

标签: php html forms

我有一个简单的表格,如下所示:

<form class="" method="post" action="">
<label for="fn">First Name:</label> <input id="fn" type="text" 
name="firstName">
<label for="ln">Last Name:</label> <input id="ln" type="text" 
name="lastName">
<label id='spou' for="sn">Spouse Name:</label> <input id="sn" type="text"  
name="spouseName">
<label id='comp' for="cn">Company Name:</label> <input id="cn" type="text" 
name="companyName">
<input type="submit" name="submit" value="Submit">
</form>

该表单正在根据开发工具提交所有数据。

firstName: Nacy
lastName: Smith
spouseName: George
companyName: Baking Inc.

firstName$_POST数组中为空白的$_REQUEST外,所有值均显示。其他值在那里。当我使用print_r($_REQUEST);print_r($_POST);时,我得到:

Array ( [firstName] => [lastName] => Smith [spouseName] => George [companyName] => Baking Inc. [submit]=> Submit)

我想念什么?

我尝试了这个PHP some $_POST values missing but are present in php://input和其他很多方法,但是当一个没有特殊字符的小数组中只有一个值丢失时,似乎都没有解决。

谢谢!

编辑:由于有些人想要我尝试的初始PHP代码无法正常工作,所以我将其包括如下:

//Process form data
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $types = array('Submit', 'Submit &amp; Add Another', 'Add Data');
    $fields = array();
    $values = array();
    foreach (array_keys($_POST) as $key) {
        ${$key} = clean_input($_POST[$key]);
            if(!in_array(${$key}, $types) && ${$key} !=''){
                array_push($fields, $key);
                array_push($values, ${$key});
            }
    }

    $table = ${$type};
    $fields = implode(', ', $fields);
    $values = implode("', '", $values);

//Send data to server
if (isset($lastName) || isset($companyName)){
$query = "INSERT INTO $table ($fields, current) VALUES ('$values', '1')";

1 个答案:

答案 0 :(得分:-2)

我发现了问题。表单上有一些Javascript,它还有另一个名为“ firstName”的隐藏字段。那就是覆盖数组中的键。一旦删除它,表格就没问题了。