Jump to content

Run Time HTMLTextField Generation In Php Problem?


zer0_c0d3r

Recommended Posts

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>

 

 

Link to comment
Share on other sites

You should be aware of three things. If you write in a name to student 1 hit "ADD More" button add to student  2 and hit button again THE student  2 name disappears. And this happens every time you hit the add button. And second If you leave a student  field empty Your program keeps it an empty slot. so "student  3" out of five students could be blank. The last thing is t this lacks a focus. That's not mandatory but it sure would help.

 

For your problem change the Page3 Code to:

<!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 type="">
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();
$num = $_SESSION["count"];
echo	'<form action="page2.php" method="post" enctype="application/x-www-form-urlencoded">';
for($i=1;$i<=$num;$i++)
{
echo	'student '.$i.': <input type="text" name="val'.$i.'" id="val'.$i.'" value="'.$_SESSION['val'.$i].'"  /><br />';
}
?>
<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="<?php echo $num; ?>" />
</form>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.