You can do it in 2 simple ways (maybe there are even more, but I am not aware of it).
You can either save the data of the first form in session after it is posted, and then, when the second form is posted, get all the data (session data and the 2nd form's posted data) and insert a record to the table.
The second method is to have hidden fields in the 2nd form, which will save all the values of the first form and when the 2nd form is submitted, you will have the data of 1st and 2nd form :)
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
Or you could insert the data from the first page into the database as soon as it is submitted, have the user fill out the second part, and just update the row when the second part is submitted.
The only problem with this is , if a user loses his interest midway [ie., after submitting 1st form], there will be a obsolete record in the table. Or else, this is good.
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
This is a situation where ajax could help. You really don't need to have two pages. You could have just the one with a DIV that is updated to show different forms. This means you could have a number of different steps without changing the page.
diafol
Rhod Gilbert Fan (ardav)
7,765 posts since Oct 2006
Reputation Points: 1,168
Solved Threads: 1,075
I've never worked with Ajax at all.
Could you please tell me more about this method or maybe point me to some article that explains it?
Thank you!
Oh boy, where do I start?
First of all, most of the time Ajax is pretty much javascript calling a php script in order to change the contents of a tag. There's heck of a lot more to it, but that's the bit that usually interests people.
The main stumbling block for most is the Ajax object. You can bypass all this nonsense with a javascript library called prototype.js. You can get this fromprototypejs.org. I should point out that there are a number of other libraries you could use, including jQuery and Yahoo! I tend to use prototype due to its simplicity and lack of bloat.
Example implementation:
1. Download the file and place it in a folder (let's call it 'scripts')
2. Create a blank text file and save it as myajax.js into 'scripts'.
3. Put script references to these files in the head area of your page:
...(dtd)...
<html>
<head>
<script src="/scripts/prototype.js"></script>
<script src="/scripts/myajax.js"></script>
</head>
<body>
...other html...
<div id = "ajform" >
<form method="post" action="" onsubmit="doForm1();">
...form contents...
</form>
</div>
...other html...
</body>
</html>
The "myajax.js" file:
function doForm1(){
fname = $F('firstname');
sname = $F('surname');
email = $F('email');
//do some validation
//$F('..') is shorthand for the id of the form item - it gives the value
//if it passes the validation, do this:
var param = "action=form1&fn=" + fname + "&sn=" + sname + "&em=" + email;
var url = "/includes/myajaxform1.php";
var oAjax = new Ajax.Updater('ajform', url,{method: 'post',parameters: param});
}
The "includes/myajaxform1.php" file:
<?php
switch($_POST['action']){
case "form1":
$fname = mysql_real_escape_string($_POST['fn']);
$sname = mysql_real_escape_string($_POST['sn']);
$email = mysql_real_escape_string($_POST['em']);
//validate the form data again
//if ok - add to DB or whatever and echo the next form
//anything you echo out here will be displayed in the DIV
break;
...etc...
}
?>
I've simplified the code above to give you an idea of how it works. It could be further optimized, but would be very confusing. TheAjax.Updater is the killer prototype function here. It just updates the div with whatever you output from the php file.
diafol
Rhod Gilbert Fan (ardav)
7,765 posts since Oct 2006
Reputation Points: 1,168
Solved Threads: 1,075