-1

simple php code that works well in terminal:

<?php

$a = array();

for($i=0; $i<3; $i++){

    $b = readline('time: ');

    $c = readline('money: ');

    $d = array('time'=>$b, 'money'=>$c);

    array_push($a, $d);

    }

print_r($a);

this pushes the values of multiple entries into an array, creating an array of arrays. however, readline() does not work in the browser. i know i can use javascript easily enough but am trying to replicate this simple action using only php and html. and i really like the way readline() works. i have tried variations on the following but am left scratching my head:

<form method="POST">
    
    <?php 

      for($i=0; $i<3; $i++){ 

    ?>

        <input name = 'time'>
    
        <input name = 'money'>
    
    <?php 
    
        } 
    
    ?>
       
    <input type="submit">
        
</form>

<?php

print_r($_POST['time']);

was hoping print_r($_POST['input name']) would return an array, but instead only gives the last input entry. is there a straight forward way to do this with php, or do i HAVE to use a client-side script like javascript?

1
  • You can use str_repeat for elegancy. Commented Oct 23, 2022 at 8:20

1 Answer 1

1

Just add [] to input names:

<form method="POST">
    
    <?php 

      for($i=0; $i<3; $i++){ 

    ?>

        <input name = 'time[]'>
    
        <input name = 'money[]'>
    
    <?php 
    
        } 
    
    ?>
       
    <input type="submit">
        
</form>

<?php

print_r($_POST['time']);
Sign up to request clarification or add additional context in comments.

1 Comment

When you see a new basic question asked in 2022, you can be sure that Stack Overflow already has the question answered several times. The fundamental goal of closing duplicate questions is to help people find the right answer by getting all of those answers in one place.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.