I'm trying to create a PHP script that will "echo" name + name2 + surname from post e.g
John "Edward" "Smith"
Thomas "Edward" "Smith"
Chris "Edward" "Smith"
etc.
This is my script:
<form action="test.php" method="post">
Test: <input type="text" name="name2"> + <input type="text" name="surname">
<input type="submit" value="Submit" />
list of names:
<?php
$name= "
John
Thomas
Chris
(...)
";
?>
+
<?php echo $name; ?> "<?php echo $_POST["name2"]; ?>" "<?php echo $_POST["surname"]; ?>"
When I press Submit I got this
John Thomas Chris + "Edward" "Smith"
instead of
John "Edward" "Smith"
Thomas "Edward" "Smith"
Chris "Edward" "Smith"
Any ideas?
You need to make $name
an array. Try this:
$names = array(
"John",
"Thomas",
"Chris"
);
foreach($names as $name)
{
// Added variables for readability
$name2 = $_POST['name2'];
$surname = $_POST['surname'];
echo "$name '$name2' '$surname'";
}
/* OUTPUT:
John 'Edward' 'Smith'
Thomas 'Edward' 'Smith'
Chris 'Edward' 'Smith'
*/
Hope this helps.
You will need to work with arrays. $name should be an array which you will have to loop through and add the fields from your $_POST form.