Reputation: 125
I am trying to insert multiple name in guestname input field. So i've declared guestname as an array. After inserting some name in guestname field using separated by comma ( e.g : superman, batman, spiderman ) i get output as "guestname":["superman, batman, spiderman "]. I want to run loop counting all the values of the array and print one by one where others data (e.g email and address) will remain same.
<input type="text" name="guestname[]" multiple>
<input type="text" name="email">
<input type="text" name="address">
<tr>
foreach(array_count_values($_POST['guestname'] as $key => $value)
{
echo "<td>". $value."</td>";
echo "<td>". $_POST["email"]."</td>";
echo "<td>". $_POST["address"]."</td>";
}
</tr>
Upvotes: 0
Views: 60
Reputation: 701
First you have to expload comma seprated value and then read it from data.
$guest = explode(",", $_POST['guestname'][0]);
foreach($guest as $value)
{
echo "<td>". $value."</td>";
echo "<td>". $_POST["email"]."</td>";
echo "<td>". $_POST["address"]."</td>";
}
Upvotes: 2
Reputation: 1700
Hope you are looking something like below:
<form method="post">
<input type="text" name="guestname">
<input type="text" name="email">
<input type="text" name="address">
<input type="submit" value="submit">
</form>
<?php
if(isset($_POST['guestname'])){
echo "<table border='1'>";
foreach(array_filter(explode(',',$_POST['guestname'])) as $key => $value)
{
echo "<tr><td>". $value."</td>";
echo "<td>". $_POST["email"]."</td>";
echo "<td>". $_POST["address"]."</td></tr>";
}
echo "</table>";
}
?>
Upvotes: 0
Reputation: 6703
foreach(explode(',', join(',', $_POST['guestname'])) as $key => $value)
{
...
}
It's what you are loking for?
It merge all $_POST['guestname'] values into a string adding additional , and then re-explode them in array and loop in.
Example:
$_POST['guestname'][] = "name1, name2, name3, name4";
$_POST['guestname'][] = "name5, name6, name7";
return:
name1
name2
name3
name4
name5
name6
name7
Upvotes: 0
Reputation: 1178
Code "guestname":["superman, batman, spiderman "]
means that "superman, batman, spiderman "
is a string. If these were an array it would be "superman", "batman", "spiderman "
. So maybe you can use explode
to get an array of elements.
For example
$guestname = explode("," , $_POST['guestname']);
And then
foreach(array_count_values($guestname as $key => $value)
Upvotes: 0
Reputation: 947
Try below code.
<?php
foreach($_POST['guestname'] as $key => $value)
{
//your content
}
?>
Upvotes: 0