johnwayne77 Posted May 28, 2008 Share Posted May 28, 2008 let me explain you my system and where i got stuck a java script generates me multiple <input> fields in a form and renames them as following: field1, field2, field3, field4. i have a variable ($count) which counts how many fields are added (4 in the above example). now i want to add the values of those fields in mysql. i don't know how could i do that... any ideas? Link to comment https://forums.phpfreaks.com/topic/107623-solved-gather-fields-value/ Share on other sites More sharing options...
rhodesa Posted May 28, 2008 Share Posted May 28, 2008 First, it's much easier to use field[] instead of field1,field2,etc. By having several fields with the same name (those close square brackets are the key) PHP will create a nice array you can loop over. HTML generated by javascript should look like: <input type="text" name="field[]" /> <input type="text" name="field[]" /> <input type="text" name="field[]" /> <input type="text" name="field[]" /> PHP <?php foreach($_POST['field'] as $value){ //mysql code for an insert } ?> If you need help on how to do the actual insert, you will need to provide more information on how the table is set up and how you want the data to be inserted. Link to comment https://forums.phpfreaks.com/topic/107623-solved-gather-fields-value/#findComment-551621 Share on other sites More sharing options...
johnwayne77 Posted May 28, 2008 Author Share Posted May 28, 2008 here is my script http://www.ducoci.ro/ASA.php believe me, i spent 2 days to find a good solution for dynamically added input fields into forms. if you look in the source code, you will notice at the beginning of the document the java script that generates the fields. do you think foreach($_POST['field'] as $value){ would be appropiate for my situation? thanks Link to comment https://forums.phpfreaks.com/topic/107623-solved-gather-fields-value/#findComment-551626 Share on other sites More sharing options...
rhodesa Posted May 28, 2008 Share Posted May 28, 2008 Well, it's a little tough for me to understand since it's not english, but instead of this: newdiv.innerHTML = '<input type=text name='+num+'> | <input type=text name=a'+num+' | <input type=text name=b'+num+'>'; you can just do: newdiv.innerHTML = '<input type="text" name="field1[]"> | <input type="text" name="field2[]" | <input type="text" name="field3[]">'; now, this will be a little confusing at first, but the POST that the form submits to, will now have an array for field1, field2, and field3. just remember though, the "group" of 3 fields is the same index across the 3 arrays. so, the first group of 3 would be $_POST['field1'][0], $_POST['field2'][0], and $_POST['field3'][0]. and your PHP code would be something like: <?php foreach(array_keys($_POST['field1']) as $n){ print $_POST['field1'][$n] . '|' . $_POST['field2'][$n] . '|' . $_POST['field3'][$n]; } ?> make sense? Link to comment https://forums.phpfreaks.com/topic/107623-solved-gather-fields-value/#findComment-551652 Share on other sites More sharing options...
johnwayne77 Posted May 29, 2008 Author Share Posted May 29, 2008 yeah it would .. but i can't change name=a'+num+' into name="field2[]" coz of the java script. any other ideas? i have the php $variable containing the number of input fields.. can't i do anything with that? Link to comment https://forums.phpfreaks.com/topic/107623-solved-gather-fields-value/#findComment-552384 Share on other sites More sharing options...
rhodesa Posted May 29, 2008 Share Posted May 29, 2008 um...ok...i don't see why you can't alter the javascript...but here is an example of how to loop over variables with an increasing numeric suffix...does that help? for($n=1;isset(${'foobar'.$n});$n++){ $value = ${'foobar'.$n}; echo $value; } Link to comment https://forums.phpfreaks.com/topic/107623-solved-gather-fields-value/#findComment-552472 Share on other sites More sharing options...
johnwayne77 Posted May 29, 2008 Author Share Posted May 29, 2008 hold on, i've tried to alter the java and it works... it echoes all inputed fields. my bad i'll try to figure out how to insert the values into the mysql and will get back cheers Link to comment https://forums.phpfreaks.com/topic/107623-solved-gather-fields-value/#findComment-552486 Share on other sites More sharing options...
johnwayne77 Posted May 29, 2008 Author Share Posted May 29, 2008 here is my idea: i have this line: foreach(array_keys($_POST['field1']) as $n){ print $_POST['field1'][$n] . '|' . $_POST['field2'][$n] . '|' . $_POST['field3'][$n] . '<br>'; which prints me each row, one under another. what if i would insert all rows (delimited by a <br> as above) in one single variable, which i would insert into the mysql and when i want to show the result, i just echo this variable and all rows will show. eh? Link to comment https://forums.phpfreaks.com/topic/107623-solved-gather-fields-value/#findComment-552507 Share on other sites More sharing options...
rhodesa Posted May 29, 2008 Share Posted May 29, 2008 You could do that: <?php $data = array(); foreach(array_keys($_POST['field1']) as $n) $data[] = $_POST['field1'][$n] . '|' . $_POST['field2'][$n] . '|' . $_POST['field3'][$n]; $value = mysql_real_escape_string(implode("\n",$data)); //Use a new line...you can always convert it to a BR later with nl2br() mysql_query("INSERT INTO tablename (fieldname) VALUES ('{$value}')"); ?> Link to comment https://forums.phpfreaks.com/topic/107623-solved-gather-fields-value/#findComment-552510 Share on other sites More sharing options...
johnwayne77 Posted May 29, 2008 Author Share Posted May 29, 2008 great dude, that works! thanks a bunch! Link to comment https://forums.phpfreaks.com/topic/107623-solved-gather-fields-value/#findComment-552526 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.