Forum Moderators: coopster

Message Too Old, No Replies

Session?

PHP sessions

         

cputek2k

8:57 pm on May 25, 2005 (gmt 0)

10+ Year Member



Not sure if this is what I need to use or not...

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.

mcibor

10:16 pm on May 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is it exactly? You have two forms on one page:

<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>';
}

if it's the second possibility, then why do you want to pass the values as it is?
don't ou want all the data stored?
If the latter you should use input hidden:
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

cputek2k

12:13 pm on May 26, 2005 (gmt 0)

10+ Year Member



A main page calls two seperate forms using "include". FormA.php and FormB.php. These forms work fine independantly.

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";
}

mcibor

12:42 pm on May 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since some version you should use $_POST to get the desired effect.

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

cputek2k

2:18 pm on May 26, 2005 (gmt 0)

10+ Year Member



That has solved one problem and created another. If the user fills in one form then submits, the form data is email as it should be.

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?

mcibor

5:52 pm on May 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whoa!
I don't really follow. What to you want the first and second form for? Why two forms?

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

cputek2k

6:40 pm on May 26, 2005 (gmt 0)

10+ Year Member



I will try to clarify:

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....

mcibor

7:50 pm on May 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, what you can do is send an email after the second form was done correctly. Moreover include javascript validation and php validation.

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>

you can use javascript onclick to submit the chosen option

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