Insert input value into array

Asked

Viewed 2,071 times

1

How to store the values of an input at array positions? I would like to take the value that was typed in the field and store it in an array, where I need to sort the values that were added in the array and sort them down.

Ex:

<?PHP
echo"<tr>
   <td>
     <input type="text" name="campos" value="">
   </td>
   <td>
      <input type="submit" value="Submit">
   </td>
</tr>";
?>
  • You’re generating several inputs on the PHP server is that it? and then you want Javascript to save the values entered in an array right? and then you want to use the array in Javascript or PHP?

  • I’m actually generating a single input in PHP, where I want to take the values inserted in it and store it in an array in PHP! I ended up not creating several inputs, because I’m seeing a way to use a single input to input the data!

1 answer

7


Just put the name of the field with keys at the end.nomedocampo[]. Hence assuming this is a form, when you send with a post to the backend.

backend will understand that this set of fields with the same name is an array and will transform it into a single object $_POST['nomedocampo']

<input type="text" name="campos[]" value="">

<input type="text" name="campos[]" value="">

<input type="text" name="campos[]" value="">


<input type="submit" value="Submit">

  • That’s just what I needed!

Browser other questions tagged

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