如何处理庞大的形式

d7p*_*p4x 6 php mysql forms

我有一个问题,你如何使用php和mysql处理表单中的大量数据?

我有下一个:~50个字段(输入文本,checkboxex,select,textarea)

之后,我需要将其保存到MySQL数据库中,我需要选择并过滤这些数据.你有练习,你在项目中使用了什么?

Pau*_*een 6

要以这种形式组织数据,您可以使用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)