Skip to content Skip to sidebar Skip to footer

Getting A Value From Dynamically Created Textbox Through Php

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-:

Solution 1:

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

?>

Solution 2:

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


Post a Comment for "Getting A Value From Dynamically Created Textbox Through Php"