Home > front end >  replicate php readline() array in browser
replicate php readline() array in browser

Time:10-23

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?

CodePudding user response:

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']);
  • Related