0

I have n number of text box (n may be any number) with same name. And I want to access the value of all the text box of that name.

Ex-:

<form method="post" id="create">
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="button" id="newFieldBtn"/>
<input type="submit" name="save" value="save"/>
</form>

jQuery

<script>
jQuery(document).ready(function($) {
        $('#newFieldBtn').click(function(){
var code='<input type="text" name="user[]" />';
jQuery('#create').append(code);

</script>

or is there any other way to access the value of the text box. Either by class or any other property..

6
  • how you are generation your textboxes? I mean by using php or javascript/jquery? you can use count .... Commented Mar 25, 2014 at 9:53
  • Since you are asking about accessing the send value in PHP – no, apart from the value the name is the only info you get send; classes or anything else client-side do not come into play. But what is your question anyway, where is the problem in accessing the values? Commented Mar 25, 2014 at 9:54
  • I am using jQuery for add new text box Commented Mar 25, 2014 at 9:54
  • Now see my code @ Nishant Solanki Commented Mar 25, 2014 at 10:00
  • $_POST['user'] return only "Array", i Can't Get the value Of user[0],user[1]...etc Commented Mar 25, 2014 at 10:03

2 Answers 2

2
<script>

    jQuery(document).ready(function($) {
    $('#newFieldBtn').click(function(){
    var count = document.getElementById('count').value;
    count++;
    var code = '<input type="text" name="user'+count+'" />';
            jQuery('#create').append(code);
            document.getElementById('count').value = count;
</script>

and your html like...

<form method="post" id="create">
    <input type="hidden" id="count" value="0" name="count">
    <input type="button" id="newFieldBtn"/>
    <input type="submit" name="save" value="save"/>
</form>

and in your php code...

<?php

if(isset($_POST))
{
    $count = $_POST['count'];
    for($i=1;$i<=$count;$i++)
    {
        $user.$i = $_POST['user'.$i];
    }
}

?>
Sign up to request clarification or add additional context in comments.

Comments

1

Try this one, it will show you the values :

<form action="#" method="post">
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="text" name="user[]" />
 <input type="submit" value="submit" >
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{ 
 foreach($_POST['user'] as $key => $value)
{
  echo $key." has the value = ". $value."<br>";
}
}
?>

See this in action: http://wistudat.be/try/array.php

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.