简体   繁体   中英

How to get/post the values in PHP from a dynamically form which add automatically input form elements

So my form is creating Dynamically forms based on the Total Person. It the Total Person is: 3

Then 3 Forms will appear with each different "valuenames" like:

<input name="lastname[1]">
<input name="lastname[2]">
<input name="lastname[3]">

But how can I call the results of each input in $POST PHP?

See this picture what I mean:

在此处输入图片说明

I need to show, get, call that values in php.

So it is the print, I need to post each values of this arrays to send the results as a mail:

**[lastname] => Array ( [1] => MARVIN [2] => JIM [3] => ZOO**

Example:

Lastname 1: MARVIN
Lastname 2: JIM
Lastname 3: ZOO

The following code will postfix a variable with a number to evaluate, and the for loop will loop over 0 to 4 (which is 5 fields in total) and then echo out the result.

You will have to adapt the code to match your other form variables.

<?php

for($i=0;$i<5;$i++){

    $var = 'lastname'.$i;

    if(isset($_POST[$var])){
        echo $_POST[$var];
    }
}

?>

<html>
<head>
<title></title>
</head>
<body>

<form action="<? $_SERVER['PHP_SELF'];?>" method="post">

    <input type="text" name="lastname1" />
    <input type="text" name="lastname2" />
    <input type="text" name="lastname3" />
    <input type="text" name="lastname4" />
    <input type="text" name="lastname5" />
    <input type="submit" value="test">
</form>
</body>
</html>

I see that you do get the values on your array. but your php code is beginners, cut, paste, adaptation and "happy thinking", We all started one day, so I will review it in some detail.

You initiate your loop with

$message .=

should be:

$message =

As you are initiating a mesagge, not concatenating to it...

Then you follow it up with:

$message =

when now, you are concatenating to $message, it should be

$message .=

read: languaje operators

Then kind of really basic stuff,

Then you follow messsage = with "White space", so the php processor goes reading until a statement closure ";" is found. The parser wont find it because it keeps reading into yor message string until "From" is found. That is because the parser either finds a literal string between "" or '' or a concatenation operator ".", for which the parser will add the variable value between concatenators. As you can check, those two conditions are met until the "From" is encountered at which point the parse will throw an error because it is either expecting an statment closure ";" or a new concatenation operator, but finds a literal.

If you did not catch it probably you need to turn on error reporting:

Put this at the begining of the page: (Do take those out in production!!)

 error_reporting(E_ALL);
 ini_set("display_errors", 0);

Happy coding!

Nevertheless, althoug some times work, it is absolutely recommended no to span comands over several lines.

There are two choices, concaternate for every line, like you have in the $headers secction...

Or use the heredoc syntax although that takes some practice to master.

Heredoc sintax will take anythin between the "<<

Additionally the parser will greedily look for variables or unidimensional array variables and replace them with their values, more complex values should be enclosed by curly braces, just for helping you start, here is the revised php code with the $meesage block under heredoc syntax an, but do read this basic page if you want to make some progress:

php strings manual

here is the revised php code:

      <?php

  error_reporting(E_ALL);
  ini_set("display_errors", 0);

  foreach($_POST['name'] as $index => $name)
       { // Just for clarity sake,Obtain this person information. similarly could have been done inside the $message build, 
         // Left bornday to show how is that done......
       $Passenger  =  ($index);
       $Name  = $name;
       $LastName = $_POST['lastname'][$index];     
       $BornDay = $_POST['bornday'][$index];     
       $Trip = $_POST['destination'][$index];     
       $Persons = $_POST['vol'][$index];     
       $Kids = $_POST['kinderen'][$index];     
       $Babys = $_POST['Babys'][$index];     


  $message .= <<<EOD         
  Trip: $Trip<br><br>
  Persons: $Persons<br>
  Kids:  $Kids<br>
  Babys: $Babys<br><br>
  Personsinformation:   $Name  $LastName   {$_POST['bornday'][$index]  <br><br>
  EOD;

  // DBG // Show the message
  echo "The $index message  is: $message<br>";

  // Sending mail dpends a bit on you enviroment, supposig this applies:

  $headers = 'From:' . $_POST['email'] . "\r\n";
  $headers .= 'Reply-To:' . $_POST['email'] . "\r\n";
  $headers .= "MIME-Version: 1.0\r\n";
  $headers .= "Content-Type: multipart/mixed; ";
  $headers .= "boundary=$num\r\n";
  $headers .= "--$num\r\n";  

  // Define the message section
  $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
  $headers .= "Content-Transfer-Encoding:8bit\r\n\n";
  $headers .= "$message\r\n";
  $headers .= "--$num\r\n";

  // Send email

  mail ($to, $subject, '', $headers);
  print_r($_POST);
  }

  ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM