Jump to content

[SOLVED] gather fields value


johnwayne77

Recommended Posts

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

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.

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

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?

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;
}

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?

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}')");
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.