Forum Moderators: coopster

Message Too Old, No Replies

how to collect data from the innerhtml elements

how to collect data from the innerhtml elements

         

Heeren

9:45 am on Jan 10, 2007 (gmt 0)

10+ Year Member



hi
I have a form that add several form elements, lets say three text boxes, eg: text1, text2, text3. There is a javascript button entitled something like "add a new row", which upon execution, will append another row onto the table (or div if you will), holding another set up text boxes. Something like this structure:

textbox1row1, textbox2row1, textbox3row1
textbox1row2, textbox2row2, textbox3row2, etc.

I have this accomplished and can dynamically build this sort of structure to any length (and remove any elements).

how to get data from these form fields in php
please help me asap
thanks

omoutop

12:19 pm on Jan 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If i get this correctly, you are wondering how to get (through POST) all your textfields into an insert/update statement, since you don't know the exact number of textfields?

For start, make sure all your textfields names, follow a logical patern: for example text1, text2, text3, text4...

Make sure that all your data are submited by your form
(check your submission page with something like:
echo "<pre>";
print_r($_POST);
echo "</pre>";

Now the tricky part:
put a hidden field into your form - this will be your textfield counter. Starting value is your minimum number of textfields (if your form has always 3 text fields, this counter will be 3 at default value).

In your java code you must add a function that increase your hidden counter whenever you "add" textfields to your form, or decrease it.

When you submit your form (and if the java part works correctly), loop through your textfields (since you know their exact number stored in your hidden counter)

something like:
for ($i=1;$i<=$hidden_counter;$i++)
{
$my_field = "textfield".$i;
$my_value = $$my_field;

if ($my_value!="")
{
// do something with your data
}
}

eelixduppy

1:11 pm on Jan 10, 2007 (gmt 0)




For start, make sure all your textfields names, follow a logical patern: for example text1, text2, text3, text4...

This would be the more difficult way to do this. You are much better off doing it like this:


<input type="text" [b]name="text[]"[/b] />
<input type="text" [b]name="text[]"[/b] />
<input type="text" [b]name="text[]"[/b] />

This way when you submit the form you already have these values in an array:


echo '<pre>';
[url=http://us2.php.net/print-r]print_r[/url]($_POST['text']);
echo '</pre>';

Good luck :)

Heeren

1:14 pm on Jan 10, 2007 (gmt 0)

10+ Year Member



thanks its working

omoutop

2:02 pm on Jan 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



eelixduppy's way is much faster... haven't though of it :)