This is a discussion on How to make a form in php accepting indefinite inputs? within the PHP Language forums, part of the PHP Programming Forums category; Hi, New to PHP, I was trying to figure out how to make such a form: Data: [ ] [add][done] When ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi,
New to PHP, I was trying to figure out how to make such a form: Data: [ ] [add][done] When I enter a data and click the [add] button, the data gets added to a list (and preferably the updated list is displayed on the same page). This repeats until I click [done] button. Any suggestions, directions? Thanks, Ge |
|
|||
|
"Ge B" <geb@sympatico.ca> wrote in message
news:107d794f.0501241323.162c40cf@posting.google.c om... > Hi, > > New to PHP, I was trying to figure out how to make such a form: > > Data: [ ] [add][done] > > When I enter a data and click the [add] button, the data gets > added to a list (and preferably the updated list is displayed > on the same page). This repeats until I click [done] button. > > Any suggestions, directions? > > Thanks, > Ge You might want to ask this in a Javascript/DHTML group. A server-side only solution would be very tideous to use. |
|
|||
|
Ge B wrote:
> Hi, > > New to PHP, I was trying to figure out how to make such a form: > > Data: [ ] [add][done] > > When I enter a data and click the [add] button, the data gets > added to a list (and preferably the updated list is displayed > on the same page). This repeats until I click [done] button. > > Any suggestions, directions? yourpage.php: ________________ <?php if (isset($_POST['action'])) { // data is entered into database whichever button is pressed $clean_data = validate_data($_POST['data']); insert_into_database($clean_data); if ($_POST['action'] == "done") { header("Location: http://server/otherpage.php"); exit(); } } show_list_of_data_from_database(); ?> <form action="" method="post"> <!-- action="" means this same page --> <p> <label for="data">Data:</label><input type="text" name="data" id="data" value="" /> <input type="submit" name="action" value="add" /> <input type="submit" name="action" value="done" /> </p> </form> > > Thanks, > Ge |
|
|||
|
Thanks for the suggestion. I see the point is use database to store
the list. But is there any way that I can store and pass along the list without going into the database first? Yes, a portion of the list will eventually go into the database, but at this stage, I perfer not if I can... Further suggestions? Thanks! Ge |
|
|||
|
Ge B wrote:
> > New to PHP, I was trying to figure out how to make such a form: > > Data: [ ] [add][done] > > When I enter a data and click the [add] button, the data gets > added to a list (and preferably the updated list is displayed > on the same page). This repeats until I click [done] button. > > Any suggestions, directions? Use JavaScript to modify the form on the client side and submit it to the server when done. Cheers, NC |
|
|||
|
What about using a SESSION array. For instance,
$_SESSION['valid'][$name]. Thus, to adapt Dani's script above: if (isset($_POST['action'])) { // data is entered into database whichever button is pressed $clean_data = validate_data($_POST['data']); $_SESSION['valid']['data'] = $clean data ; if ($_POST['action'] == "done") { header("Location: http://server/otherpage.php"); exit(); } Once the user hits done, you'd have the $_SESSION['valid'] array all ready to drop in the database or do with as you please. Any obvious drawbacks to that? Tom |
|
|||
|
Ge wrote: > Thanks for the suggestion. I see the point is use database to store > the list. But is there any way that I can store and pass along the list > without going into the database first? > > Yes, a portion of the list will eventually go into the database, but at > this stage, I perfer not if I can... Try this modification of the above code: <?php session_start(); $tmp = (isset($_SESSION['tmp']))?$_SESSION['tmp']:array(); if (isset($_POST['action'])) { // data is added onto the $tmp array whichever button is pressed if ($_POST['data'] != '' )$tmp[] = htmlentities(stripslashes($_POST['data'])); if ($_POST['action'] == "done") { // clean_data($tmp); // insert_into_database($tmp); echo 'The final list of data entered is:<br>'."\n"; for ($i=0;$i<count($tmp);$i++) echo 'Data: ' . $tmp[$i] . "<br>\n"; unset($_SESSION['tmp']); exit(); } $_SESSION['tmp'] = $tmp; } for ($i=0;$i<count($tmp);$i++) echo 'Data: ' . $tmp[$i] . "<br>\n"; ?> <form style="margin-top:0;padding-top:0" action="<? echo $_SERVER['PHP_SELF'] ?>" method="post"> <label for="data">Data:</label><input type="text" name="data" id="data" value="" /> <input type="submit" name="action" value="add" /> <input type="submit" name="action" value="done" /> </form> Ken (posting from Google Groups Beta which unindents code :-( ) |