...

View Full Version : need help in php forms



doforumda
10-24-2009, 11:16 AM
hi i want to make a form. the form will have three text fields and with two buttons. what i want with this form is when the user enters the first three fields and if he wants to add more then he will click on add more button and on the same page three more fields will appear below the first three fields. the user will then enter these three fields then if he wants to add more then he will click on add more button. so three more fields will appear below the first six fields. user will enter these fields. he will be allowed to enter upto eight or less than eight times. once he finishes with this then he will click the second button to insert this data into db.
i create the following form but it only works just for two times. how can i achieve this?

my code is here

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
<form name="form1" method="post" action="cvEducation.php">
<label>Degree:
<input type="text" name="degree1" id="degree1">
</label>
<p>
<label>CGPA/Grade:
<input type="text" name="cgpa1" id="cgpa1">
</label>
</p>
<p>
<label>Institute:
<input type="text" name="institute1" id="institute1">
</label>
</p>
<p>
<label>
<?php if(!isset($_POST['return'])) { ?>
<input type="submit" name="return" id="return" value="Save And Add Another">
<?php } ?>
</label>
<?php
if(isset($_POST['return']))
{ ?>
<label>Degree:
<input type="text" name="degree2" id="degree2">
</label>
<p>
<label>CGPA/Grade:
<input type="text" name="cgpa2" id="cgpa2">
</label>
</p>
<p>
<label>Institute:
<input type="text" name="institute2" id="institute2">
</label>
</p>
<p>
<label>
<input type="submit" name="return" id="return" value="Add More">
</label>
</p>
<?php }
?>
</p>
</form>
</body>
</html>


in this form i provide only one button but it will be two once this problem is solved.

met
10-24-2009, 03:04 PM
the reason it only works once is because your code is simply displaying those three boxes everytime the button is clicked. you need to code a way to handle if the button is submitted more than once, otherwise it wil just repeat itself, hence you only see the same 3 every time.

this might be what you are after
http://www.dustindiaz.com/add-and-remove-html-elements-dynamically-with-javascript/

this kind of effect can be achieved wtih PHP but its probably easier to achieve with JS/ajax or similar.

you could put the extra fields in to several <divs> with their display:hidden property, then if an add-more is clicked, change it to display:visible

l googled dynamically adding form fields, there's a tonne of stuff available


edit
unless I misunderstand and you just want a way for the user to add another record after they submit the form.. in that case



if(isset($_POST['return'])) {

// process form

}

// display form

/* on the same page, it will insert the record to DB
and provide the form again on teh same page, allowing you to add another record. */



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum