如何将Html输入值添加到PHP数组中


How to add Html input values into a PHP array?

我有大量的输入字段。如何将值传递到数组中,以便将数组从一个页面传递到另一个页面?示例:

<input type="text" value=<?php $arry = "compName" ?> placeholder="Company Name"  />

这合法吗?我怎样才能正确地做到这一点?

编辑:

在我的页面中,我有"添加"answers"删除"按钮,可以添加/删除更多的输入字段。我在底部还有一个"预览"按钮。用户将在点击预览之前添加/删除。预览将收集所有输入并调用下一页。因此,场的大小是未知的。

以下是我的代码/标记:

<div class="panel" style="width: 98%; margin:0 auto">
        <div class="row">
            <div class="large-6 columns">
                Advertiser Info: <hr>
                <input type="text" value="compName" placeholder="Company Name"  />
                <input type="text" value= "adEmail" placeholder="Email"  />
                <input type="text" value="adContName" placeholder="Contact Name"  />
                <input type="text" value="adPhone" placeholder="Phone # (NO DASHES)"  />
                <input type="text" value="adStreet" placeholder="Street Address"  />
                <input type="text" value="adCity" placeholder="City"  />
                <input type="text" value="adState" placeholder="State/Provice"  />
                <input type="text" value="adZip" placeholder="Zip/Postal Code"  />
                <input type="text" value="adCountry" placeholder="Country"  />
            </div>
            <div class="large-6 columns">
                Billing Info: <hr>
                <input type="text" value="bEmail" placeholder="Email"  />
                <input type="text" value="bContName" placeholder="Contact Name"  />
                <input type="text" value="bPhone" placeholder="Phone # (NO DASHES)"  />
                <input type="text" value="bStreet" placeholder="Street Address"  />
                <input type="text" value="bCity" placeholder="City"  />
                <input type="text" value="bState" placeholder="State/Provice"  />
                <input type="text" value="bZip" placeholder="Zip/Postal Code"  />
                <input type="text" value="bCountry" placeholder="Country"  />
            </div>
        </div>
    </div>

因此,当用户点击添加时,将制作上述代码的精确副本。

话虽如此,我希望能够使用PHP数组来传递html值。我如何设置我的输入才能做到这一点?

你可以做一些调整,比如:

使用输入类型元素作为数组元素。

因此:

<input type="text" name="elem[name]" value="<?php echo $YOUR_VALUE;?>"
<input type="text" name="elem[class]" value="<?php echo $YOUR_VALUE;?>"
<input type="text" name="elem[marks]" value="<?php echo $YOUR_VALUE;?>"

因此,你发布的变量应该更少。

在这种情况下,您只发布一个变量,而不是三个。

使用Jquery获取输入字段的所有值,并制作一个{input_name:value}的json对象或键值的数组变量,然后将其传递到参数中。

var key_value = {};
$('input').each(function(){
    key_value[$(this).attr('name')]= $(this).val();
})
alert(JSON.stringify(key_value));

现在,您有了一个以字段名为关键字及其值的对象。