YVS1102 YVS1102 - 1 month ago 7
PHP Question

Looping Input PHP

Good day. I have this Input form. How to

loop
an Input with PHP.

<form action="#" method="POST">
Name 1 : <input type="text" name='fName[]' />
Name 2 : <input type="text" name='fName[]' />
Name 3 : <input type="text" name='fName[]' />
<br />
Age 1 : <input type="text" name='fAge[]' />
Age 2 : <input type="text" name='fAge[]' />
Age 3 : <input type="text" name='fAge[]' />
<button type="submit" name="sbmt"> Submit </button>
</form>

<?php
if(isset($_POST['sbmt']))
{
echo "<pre>";print_r($_POST['fName']);
echo "<pre>";print_r($_POST['fAge']);
}

?>


Here is the output example :

Array
(
[0] => Name1
[1] => Name2
[2] => Name3
)
Array
(
[0] => Age1
[1] => Age2
[2] => Age3
)


Well, above is working fine. My question Is, How can i achieve this.

Name : Name1;Name2;Name3;
Age : Age1;Age2;Age3;

Answer

Using implode() would do the trick for you. This function converts your array into a string, joining them using a 'glue' (in this case, the glue is semicolon ;)

Try this:

<?php
    if (isset($_POST['sbmt'])) {
        echo "Name:". implode(';', $_POST['fName']) . ';';
        echo '<br/>';
        echo "Agee:". implode(';', $_POST['fAge']) . ';';
    }
?>
Comments