Forum Moderators: coopster
I have two forms that are being called to one page. I have named the forms and created seperate submit buttons. One form works fine, but the other does not get the variables passed to it... Not sure why or what the solution is. I am calling the variables the same on both forms but still not working..
Any tips... I will post code if requested.
<body><form name="f1" method="POST"><input type="submit"></form>
<form name="f2" method="POST"><input type="submit"></form></body>
or you have one form after another:
echo '<body>';
if(!$_POST)
{
echo '<form name="f1" method="POST"><input type="submit"></form>';
}else{
echo '<form name="f2" method="POST"><input type="submit"></form>';
}
echo '<body>';
if(!$_POST)
{
echo '<form name="f1" method="POST"><input type="text" name="a"><input type="submit"></form>';
}else{
echo '<form name="f2" method="POST"><input type="text" name="b"><input type="hidden" name="a" value="'.$_POST["a"].'"><input type="submit"></form>';
}
I don't know what is the problem, therefore different approaches
If this doesn't help, then specify more the problem, but post here only relevant code (not whole)
Best regards
Michal Cibor
FormA (being called first) works no matter what.
FormB does NOT work when it is called after formA.
I am passing the variable using "hidden" to both forms. Looking at the source code after the page has been parsed, the variables are only in the formA and not FormB.
Here is the code for formA.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if ($CEmail == "Add"){
/* --------------------- Declare Variable(s) --------------------- */
$name; // Represents the name of the elements in $HTTP_POST_VARS
$value;// Represents the value of the elements in $HTTP_POST_VARS
// Do while there is an element in $HTTP_POST_VARS...
while (list($name, $value) = each($HTTP_POST_VARS))
/* ...get the name and value of the element and use it to construct a hidden input field.
Then go back to the beginning of the loop and do the same thing with the next element. */
{
echo "<input type=\"hidden\" value=\"$value\" name=\"$name\">\n";
}
/*------------------------- End generate hidden fields ---------------*/
AddEmail();
}
else if ($CEmail == "Change"){
/* --------------------- Declare Variable(s) --------------------- */
$name; // Represents the name of the elements in $HTTP_POST_VARS
$value;// Represents the value of the elements in $HTTP_POST_VARS
// Do while there is an element in $HTTP_POST_VARS...
while (list($name, $value) = each($HTTP_POST_VARS))
/* ...get the name and value of the element and use it to construct a hidden input field.
Then go back to the beginning of the loop and do the same thing with the next element. */
{
echo "<input type=\"hidden\" value=\"$value\" name=\"$name\">\n";
}
/*------------------------- End generate hidden fields ---------------*/
ChangeEmail();
}
else if ($CEmail == "Delete"){
/* --------------------- Declare Variable(s) --------------------- */
$name; // Represents the name of the elements in $HTTP_POST_VARS
$value;// Represents the value of the elements in $HTTP_POST_VARS
// Do while there is an element in $HTTP_POST_VARS...
while (list($name, $value) = each($HTTP_POST_VARS))
/* ...get the name and value of the element and use it to construct a hidden input field.
Then go back to the beginning of the loop and do the same thing with the next element. */
{
echo "<input type=\"hidden\" value=\"$value\" name=\"$name\">\n";
}
/*------------------------- End generate hidden fields ---------------*/
DeleteEmail();
}
else if (!$CEmail) {
echo "No Email Changes Necessary";
}
Function AddEmail(){
global $LastName, $FirstName, $Submitter, $Email, $HelpdeskEmail;
if (empty($LastName)) {
echo " Form Data goes here";
}
else {
AddEmailVerify();
}
}
Function AddEmailVerify(){
global $FirstName, $LastName, $Submitter, $Email, $ip, $CurrentDate, $HelpdeskEmail;
// Remove white spaces and leading space from data entry
$Group=trim($Group);
$FirstName=trim($FirstName);
$Supervisor=trim($Supervisor);
$Submitter=trim($Submitter);
$Email=trim($Email);
$Submitter-trim($Submitter);
//Verify that the form has been filled out.
if {
Verify form is filled out script.
}
else {
echo "Submission Verification";
}
// Send Message
$to = ("$Email, $HelpdeskEmail");
//$headers = "MIME-Version: 1.0\r\n";
//$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers = "From: $Email\n";
$subject = "Add Email Address";
$message = "--------Employee Affected--------\n"
."First Name: ".$FirstName."\n"
."Last Name: ".$LastName."\n"
."\n\n--------Submitter Information--------\n"
."Submitter: ".$Submitter."\n"
."Email: ".$Email."\n\n\n"
."\n\n--------Stats--------\n"
."Sent from ".$ip."\n"
."Current Date & Time: ".$CurrentDate."\n";
//In case any of our lines are larger than 70 characters, we should use wordwrap()
//$message = wordwrap($message, 70);
// Send
mail($to, $subject, $message, $headers);
echo "Message Sent";
}
Try doing:
foreach($_POST as $name=> $value)
{
echo "<input type=\"hidden\" name=\"$name\" value=\"$value\">";
}
Or see if in the source code there are desired values. It may be the problem with passing the vars from one to another.
BTW Don't use globals, as you may get confused. Use reference:
function ref($normal, $reference)
{
$normal = 2*$normal;
$reference = $normal + $reference;return $normal;
}$a = 2; $b = 3; //a = 2, b = 3
$a = ref($a, $b);//a = 4, b = 3 - no reference
$a = ref($a, &$b);//a = 8, b = 8 + 3 = 11 - with reference, value altered
Hope this helps
Michal Cibor
Then the user fills in the second form and submits, it sends both forms. Duplicating the first forms data.
The question is, Is there a way to destroy the data from the first form? I have tried using the unset($variable). That doesn't seem to do the trick. Maybe it is my placement of unset that isn't working, but I don't understand where it should go?
If you don't want the variables from first send by second form, just don't put them in hidden fields.
$dont = array("id", "email");// example of inputs you don't want to pass from first form
foreach($_POST as $name=> $value)
{
if(!in_array($name, $dont)) echo "<input type=\"hidden\" name=\"$name\" value=\"$value\">";
}
or if it's easier you can print out not the whole array POST, but only some elements:
<input type="hidden" name="id" value="<?php echo $_POST["id"];?>">
etc
Hope this shows you what to do
Best regards
Michal Cibor
The main page give the user options of what they need to do. Each option has a form attached. If they need to do multiple things (multiple forms) they select the options and all the forms are "included".
I have to make this idiot proof. So, if the user doesn't fill out all the forms at once or incorrectly fills one or the other out, I don't get duplicate emails from the form that was done correctly.
Make more sense now? If there is a better way, I am all ears, well eyes....
You can do eg:
1st_form.php:
<body><form action="2nd_form.php" method="POST"><select name="select">
<?php
$option = array("option1"=>"First option", "option2"=>"Second option");
foreach($option as $name=>$value)
{
echo "<option value=\"$name\">$value</option>";
}?>
<input type="submit"></form></body>
2nd_form.php:
<?php
if(!$_POST["select"]) header("Location: 1st_form.php");//This you will have to correct, as I don't recall how to do redirect
else
{
if($_POST["select"]=="option1") do_stuff_for_option_one();
elseif($_POST["select"]=="option2") do_stuff_for_option_two();
else redirect
}
?>
This way you are sure which is chosen, or at least you can store that select in second form hidden input
Michal Cibor