Click to See Complete Forum and Search --> : [RESOLVED] needs direction on how to loop a form
dataline
07-09-2008, 12:37 PM
Hi,
I'm learning from a book and building my pages as I go along. Newbie question and not sure if it falls under HTML or PHP. I have a form where user input is captured; it also has button. I have the following code:
<form action="ProcessForm.php" method=post>
html and php code
<input type="submit" value="submit">
</form>
when the user clicks on "submit", "ProcessForm.php" is executed, and the input is saved in a DB. All is fine. Now, instead of 1 "submit" button, I need to have 2: "submit and continue", "submit and end". I need the following functionality:
a) when the user clicks "submit and continue", the data is saved in an array and the form appears again for next round of input.
b) when the user clisks "submit and end", the data is saved in an array and all data from the array is saved to a DB.
My problem is how do I do (a) above: the looping part where the form reappears. Any direction would be much appreciated. thanks.
DL
mitya
07-09-2008, 05:17 PM
Your best bet is to use JS to determine whether or not the user wishes to save or continue. If continuing, you've got 3 options for storing vars across page transitions:
1) in the URL as a var (not very secure)
2) in a post var (requires string handling later)
3) in a session var (best option - we can store it as an array throughout).
See below. The method I've used to determine whether the user wishes to save or contiue, i.e. by writing to a hidden field which is read by the PHP on the handler page, is one of many methods. You could also alter the form's action ("?saveOrcont=s" etc) or whatever.
======================
<?php
session_start();
?>
<script>
function formSubmit(finished) {
formObj = document.forms['myForm'];
if (finished == true)
document.getElementById('finished').value = "yes";
if (document.getElementById('myField').value != '') formObj.submit();
}
</script>
<?php
if (array_key_exists("myField", $_POST)) {
$_SESSION['savedValues'][] = $_POST['myField'];
if ($_POST['finished'] == "yes") {
//save all to DB
for ($h=0; $h<count($_SESSION['savedValues']); $h++) {
//insert into db
}
die();
}
}
?>
<form action="<?php echo $PHP_SELF; ?>" method=post id=myForm onSubmit="return false">
<input type=text name=myField id=myField size=30>
<input type="submit" value="continue" onClick="formSubmit();">
<input type="submit" value="save" onClick="formSubmit(true);">
<input type=hidden id=finished name=finished>
</form>
dataline
07-10-2008, 08:55 AM
Hi Mitya,
Thanks for the reply and the tip. I'm learning JS as well, and will try to incorporate your idea.
Thanks again.
DL
Never rely on JavaScript!
The clicked button name/value pair will be passed to the script; check it's post values.<?php
foreach ( $_POST as $key => $value ) {
print $key . " = " . $value . "<br>";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>submit</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<form action="<?php echo $PHP_SELF; ?>" method="post">
<div>
<input type="text" name="data">
<input type="submit" name="continue" value="continue">
<input type="submit" name="save" value="save">
</div>
</form>
</body>
</html>
dataline
07-15-2008, 02:49 PM
let's say I have the following:
<html>
<body>
<form action="add.php" method="post">
First Name : <input type="text" name="firstname" size="40" length="40" value="First Name"><BR>
Surname : <input type="text" name="surname" size="40" length="40" value="Surname"><BR>
Email Address : <input type="text" name="emailaddress" size="40" length="40" value="Email Address"><BR>
<input type="submit" name="submit" value="Submit">
<input type="reset" name="reset" value="Clear It">
</form>
</body>
</html>
I have written "add.php" where the 3 pieces of data are stored in the database.
I now want to change it so that I have 2 buttons:
<input type="submit" name="submit" value="Next">
<input type="submit" name="submit" value="No more">
When the user clicks "Next", the 3 pieces of data are saved in an array and the form appears again. When the user clicks "No more", all data are saved to the DB.
It sounds simple, but I'm having such a hard time to code it.
Help anyone! Thanks.
dataline
07-15-2008, 03:21 PM
never mind. I've figured it out.