0

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>

5 Answers 5

2

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>";
}
Sign up to request clarification or add additional context in comments.

5 Comments

i am trying your code and getting the following error. (1)Warning: explode() expects parameter 2 to be string, array given in line 19 and (2)Warning: Invalid argument supplied for foreach() line 20
Yes got it you are getting $_POST['guestname'] name in array like guest name = ['first, second'] but you have to convert it to name = "first, second"
@amiHasan just check updated answer and try like that.
ohh... nice.. it's working now. Thank you so much :)
Its my pleasure. :)
0

Try below code.

<?php
    foreach($_POST['guestname'] as $key => $value)
    {
       //your content
    } 
?>

Comments

0

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)

Comments

0
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

2 Comments

let say i've 3/4 names separated by comma. i want to print the names one by one.
So my answer do that.
0

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>";
}

?>

Comments

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.