dimanche 29 mars 2015

How to submit a single form value multiple times and store them in an array?

Attempt 1: This shows what I want to achieve. The same array but different key and value pair.When printed, it list out all the keys and values.



<?php

$rental[]=array('day'=>1,'rate'=>10);
$rental[]=array('day'=>2,'rate'=>20);
$rental[]=array('day'=>3,'rate'=>30);
$rental[]=array('day'=>4,'rate'=>40);
print_r($rental);
?>


Attempt 2: Now, I'm using a single form to submit multiple times.I expect the key and values to be stored inside an array each time the form is submitted. But what happens is, the last value pair overwrites the previous one.Therefore only one pair is shown. So far, I can store them with the help of session. But I just wonder if there's a way to achieve it via php array on the same page?



<?php

$rental[]=array('day'=>$_POST['days'],'rate'=>$_POST['rental']);

print_r($rental);
?>
<form action="#" method="post">
Number of Days: <input type="text" name="days" value=""><br/>
Rental rate: <input type="text" name="rental" value=""><br/>
<input type="submit" name="add_rental_rate" value="Add Rental Rate">
</form>


Thanks for any help.


Aucun commentaire:

Enregistrer un commentaire