1

i make a dynamic textbox through javascript now i want to post the data of all dynamically generated textboxes to php script and then insert into a table. how can i do this...


<head>
<title>Dynamic Form</title>
<script language="javascript">
function changeIt()
{
var i = 1;
my_div.innerHTML = my_div.innerHTML +"<br><input type='text' name='mytext'+ i>"
}
</script>
</head>
<body>
<form name="form" action="post" method="">
<input type="button" value="add another textbox" onClick="changeIt()">
<div id="my_div">
<input type="submit" value="save">
</body>

2 Answers 2

1

HI, whenever you post your form at that time all the fields are posted on the php script, where you can get array of that textbox variable by using for loop.

ex: 
    $textboxarr = array();
    for($i = 0;$i<count($_POST['mytext']);$i++){
             $textboxarr = $_POST['mytext'][$i];
    }

now you having your all text boxes in the $textboxarr.try this.

Thanks.

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

Comments

1

Use it like this

<head>
<title>Dynamic Form</title>
<script language="javascript">
function changeIt()
{
my_div.innerHTML = my_div.innerHTML +"&ltbr><input type='text' name='mytext[]'>"
}
</script>
</head>
<body>
<form name="form" action="post" method="">
<input type="button" value="add another textbox" onClick="changeIt()">
<div id="my_div">
<input type="submit" value="save">
</body>

In php you will be able to use it as an array like

$_POST['mytext'][0], $_POST['mytext'][1] etc... or you can use a foreach loop for $_POST['mytext']

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.