View Full Version : need help with dynamic form

02-18-2010, 03:42 AM

i create a form using jquery. i actually found jquery code from other website and modified it according to my needs. i need help in how can i get data in side all these fields to php file. on php side i am just echoing this data.

html form

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {

$('#btnAdd').click(function() {

var num = $('.clonedInput').length;

var newNum = new Number(num + 1);

var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
$('#input' + num).after(newElem);

#myForm div {
margin-bottom: 10px;
clear: left;
#myForm label {
width: 125px;
display: block;
font-weight: bold;
color: #999;
float: left;
#btnAdd {



<form id="myForm" method="post" action="process.php">
<label>Firstname: </label><input type="text" name="fname" id="fname"><br>
<label>lastname: </label><input type="text" name="lname" id="lname"><br>
<div id="input1" class="clonedInput">
<label>IM Screen Names:</label>
<input type="text" name="name1" id="name1" />
<select name="screenname1" id="screenname1">
<option value="AIM" selected="selected">AIM</option>
<option value="gtalk">Google Talk</option>
<option value="skype">Skype</option>
<option value="windows live">Windows Live</option>
<option value="yahoo">Yahoo</option>
</select><br />

<a href="#" id="btnAdd">Add another</a>
<input type="submit" name="submit" value="Submit">



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$name1 = $_POST['name1'];
$screenname1 = $_POST['screenname1'];

echo $fname."<br>";
echo $lname."<br>";
echo $name1."<br>";
echo $screenname1."<br>";


02-18-2010, 04:15 AM
Are you asking how to send the form data to the PHP script via Ajax?

02-18-2010, 04:54 AM
Are you asking how to send the form data to the PHP script via Ajax?

i am not using ajax. just normal post method

02-18-2010, 01:29 PM
Looks to me like the field will be numbered sequentially so you would need to check

$name2 = $_POST['name2'];
$screenname2 = $_POST['screenname2'];
$name3 = $_POST['name3'];
$screenname3 = $_POST['screenname3'];

or more realistically as there could be unlimited presumably load them into an array in a loop, something like:

$intField = 1;
$arrNames = array();
$arrScreenNames = array();
while(isset($_POST['name'.$intField])) {
$arrNames[] = $_POST['name'.$intField];
$arrScreenNames[] = $_POST['screenname'.$intField];