...

View Full Version : need help with dynamic form



doforumda
02-18-2010, 03:42 AM
hi

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"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<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);
});

});
</script>
<style>
#myForm div {
font-size:14px;
margin-bottom: 10px;
clear: left;
}
#myForm label {
width: 125px;
display: block;
font-size:14px;
font-weight: bold;
color: #999;
float: left;
}
#btnAdd {
margin-left:130px;
}
</style>

</head>

<body>

<form id="myForm" method="post" action="process.php">
<div>
<label>Firstname: </label><input type="text" name="fname" id="fname"><br>
</div>
<div>
<label>lastname: </label><input type="text" name="lname" id="lname"><br>
</div>
<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 />
</div>

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

</body>
</html>

process.php

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$name1 = $_POST['name1'];
$screenname1 = $_POST['screenname1'];

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

?>
</body>
</html>

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

doforumda
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

DaiWelsh
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];
$intField++;
}


HTH,

Dai



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum