如何处理PHP中的重复表单输入?

时间:2015-08-10 00:58:07

标签: php

我有一个表单给php脚本发送电子邮件。网站的上下文使我有必要在用户点击按钮时添加重复的表单字段。如何正确处理表单输入?例如,我有一个车辆表格,当用户点击添加车辆时,我附上了几个车辆表格组的副本。这些表单输入具有相同的名称"并且php正在尝试在我的脚本中访问两个或多个同名的表单输入 我应该以某种方式将输入存储为数组吗?

这是我的剧本:

 <?php
  $admin_email = "email@gmail.com";
  $name = $_POST['name'];
  $email = $_POST['email'];
  $address = $_POST['address'];
  $carrier = $_POST['carrier'];
  $yes = $_POST['yes'];
  $no = $_POST['no'];

  $renewal = $_POST['renewal'];
  $homephone = $_POST['homephone'];
  $cellphone = $_POST['cellphone'];
  $year = $_POST['year'];
  $makemodel = $_POST['makemodel'];
  $twowd = $_POST['twowd'];
  $fourwd = $_POST['fourwd'];
  $vin = $_POST['vin'];
  $damage = $_POST['damage'];
  $payment = $_POST['payment'];
  $umuim = $_POST['umuim'];
  $drivername = $_POST['drivername'];
  $driverbday = $_POST['driverbday'];
  $ssn = $_POST['ssn'];
  $dlnumber = $_POST['dlnumber'];
  $dlstate = $_POST['dlstate'];
  $violations = $_POST['violations'];
  $email_body = "Auto Quote\n From: $email \n $address, $carrier, $yes,   $no, $carrier, $renewal, $homephone, $cellphone, $year, $makemodel, $twowd, $fourwd, $vin, $damage, $payment, $umuim, $drivername, $driverbday, $ssn, $dlnumber, $dlstate, $violations)";

  mail($admin_email, "Auto Quote Request", $email_body);

  echo "Thank you for contacting us!";
  ?>

所以,当我按下&#34;添加车辆按钮&#34;我在表单上附上一份表单组的副本,从$ year到$ umuim。我目前的代码是否能够处理这个?我手动测试时没有任何错误(我无法看到电子邮件的样子,因为我没有开发中的邮件服务器),但最后的echo语句有效。

我能看到的一个问题是变量在收到第二个输入后才会重置。我应该以某种方式使用数组吗?感谢。

1 个答案:

答案 0 :(得分:1)

在HTML中,当您需要多个具有相同名称的字段时,请使用

value_counts

数据发布到PHP的时候,$ _POST数组中应该有一个名为<input type="text" name="fieldname[]" ....> <input type="text" name="fieldname[]" ....> <input type="text" name="fieldname[]" ....> fieldname

的字段

然后处理

$_POST['fieldname']