469,568 Members | 1,547 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

adding more fields to a form

hi all,

I wonder if I could do the following task in PHP before searching for
javascript which I prefer not to have in my code:

I have a form to create new tables in a database and, since I can't
know how many columns needs each table, I want to load the page with a
single text field in the form and then with a button adding more
fields to the form if required.

so something like this:

text field
"add more fields" button
"submit" button

everytime the "add more fields" button is clicked the page reloads and
the form has another text field to enter the name of the table column.

Can be done with php instead of javascript ?

Thanks in advance for any tip

johnny

Jan 17 '06 #1
5 6351
Johnny, I'm not sure if I understand you correctly, but you can try
this:

note.. I didn't test this code, but I think this is what you're looking
for.

<?PHP

if ($_POST['Submit'])
{
$field = $_POST['field'];

for ($i = 0; $i < count($field); $i++)
{
// echo all results
echo $field[$i] ."<br />\n";
}
}
else
{
// $fc = field count

$fc = $_POST['fc'];
if (!is_numeric($fc) || $fc == "")
{
// if fc isn't set - default it to 1
$fc = 1;
}

if ($_POST['AddField'])
{
// if AddField button was clicked - increment $fc
$fc++;
}
?>
<form action="<?PHP echo $_SERVER['PHP_SELF'];?>" method="post">
<?PHP
for ($i = 1; $i <= $fc; $i++)
{
echo "<input type=\"text\" name=\"field[". $i ."]\"
value=\"\"><br />\n";
}
?>
<input type="submit" name="AddField" value="add more fields">
<input type="submit" name="Submit" value="submit">
<input type="hidden" name="fc" value="<?PHP echo $i;?>">
</form>
<?PHP
}
?>

Jan 17 '06 #2
"johnny" <mr********@yahoo.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
hi all,

I wonder if I could do the following task in PHP before searching for
javascript which I prefer not to have in my code:

I have a form to create new tables in a database and, since I can't
know how many columns needs each table, I want to load the page with a
single text field in the form and then with a button adding more
fields to the form if required.

so something like this:

text field
"add more fields" button
"submit" button

everytime the "add more fields" button is clicked the page reloads and
the form has another text field to enter the name of the table column.

Can be done with php instead of javascript ?

Thanks in advance for any tip

johnny


Seems pretty straightforward and yes, PHP can handle everything including
validation.

I would simply have a form with a single input and submit button. Every time
an entry is submitted, the page refreshes displaying the same form followed
by the list of previously entered values. The form is always at the top of
the page for convenience. You should also inform the user the table creation
table was successful.
Jan 17 '06 #3
Bosconian wrote:
"johnny" <mr********@yahoo.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I have a form to create new tables in a database and, since I can't
know how many columns needs each table, I want to load the page with a
single text field in the form and then with a button adding more
fields to the form if required.


Seems pretty straightforward and yes, PHP can handle everything including
validation.

I would simply have a form with a single input and submit button. Every time
an entry is submitted, the page refreshes displaying the same form followed
by the list of previously entered values. The form is always at the top of
the page for convenience. You should also inform the user the table creation
table was successful.


Only now did I notice that johnny wants to create tables.

johnny, unless you have a good reason to let users create tables:
Don't do that.
Don't allow users to create tables.
Create all the tables you need before making your script available.

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Jan 17 '06 #4

"Pedro Graca" <he****@dodgeit.com> wrote in message
news:sl*******************@ID-203069.user.individual.net...
Bosconian wrote:
"johnny" <mr********@yahoo.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I have a form to create new tables in a database and, since I can't
know how many columns needs each table, I want to load the page with a
single text field in the form and then with a button adding more
fields to the form if required.


Seems pretty straightforward and yes, PHP can handle everything including validation.

I would simply have a form with a single input and submit button. Every time an entry is submitted, the page refreshes displaying the same form followed by the list of previously entered values. The form is always at the top of the page for convenience. You should also inform the user the table creation table was successful.


Only now did I notice that johnny wants to create tables.

johnny, unless you have a good reason to let users create tables:
Don't do that.
Don't allow users to create tables.
Create all the tables you need before making your script available.

--
If you're posting through Google read <http://cfaj.freeshell.org/google>


I complete agree. I assume this was some sort of admin tool and not for
public consumption.
Jan 17 '06 #5
sorry, I forgot to reply, thanks Adam, this is what I had to do.
For Bosconian and Pedro , yes it's part of an admin tool to create and
manage contact lists, each list has its own table.

johnny

Jan 21 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Randy A. Bohannon | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.