Forum Moderators: coopster

Message Too Old, No Replies

Unset Question

Unset Question

         

cputek2k

12:47 pm on May 24, 2005 (gmt 0)

10+ Year Member



I am trying to build a page with two seperate forms. If the user fills in one form but not the other, I only want that form to be submitted once. Currently, if the user fills in one form, then fills in the second form and submits, BOTH forms are submitted, causing a duplicate message to be sent.

Any ideas on how I can circumvent this?

Blackie

1:20 pm on May 24, 2005 (gmt 0)

10+ Year Member



Alt 1.
Use different <form> with two separate submit buttons.

Alt 2.
Give different names to the variables like
name_form1
name_form2

so that you can check if the user filled out first form or second or both.

cputek2k

1:22 pm on May 24, 2005 (gmt 0)

10+ Year Member



I tried the nameing thing.. that didn't work... I tried the seperate submit buttons and that makes both forms disappear...

Stormfx

1:23 pm on May 24, 2005 (gmt 0)

10+ Year Member



Another option is create a hidden field for each form that identifies the form that's submitted.

Blackie

1:27 pm on May 24, 2005 (gmt 0)

10+ Year Member



If you present the code here with "naming thing" we can try finding a mistake there :-)

P.S. the same with different forms

cputek2k

1:39 pm on May 24, 2005 (gmt 0)

10+ Year Member



Here is the "email.php" page... there are 3 pages total. I will post a seperate reply with the "first page".

<chop>

[edited by: jatar_k at 4:24 pm (utc) on May 24, 2005]
[edit reason] waaaay too much code [/edit]

cputek2k

1:43 pm on May 24, 2005 (gmt 0)

10+ Year Member



<!---
Created by: Me
For: My Company
Date: 05/23/2005
Copyright 2005 All Rights Reserved
--->
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title></title>
</head>
<body bgcolor='aliceblue'>
<fieldset><legend><font color='green'><b>New Employee Setup Forms</b></font></legend>
<form >
<?

//Checking to see if the values are present

if (isset($CEmail)) {
echo "CEmail is set.<br>";
}
if (isset($Enrich)) {
echo "Enrich is set.<br>";
}
if ((!$Enrich) && (!$CEmail)) {
echo "
<table align='center'>
<th colspan='2'>Select your desired tasks.<hr color='green'></th>
<tr>
</tr>
<tr>
<td align='right'><strong>Email : </strong></td>
<td>
<input type='radio' name='CEmail' value='Add'>Add,
<input type='radio' name='CEmail' value='Change'>Change,
<input type='radio' name='CEmail' value='Delete'>Delete</td>
</tr>
<tr>
<td align='right'><strong>Enrich : </strong></td>
<td>
<input type='radio' name='Enrich' value='Add'>Add,
<input type='radio' name='Enrich' value='Change'>Change,
<input type='radio' name='Enrich' value='Delete'>Delete</td>
</tr>
<tr>
<td align='center' colspan='2'><input type='submit' value='Make Changes'></td>
</tr>
</table>";
unset($CEmail);
unset($Enrich);
}

else{

if ($CEmail == 'Add') {
echo "<input type='hidden' name='CEmail' value='$CEmail'>";
include 'email.php';
unset($CEmail);
}
else if ($CEmail == 'Change') {
echo "<input type='hidden' name='CEmail' value='$CEmail'>";
include 'email.php';
unset($CEmail);
}
else if ($CEmail == 'Delete') {
echo "<input type='hidden' name='CEmail' value='$CEmail'>";
include 'email.php';
unset($CEmail);
}
else if (empty($CEmail)) {
// Do nothing
}
if ($Enrich == 'Add') {
echo "<input type='hidden' name='Enrich' value='$Enrich'>";
include 'enrich.php';
unset($Enrich);
}
else if ($Enrich == 'Change') {
echo "<input type='hidden' name='Enrich' value='$Enrich'>";
include 'changeenrich.php';
unset($Enrich);
}
else if ($Enrich == 'Delete') {
echo "<input type='hidden' name='Enrich' value='$Enrich'>";
include 'deleteenrich.php';
unset($Enrich);
}
else if (empty($Enrich)) {
// Do nothing
}
else {
echo "Sorry, but I didn't understand your instructions";
}

//unset($CEmail);
//unset($Enrich);

//Checking to see if the unset works

if (isset($CEmail)) {
echo "CEmail is set set.<br>";
}
if (isset($Enrich)) {
echo "Enrich is set set.<br>";
}
if (!$CEmail){
echo "CEmail is not set.<br>";
}
if (!$Enrich){
echo "Enrich is not set.<br>";
}
echo "<input type='submit' value='Submit Data'>";
echo "</form>";
}


?>

</fieldset>
</body>
</html>

Stormfx

1:45 pm on May 24, 2005 (gmt 0)

10+ Year Member



(Blah, stupid edit time limits. Who comes up with this stuff? :p)

Another option is create a hidden field for each form that identifies the form that's submitted:

<input type="hidden" name="form" value="form1">

On naming, make sure your two forms have different names:

<form name="form1">
.....
</form>

<form name="form2">
...
</form>

And that each form has its own submit button.

Another... rather interesting way to do it might be use an array to store the information, and then use a hidden field to access it dynamically.

This should work:

HTML

<form name="form1" action="whatever.php" method="post">
<input type="text" name="form1[name]">
<input type="text" name="form1[email]">
<input type="hidden" name="form" value="form1">
<input type="submit" value="Submit">
</form>

<form name="form2" action="whatever.php" meothd="post">
<input type="text" name="form2[name]">
<input type="text" name="form2[email]">
<input type="hidden" name="form" value="form2">
<input type="submit" value="Submit">
</form>

PHP

<?php
// Access the form data dynamically by using the hidden field's value

$name = {$_POST['form']}['name'];
$email = {$_POST['form']}['email'];
?>

Note that I am at work, so cannot test it, but you see the idea.

cputek2k

2:39 pm on May 24, 2005 (gmt 0)

10+ Year Member



When I use form names and seperate submit buttons, both forms disappear if one or the other is filled out... There has to be away to unset or destroy the first form so that it doesn't resend...

Stormfx

3:00 am on May 25, 2005 (gmt 0)

10+ Year Member



From what I've seen of your code, I think you're trying to implement a single form. Correct me if I'm wrong.

In your example above, you call the same output 3 times for the CEmail section. For add, change or delete. However, you don't need to do that going by the code that's there. You could just check to see if it's empty or not and eliminate two if statements. Then you'd just modify your form action url a bit to accomodate the appropriate setting.

For example change:

if ($CEmail == 'Add') {
echo "<input type='hidden' name='CEmail' value='$CEmail'>";
include 'email.php';
unset($CEmail);
}
else if ($CEmail == 'Change') {
echo "<input type='hidden' name='CEmail' value='$CEmail'>";
include 'email.php';
unset($CEmail);
}
else if ($CEmail == 'Delete') {
echo "<input type='hidden' name='CEmail' value='$CEmail'>";
include 'email.php';
unset($CEmail);
}
else if (empty($CEmail)) {
// Do nothing
}

To:

if (!empty($CEmail)) {
echo "<input type='hidden' name='CEmail' value='$CEmail'>";
include 'email.php';
unset($CEmail);
}
else {
// Do nothing
}

I'm trying to help you as much as I can with the available code, but without the full script and knowing what's going on, it's hard to see exactly where your problems are. But judging from what you stated in your original post, I am guessing that the problem is that you're sharing the same form field names and it's buggering the script.

If you desire, you can sticky me code that I can copy and paste into some test pages that way I can see what's going on.

cputek2k

12:36 pm on May 25, 2005 (gmt 0)

10+ Year Member



The first page of code has been posted. Here is the "email.php" page...

[php]
<?
$HelpdeskEmail = "email@company.com";
$ip = $_SERVER['REMOTE_ADDR']; //Get IP Address
$CurrentDate=date("l dS of F Y h:i:s A"); //Set Current Date (Friday 25th of March 2005 09:46:28 AM)

if ($CEmail == "Add"){
echo "
<input type='hidden' name='FirstName' value='$FirstName'>
<input type='hidden' name='LastName' value='$LastName'>
<input type='hidden' name='Submitter' value='$Submitter'>
<input type='hidden' name='Email' value='$Email'>";
AddEmail();
}
else if ($CEmail == "Change"){
echo "
<input type='hidden' name='FirstName' value='$FirstName'>
<input type='hidden' name='LastName' value='$LastName'>
<input type='hidden' name='Submitter' value='$Submitter'>
<input type='hidden' name='Email' value='$Email'>";
ChangeEmail();
}
else if ($CEmail == "Delete"){
echo "
<input type='hidden' name='FirstName' value='$FirstName'>
<input type='hidden' name='LastName' value='$LastName'>
<input type='hidden' name='Submitter' value='$Submitter'>
<input type='hidden' name='Email' value='$Email'>";
DeleteEmail();
}
else if (!$CEmail) {
echo "No Email Changes Necessary";
}

Function AddEmail(){
global $LastName, $FirstName, $Submitter, $Email, $HelpdeskEmail;
if (empty($LastName)) {
echo "
<table align='center'>
<tr>
<th>You must complete the entire form.</th>
</tr>
<tr>
<td>First Name:</td>
<td><input type='text' name='FirstName' value='$FirstName'></td>
<td><font face='comic' color='red'><i>Required</i></font></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type='text' name='LastName' value='$LastName'></td>
<td><font face='comic' color='red'><i>Required</i></font></td>
</tr>
<tr>
<td>Submitted By:</td>
<td><input type='text' name='Submitter' value='$Submitter'></td>
<td><font face='comic' color='red'><i>Required</i></font></td>
</tr>
<tr>
<td>Your Email Address:</td>
<td><input type='text' name='Email' value='$Email'></td>
<td><font face='comic' color='red'><i>Required</i></font></td>
</tr>
<tr>
<td colspan='3' align='center'>$CurrentDate</td>
</tr>

</table>";
}
else {
AddEmailVerify();
}
}

Function ChangeEmail(){
global $LastName, $FirstName, $Submitter, $Email, $HelpdeskEmail;
if (empty($LastName)) {
echo "
<table align='center'>
<tr>
<th>You must complete the entire form.</th>
</tr>
<tr>
<td>First Name:</td>
<td><input type='text' name='FirstName' value='$FirstName'></td>
<td><font face='comic' color='red'><i>Required</i></font></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type='text' name='LastName' value='$LastName'></td>
<td><font face='comic' color='red'><i>Required</i></font></td>
</tr>
<tr>
<td>Submitted By:</td>
<td><input type='text' name='Submitter' value='$Submitter'></td>
<td><font face='comic' color='red'><i>Required</i></font></td>
</tr>
<tr>
<td>Your Email Address:</td>
<td><input type='text' name='Email' value='$Email'></td>
<td><font face='comic' color='red'><i>Required</i></font></td>
</tr>
<tr>
<td colspan='3' align='center'>$CurrentDate</td>
</tr>
<tr>
<td colspan='3' align='center'><input type='submit' value='Submit Data'>
<input type='reset' value='Reset'></td>
</tr>
</table>";
}
else {
ChangeEmailVerify();
}
}

Function DeleteEmail(){
global $LastName, $FirstName, $Submitter, $Email, $HelpdeskEmail;
if (empty($LastName)) {
echo "
<table align='center'>
<tr>
<th>You must complete the entire form.</th>
</tr>
<tr>
<td>First Name:</td>
<td><input type='text' name='FirstName' value='$FirstName'></td>
<td><font face='comic' color='red'><i>Required</i></font></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type='text' name='LastName' value='$FirstName'></td>
<td><font face='comic' color='red'><i>Required</i></font></td>
</tr>
<tr>
<td>Submitted By:</td>
<td><input type='text' name='Submitter' value='Submitter'></td>
<td><font face='comic' color='red'><i>Required</i></font></td>
</tr>
<tr>
<td>Your Email Address:</td>
<td><input type='text' name='Email' value='$Email'></td>
<td><font face='comic' color='red'><i>Required</i></font></td>
</tr>
<tr>
<td colspan='3' align='center'>$CurrentDate</td>
</tr>
<tr>
<td colspan='3' align='center'><input type='submit' value='Submit Data'>
<input type='reset' value='Reset'></td>
</tr>
</table>";
}
else {
DeleteEmailVerify();
}
}

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 ((!$FirstName) ¦¦ (!$LastName) ¦¦ (!$Submitter) ¦¦ (!$Email)) {
echo "<h1 align='center'>You did not submit the following required information! <br />
</h1><br />";
if(!$FirstName){
echo "<p>First Name is a required field. Please enter it now. <input type='text' name='FirstName'> </p>";
}
if(!$LastName){
echo "<p>Last Name is a required field. Please enter it now. <input type='text' name='LastName'> ></p>";
}
if(!$Submitter){
echo "<p>Your Name is required field. Please enter it now. <input type='text' name='Submitter'> </p>";
}
if(!$Email){
echo "<p>Your Email Address is required field. Please enter it now. <input type='text' name='Email'> </p>";
}
echo "<h1 align='center'><input type='submit' value='Update Data'></h1>";
}
else {
echo "
<table align='center'>
<tr>
<th colspan='2'>You have submitted the following information to the helpdesk.</th>
</tr>
<tr>
<td>First Name:</td>
<td><input type='text' name='FirstName' value='$FirstName'></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type='text' name='LastName' value='$LastName'></td>
</tr>
<tr>
<td colspan='2' align='center'>$CurrentDate</td>
</tr>
<tr>
<td colspan='2' align='center'>The IP address is <font color='red'><strong>$ip</strong></font></td>
</tr>
</table>";

// 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 "<table align='center'>
<tr>
<th>Message has been sent</th>
</tr>
</table>";
}
unset($CEmail);
unset($LastName);
unset($FirstName);
unset($Submitter);
unset($Email);
unset($HelpdeskEmail);
unset($to);
unset($subject);
unset($message);
unset($headers);
}

Function ChangeEmailVerify(){
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 ((!$FirstName) ¦¦ (!$LastName) ¦¦ (!$Submitter) ¦¦ (!$Email)) {
echo "<h1 align='center'>You did not submit the following required information! <br />
</h1><br />";
if(!$FirstName){
echo "<p>First Name is a required field. Please enter it now. <input type='text' name='FirstName'> </p>";
}
if(!$LastName){
echo "<p>Last Name is a required field. Please enter it now. <input type='text' name='LastName'> ></p>";
}
if(!$Submitter){
echo "<p>Your Name is required field. Please enter it now. <input type='text' name='Submitter'> </p>";
}
if(!$Email){
echo "<p>Your Email Address is required field. Please enter it now. <input type='text' name='Email'> </p>";
}
echo "<input type='submit' value='Update Data'>";
}
else {
echo "
<table align='center'>
<tr>
<th colspan='2'>You have submitted the following information to the helpdesk.</th>
</tr>
<tr>
<td>First Name:</td>
<td><input type='text' name='FirstName' value='$FirstName'></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type='text name='LastName' value='$LastName'></td>
</tr>
<tr>
<td colspan='2' align='center'>$CurrentDate</td>
</tr>
<tr>
<td colspan='2' align='center'>The IP address is <font color='red'><strong>$ip</strong></font></td>
</tr>
</table>";

// 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 = "Change Email Address";

$message = "--------Employee Affected--------\n"
."First Name: ".$FirstName."\n"
."Last Name: ".$LastName."\n"
."\n\n--------PHONE--------\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 "<table align='center'>
<tr>
<th>Message has been sent</th>
</tr>
</table>";
}
unset($CEmail);
unset($LastName);
unset($FirstName);
unset($Submitter);
unset($Email);
unset($HelpdeskEmail);
unset($to);
unset($subject);
unset($message);
unset($headers);
}
Function DeleteEmailVerify(){
global $EFirstName, $ELastName, $ESubmitter, $EEmail, $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 ((!$FirstName) ¦¦ (!$LastName) ¦¦ (!$Submitter) ¦¦ (!$Email)) {
echo "<h1 align='center'>You did not submit the following required information! <br />
</h1><br/>";
if(!$FirstName){
echo "<p>First Name is a required field. Please enter it now. <input type='text' name='FirstName'> </p>";
}
if(!$LastName){
echo "<p>Last Name is a required field. Please enter it now. <input type='text' name='LastName'> ></p>";
}
if(!$Submitter){
echo "<p>Your Name is required field. Please enter it now. <input type='text' name='Submitter'> </p>";
}
if(!$Email){
echo "<p>Your Email Address is required field. Please enter it now. <input type='text' name='Email'> </p>";
}
echo "<input type='submit' value='Update Data'>";
}
else {
echo "
<table align='center'>
<tr>
<th colspan='2'>You have submitted the following information to the helpdesk.</th>
</tr>
<tr>
<td>First Name:</td>
<td><input type='text' name='FirstName' value='$FirstName'></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type='text name='LastName' value='$LastName'></td>
</tr>
<tr>
<td colspan='2' align='center'>$CurrentDate</td>
</tr>
<tr>
<td colspan='2' align='center'>The IP address is <font color='red'><strong>$ip</strong></font></td>
</tr>
</table>";

// 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 = "Delete Email Address";

$message = "--------Employee Affected--------\n"
."First Name: ".$FirstName."\n"
."Last Name: ".$LastName."\n"
."\n\n--------PHONE--------\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 "<table align='center'>
<tr>
<th>Message has been sent</th>
</tr>
</table>";
}
}

unset($LastName);
unset($FirstName);
unset($Submitter);
unset($Email);
unset($HelpdeskEmail);
unset($to);
unset($subject);
unset($message);
unset($headers);

//<table align='center' border='3' bgcolor='aliceblue' frame='box' bordercolor='blue' rules='none'>
// <th colspan='2'><h3>Add Email Address</h3></th>
// <tr>
// <td>Employee First Name:</td>
// <td><input type='text' name='FirstName' value='$FirstName' size='40'></td>
// </tr>
// <tr>
// <td>Employee Last Name:</td>
// <td><input type='text' name='LastName' value='$LastName' size='40'></td>
// </tr>
// <tr>
// <td colspan='2'><hr color='blue' width='75%'></td>
// </tr>
// <tr>
// <td>Submitted By:</td>
// <td><input type='text' name='Submitter' value='$Submitter' size='40'></td>
// </tr>
// <tr>
// <td>Your Email Address:</td>
// <td><input type='text' name='Email' value='$Email' size='40'></td>
// </tr>
// <tr>
// <td colspan='2' align='center'>$CurrentDate</td>
// </tr>
// <tr>
// <td colspan='2' align='center'>The IP address is <font color='red'><strong>$ip</strong></font></td>
// </tr>
// </table>

?>[/php]

cputek2k

12:37 pm on May 25, 2005 (gmt 0)

10+ Year Member



And finally.... "enrich.php". This script is very simple with only one field. This was built for testing and more complex functionality will be added later.

<?
if (empty($EnrichName)){
echo "

<table>
<tr>
<td>Add Enrich Name:<input type='text' name='EnrichName' value='$EnrichName'></td>
</tr>
</table>";
}
else {
echo "You have entered ".$EnrichName.". (Enrich)";
}

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

?>