要以这种形式组织数据,您可以使用HTML表单数组.假设我们提交了大量有关房屋的数据.我数据拆分成例如部分:general,geo,features,descriptions和撰写窗体这样.
<form>
<fieldset>
<legend>General information</legend>
<input type="number" name="general[pax]" value="" placeholder="Pax" />
<input type="number" name="general[pets]" value="" placeholder="Pets" />
<input type="text" name="general[type]" value="" placeholder="Type" />
</fieldset>
<fieldset>
<legend>GEO data</legend>
<input type="text" name="geo[longitude]" value="" placeholder="Longitude" />
<input type="text" name="geo[latitude]" value="" placeholder="Latitude" />
</fieldset>
<fieldset>
<legend>Features</legend>
<input type="checkbox" name="features[internet]" value="1" title="Internet" />
<input type="checkbox" name="features[pool]" value="1" title="Pool" />
<input type="checkbox" name="features[conditioner]" value="1" title="A/C" />
</fieldset>
</form>Run Code Online (Sandbox Code Playgroud)
更新:使用<fieldset>和<legend>标签和一些jQuery(未演示)您可以轻松地显示/隐藏不同的组并根据您的口味命名它们.
提交此类表单后,您将能够访问以下值:
$pets = (int)$_POST['general']['pets'];
$features = $_POST['features'];
$lon = (float)$_POST['geo']['longitude'];
$lat = (float)$_POST['geo']['latitude'];
Run Code Online (Sandbox Code Playgroud)
它将简化您的开发并减少控制/解析/枚举不同数据组的工作量.
更新:或者另一种可能的变体是
<input type="text" name="params[param1]" value="" />
<input type="text" name="params[param2]" value="" />
<input type="text" name="params[param3]" value="" />
Run Code Online (Sandbox Code Playgroud)
同时在 PHP
$params = $_POST['params']; // getting our isolated array of parameters
$names = array_keys($params); // extracting names from array
$values = array_values($params); // extracting values from array
$mysql->insert($names, $values) // and trying to implement desired solution
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2749 次 |
| 最近记录: |