Help to send jQuery cloned inputs to the database

Asked

Viewed 39 times

1

I have a form that will add links from download servers to a file, however each file may contain n links from download servers

In HTML:

<input type="text" name="server_down">

This input is cloned by a jQuery so that the user can add other server options if the file has more than one distribution server, so I did something like this:

<input type="text" name="server_down_1">
<input type="text" name="server_down_2">
<input type="text" name="server_down_n">

Whenever the user clones the input, the last value adds up to 1. However, to send to the database I need to make a loop based on the amount of elements I cloned, and for this I made an auxiliary input as follows

<input type="hidden" name="field_clonada" value="Quantas vezes foi clonada">

This way I can save in the database without problem, but what I would like to know is if there are more "efficient" ways to do this, or what are the other ways to do it?

The PHP Loop I use to process the data is this:

<?php $count = $_POST['field_clonada'];
for ($i=1; $i <= $count; $i++) {
   salva_no_bd($arq_id, 'server_down_'.$i, $_POST['server_down_'.$i]);
}
  • Just do name="server_down[]", with brackets and in PHP do $_POST['server_dow'] to obtain the array.

1 answer

1


Instead of listing the name, which is more complicated, you can use the name as an array adding square brackets:

name="server_down[]"

The values will be sent to PHP in array form, where you can make a foreach to take the values:

$server_down = $_POST['server_down'];
foreach($server_down as $key => $i){
   salva_no_bd($arq_id, 'server_down_'.$key, $i);
}

The $i is the value of each field in the array. The $key returns the index of the items in the array, starting from 0.

See if that’s what it was, because I didn’t quite understand the 'server_down_'.$count, where you concatenate the amount of the items. If that’s what you want, change to:

$server_down = $_POST['server_down'];
$count = sizeof($server_down);
foreach($server_down as $key => $i){
   salva_no_bd($arq_id, 'server_down_'.$count, $i);
}

The sizeof($server_down) returns the number of elements in the array.

  • Ops, I apologize for my mistake, I was writing the sample code and I ended up being wrong, I saw your first answer and it’s exactly perfect for the question I asked, but since it was a mistake and you quickly noticed and corrected it, and now in addition to correcting me, is the correct solution to the question.

  • I would still recommend uniting these values in a single SQL and sending everything at once to the database, thus running only 1 time and not N times.

  • @Andersoncarloswoss Could you exemplify this method? Would it be using the data serialization method? serialize() ?

  • 1

    @Heathcliff Instead of running INSERT INTO ... VALUES (...) Sometimes, you can do INSERT INTO ... VALUES (...), (...), (...), ..., (...) already sending all values.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.