Mohammadali mirahamed Mohammadali mirahamed - 3 months ago 9
PHP Question

how to echo input text in For{}

I write below code and put an html input text in for :

for($i=0;$i<5;$i++)
echo '<input type="text" name="subject">';


but when i want to echo the that value I type in to the above text box
it returns "" nothing

echo $_POST['subject'];


I have

<form name="form1" method="post" action="index.php">


Tag top of this codes ?

Sorry if I had Mistake

Answer

Make the input as array and read the values in PHP using loop like below.

HTML code :

<form name='myForm' action='index.php' method="post">
     <?php
     for {$i=i;$i<5;$i++} {
           echo "<input type='text' name='subject[]'>";
     }
    ?>  
</form>

PHP Code :

$subjectArray = $_POST['subject'];
foreach($subjectArray as $key => $val) {
     if($val != "") {  
         echo $key."==".$val."<br>";
     }
}