1

What I'm trying to do:

  1. We have one input on the page
  2. We press "add" button and one more input adds after first
  3. On submit we get all their values and add them into an array.

How to add additional inputs on page (with jquery), and then get their contents in php?

1
  • Define "inputs" please. It's not clear what an "input" represents Commented Sep 12, 2010 at 12:42

3 Answers 3

5

You don't need to add the inputs into an array explicitly. If you name the input fields correctly, PHP will automatically parse them into an array.

HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>jQuery dynamic input sample</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">

    // Input adding function
    function addInput() {
        $('#inputs').append('<p><input type="text" name="input[]"></p>');
    }

    // Event handler and the first input
    $(document).ready(function () {
        $('#adder').click(addInput);
        addInput();
    });

</script>

  </head>
  <body>
      <form id="form" method="post" action="jquery.php">
          <a id="adder" href="#">Add Input</a>
          <div id="inputs">
          </div>
          <input type="submit" />
      </form>
  </body>
</html>

PHP

foreach ($_POST['input'] as $key => $value) {
    echo "$key $value";
}
Sign up to request clarification or add additional context in comments.

Comments

3

Maybe this will work (if I understand question correctly)

$('#add').click(function(){
  $(this).before('<input name="array[]"/>');
});

and i php

<?php
if (isset($_GET['array'])) {
  for ($i=0; $i<count($_GET['array']); $i++) {
    echo "array[$i] -> {$_GET['array'][$i]}";
  }
}
?>

2 Comments

name[] - this means an array of inputs?
Yes that is a way it work, you can see in php manual: php.net/manual/en/language.variables.external.php
1

I have posted this for a similar question, although this one deals with only one input that's being cloned, not multiple ones. So basically, you HTML would be something like:

<form id="my_form">
    <input name="my_input[1]" id="my_input[1]">
</form>
<a href="#" id="add_input">Add one more</a>

And the jQuery for it would look like this:

$("#add_input").click(function(event) {
    // Get the number of current inputs and set the new count ...
    var inputCount = parseInt($("input[id^=my_input]").size());
    var newInputCount = inputCount++;

    // ... then create new clone based on the first input ...
    var newInput = $("#my_input[1]").clone();

    // .. and do the cleanup, make sure it has
    // an unique ID and name for server-side parsing
    newInput.attr('id', 'my_input[' + newInputCount + ']').attr('name', 'my_input[' + newInputCount + ']').val('');

    // .. and finally insert it after the last fieldset
    newInput.insertAfter("#my_input[" + inputCount + "]");
    event.preventDefault();
});

Then, on the PHP side, $_POST['my_input'] would be an array of all the added fields' values, and it's easy to iterate through them with a foreach(), for example.

Hope this helps !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.