How to make a form in php accepting indefinite inputs?

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-24-2005
Ge B
 
Posts: n/a
Default How to make a form in php accepting indefinite inputs?

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
Reply With Quote
  #2 (permalink)  
Old 01-25-2005
Chung Leong
 
Posts: n/a
Default Re: How to make a form in php accepting indefinite inputs?

"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.


Reply With Quote
  #3 (permalink)  
Old 01-25-2005
Dani CS
 
Posts: n/a
Default Re: How to make a form in php accepting indefinite inputs?

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

Reply With Quote
  #4 (permalink)  
Old 01-25-2005
Ge
 
Posts: n/a
Default Re: How to make a form in php accepting indefinite inputs?

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

Reply With Quote
  #5 (permalink)  
Old 01-25-2005
NC
 
Posts: n/a
Default Re: How to make a form in php accepting indefinite inputs?

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

Reply With Quote
  #6 (permalink)  
Old 01-25-2005
Tom
 
Posts: n/a
Default Re: How to make a form in php accepting indefinite inputs?

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

Reply With Quote
  #7 (permalink)  
Old 01-25-2005
Ken Robinson
 
Posts: n/a
Default Re: How to make a form in php accepting indefinite inputs?


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="" />&nbsp;
<input type="submit" name="action" value="add" />&nbsp;<input
type="submit" name="action" value="done" />
</form>

Ken (posting from Google Groups Beta which unindents code :-( )

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:19 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0