Hi guys i am facing a problem in my php code and i dont know how to figure out so i search on google and i have started searching for a php forums and i found this community.
The problem is i made three pages in page1 i create a simple form with one text field and add two buttons one name is "add more fields" and one is "submit" when i click on add more fields button a text field is generate by using javascript functions. Let say i create 3 fields and enter names and submit the form by having form action "page2"
In page2 i just simply get all values i entered in fields and print them on screen and also there is a link of "page3" that says edit your information and the session is start. In "page3" i made changes to values and submit them again again. Its all working. But i i also want "add more fields" button in page3 so that i can generate more fields also on page3 and this this the point where i stucked and i didn't figure out how can i do this task to create add more fields button.
Can you guys help me in this regard thanks.
Page1 Code:
<!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>
<script>
function addonemore()
{
var tot = document.getElementById('count').value;
tot = parseInt(tot)+1;
document.getElementById('count').value=tot;
var newhtml = 'student '+tot+' <input type="text" name="val'+tot+'" id="val'+tot+'" /><br/>';
document.getElementById('mydiv').innerHTML += newhtml;
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="page2.php" method="post" enctype="application/x-www-form-urlencoded">
student 1 <input type="text" name="val1" id="val1" />
<div id="mydiv"></div>
<input type="button" id="addnew" name="addnew" value="Add more" onclick="addonemore()" />
<input type="submit" id="submit" name="submit" value="submit" />
<input type="hidden" name="count" id="count" value="1" />
</form>
</body>
</html>
Page2 Code:
<?php
session_start();
$_SESSION['count'] = $_REQUEST['count'];
for($i=1;$i<=($_REQUEST['count']);$i++)
{
$html .= 'student '.$i.':'.$_REQUEST['val'.$i].'<br/>';
$_SESSION['val'.$i] = $_REQUEST['val'.$i];
}
echo $html;
echo "<br/><a href='page3.php'>click here to edit</a>";
?>
Page3 Code:
<html>
<head>
<script>
function addonemore()
{
var tot = document.getElementById('count').value;
tot = parseInt(tot)+1;
document.getElementById('count').value=tot;
var newhtml = 'student '+tot+' <input type="text" name="val'+tot+'" id="val'+tot+'" /><br/>';
document.getElementById('mydiv').innerHTML += newhtml;
}
</script>
</head>
<body>
<?php
session_start();
$html = '<form action="page2.php" method="post" enctype="application/x-www-form-urlencoded">';
for($i=1;$i<=($_SESSION['count']);$i++)
{
$html .= 'student '.$i.':<input type="text" name="val'.$i.'" id="val'.$i.'" value="'.$_SESSION['val'.$i].'" />';
}
$html .='<input type="submit" id="submit" name="submit" value="submit" /><input type="hidden" name="count" id="count" value="'.$_SESSION['count'].'" /></form>';
echo $html;
?>
<?php
$html = '<form action="" method="post" enctype="application/x-www-form-urlencoded">';
$html .= '<input type="button" id="addnew" name="addnew" value="Add more" onclick="addonemore()" /></form>'
?>
</body>
</html>