2

I have a form to email php script. The context of the website makes it necessary for me to add repeating form fields at the User's click of a button. How do I properly handle the form input? For example I have a Vehicle form and when the User click's add vehicle I append a carbon copy of several of the vehicle's form groups. These form inputs have the same "name" and there for php is trying to access two or more form inputs of the same name in my script.
Should I store the inputs as an array somehow?

Here is my script:

 <?php
  $admin_email = "[email protected]";
  $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!";
  ?>

So when I press the "add vehicle button" I append a copy of the form groups on my form from $year to $umuim. Is my current code capable of handling this? I have not had any errors when testing manually (I can't see what the email looks like as I don't have a mail server in development), but the echo statement at the end works.

One problem I could see is would the variable just reset after receiving the second input. Should I use an array somehow? Thanks.

2
  • Well ECHO the $email_body as well if you want to see whats in the email Commented Aug 10, 2015 at 1:12
  • Any field that can have more than one value sent over should be configured in the HTML form as an array, and of course parsed in PHP at the server side as an array of 1 or more elements. You can always run a foreach loop on an element that has been POSTed as an array. Commented Aug 10, 2015 at 1:16

1 Answer 1

1

In HTML when you want multiple fields with the same name you use

<input type="text" name="fieldname[]" ....>
<input type="text" name="fieldname[]" ....>
<input type="text" name="fieldname[]" ....>

The when the data is posted to PHP there should be a field in the $_POST array called fieldname $_POST['fieldname']

You then process that like

foreach ( $_POST['fieldname'] as a_name ) {

    echo $a_name;

}
Sign up to request clarification or add additional context in comments.

3 Comments

What would happen if I tried to process the field name without the foreach loop?
Ignore the first comment. So these loops would just replace $fieldname = $_POST['fieldname']?
No, the loop would step through each and every array element of $_POST['fieldname'] so you can then do something with each of the values posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.