Hello I am working on php website. Now here im working on sending orders. So once it is ordered the confirmation e-mail goes to one who ordered(he enters his e-mail id manually) and the head of the department(e-mail ids written in code).
But now I have put a checkbox that if the person is head of the department he checks the checkbox and e-mail should be sent to him. So how to verify this by checkbox.Can anyone please help me with this. The person should not get 2 e-mails.
I hope I was clear in explaining my doubt. If not please do ask me for more explanation.
Thank you.

Recommended Answers

All 14 Replies

Hopefully I'm understanding you right, but it seems that a simple solution would be to have your form processing script check whether the box was checked and deal with it through an if then statement.

So if the checkbox in your form is called "department_head_checkbox" and it has a checked value of 1...

$deptheadbox = $_POST['department_head_checkbox'];
if ($deptheadbox == '1'){
     ...//Whatever needs to happen if the box was checked
} else {
     ...//Whatever needs to happen if the box was not checked
}

Put that wherever your processing script decides what e-mail addresses are used.

Sort of rudimentary, but if you need more help, just post your questions (and including some code always helps).

-Ty

Thank you so much for your prompt reply. I will definitely try this out and get back to you. Hmm am just elaborating out your reply just to be sure that I understood it right. I have 2pages index.php=where i enter the values and tick the checkbox. so there i just need to write input type="checkbox.........n so on right?
The other page is send.php so this page will include the code :
1.
$deptheadbox = $_POST;
2.
if ($deptheadbox == '1'){
3.
...//Whatever needs to happen if the box was checked
4.
} else {
5.
...//Whatever needs to happen if the box was not checked
6.
}
right?

Yup :)

Thanks again for the reply. I will get back to you about my working of code.

$departments = array("access"=>"Access Services",
	  	"advancement"=>"Advancement",
		"archives"=>"Archives",
		"closet"=>"Supply Closet",
		"digital"=>"Digital Library Management",
		"dmds"=>"DMDS",
		"metadata"=>"Metadata Management",
		"office"=>"Office of the Dean",
		"research"=>"Research & Instruction",
		"resourcem"=>"Resource Management",
		"resource"=>"Resource Sharing",
		"scholarly"=>"Scholarly Resources", 
		"systems"=>"Systems" ,
	    "others"=>"Other");
$to = array("access"=>"d.kennedy@neu.edu",
		"advancement"=>"m.carpenter@neu.edu", //m.carpenter@neu.edu
		"archives"=>"j.krizack@neu.edu",
		"closet"=>"e.bren@neu.edu",
		"digital"=>"p.yott@neu.edu",
		"dmds"=>"d.mandel@neu.edu",
		"office"=>"e.bren@neu.edu; [email]e.habich@neu.edu[/email]",
		"metadata"=>"m.menke@neu.edu",
		"research"=>"j.dendy@neu.edu",
		"resourcem"=>"j.morrow@neu.edu",
		"resource"=>"br.greene@neu.edu",
		"scholarly"=>"a.aaron@neu.edu", 
		"systems"=>"r.krol@neu.edu",//r.krol
		"others"=>"sharma.me@husky.neu.edu"); 
		
		
		
// STEP 1: DATA LOADING

$_SESSION = $_POST;

// Load all the items requested into an array for easier handling...
$itemelements = array("description", "url", "catalog", "page", "unit", "quantity", "vendor", "price");
foreach ($itemelements as $element)
{
	$x = 0;
	foreach ($_POST[$element] as $value)
	{
		$items[$x][$element] = $value;
		$x++;
	}
}

// Check to make sure all fields have a value...
$requiredelements = array("name", "department", "email", "extension", "whenneeded");
foreach ($requiredelements as $element)
{
	if ($_POST[$element] == "")
	{
		$_SESSION["error"][] = "$element";
	}
}

$requiredelements = array("description", "catalog", "unit", "quantity", "vendor", "price");

for ($x = 0; $x < sizeof($items); $x++)
{
	if (trim($items[$x]['price']) == '0.00' ||
		trim($items[$x]['price']) == '')
	{
		$skip = true;
		foreach ($requiredelements as $element)
		{
			if (trim($items[$x][$element]) != "" &&
				$element != 'price')
			{
				$skip = false;
			}
		}
		if ($skip)
		{
			$_SESSION["ItemTotal"] = $_SESSION["ItemTotal"] - 1;
			$items[$x] = NULL;
			foreach ($itemelements as $element)
			{
				unset($_SESSION[$element][$x]);
			}
			continue;
		}
	}
}	
	
// Reset the ID's of the various arrays...
$y = 0;
foreach ($items as $item)
{
	if ($item != NULL)
	{
		$newitems[$y] = $item;
		$y++;
	}
}
$items = $newitems;

foreach ($itemelements as $element)
{
	$y = 0;
	foreach ($_SESSION[$element] as $value)
	{
		$newelement[$y] = $value;
		$y++;
	}
	$_SESSION[$element] = $newelement;
	unset($newelement);
}

for ($x = 0; $x < sizeof($items); $x++)
{	
	// Make sure each item has a value...
	foreach ($requiredelements as $element)
	{
		if ($items[$x][$element] == "")
		{
			$_SESSION["error"][] = "$element$x";
		}
		else
		{
			// Check the price
			if ($element == "price")
			{
				// Remove letters
				$price = ereg_replace("[A-za-z]", "", $items[$x]['price']);
				
				// Remove the $ sign if it's there
				if (substr($price, 0, 1) == '$')
				{
					$price = substr($price, 1);
				}
				
				// The price can't be zero.
				if (ceil($price) == 0)
				{
					$_SESSION["error"][] = "$element$x";
				}
				else
				{
					// If there's a dot, check to make sure there are only 2 decimal places
					// for the cents.  Otherwise, there's probably something wrong.
					if (strstr($price, "."))
					{
						$price = explode('.', $price);
						if ($price)
						{
							if (sizeof($price) == 2)
							{
								if (strlen($price[1]) != 2)
								{
									$_SESSION["error"][] = "$element$x";
								}
							}
							else
							{
								$_SESSION["error"][] = "$element$x";
							}
						}
						else
						{
							$_SESSION["error"][] = "$element$x";
						}
					}
					else
					{
						// There is no dot, so just check to make sure there's a number.
						if (!is_numeric($price))
						{
							$_SESSION["error"][] = "$element$x";
						}
					}
				}
			}
		}
	}
}

// STEP 3: Verification
if (sizeof($_SESSION["error"]) == 0)
{	
	// STEP 4: Everything is good to go!  Start composing the e-mail.
	$message = file_get_contents("messagem.html");
	$message = str_replace('$department', $departments[$_POST["department"]], $message);
	$message = str_replace('$name', $_POST["name"], $message);
	$message = str_replace('$email', $_POST["email"], $message);
	$message = str_replace('$extension', $_POST["extension"], $message);
	$message = str_replace('$whenneeded', $_POST["whenneeded"], $message);
	
	if ($_POST["comments"] != "")
	{
		$message = str_replace('$comments', $_POST["comments"], $message);
	}
	else
	{
		$message = str_replace('$comments', "None", $message);
	}
	
	$itemhtml = "";
	for ($x = 0; $x < sizeof($items); $x++)
	{	
		$itemhtml .= "<li class=\"style1\"><strong>" . $items[$x]["description"] . "</strong><br /><ul>";
		
		if ($items[$x]["url"] != "")
		{
			$itemhtml .= "<li>URL: " . $items[$x]["url"] . "</li>";
		}
		
		$itemhtml .= "<li>Catalog Item: " . $items[$x]["catalog"] . "</li>";
		
		if ($items[$x]["page"] != "")
		{
			$itemhtml .= "<li>Page: " . $items[$x]["page"] . "</li>";
		}
		
		$itemhtml .= "<li>Quantity: " . $items[$x]["quantity"] . "</li>";
		$itemhtml .= "<li>Unit: " . $items[$x]["unit"] . "</li>";
		$itemhtml .= "<li>Price: $" . $items[$x]['price'] . "</li>";
		$itemhtml .= "<li>Vendor: " . $items[$x]["vendor"] . "</li>";
		$itemhtml .= "</ul></li>";
	}		
	$message = str_replace('$items', $itemhtml, $message);
	$message = str_replace("^", "/", $message);
	
	$headers = "";
	$headers .= "Content-type: text/html; charset=iso-8859-1\n";
	$headers .= "Reply-to: <" . $_POST["email"] . ">\n";
	$headers .= "From: " . addslashes($_POST["name"]) . " <" . $_POST["email"] . ">\n";
	$headers .= "CC: " . addslashes($_POST["name"]) . " <" . $_POST["email"] . ">\n";
	$headers .= "MIME-Version: 1.0\n";

//	if (mail("wood.jac@husky.neu.edu", "Supply Form", $message, $headers))	
	if (mail($to[$_POST["department"]], "You have a new order", $message, $headers))
	{
		echo "<font color=\"#009900\">Order complete!</font> - <a href=\"index.php\">Order More</a>";
	}
	else
	{
		
		echo "<font color=\"#FF0000\">Order failed.  Reason: Mail fail.</font> - <a href=\"index.php\">Order More</a>";
	}
	
	echo $message;

This is the code now after checkbox is verified it sends to head of the department means the e-mail ids listed above so how do I write them here again in if condition how to see whether it goes to that particular head and if not then it should go to both the person who entered his e-mail id and to the head(e-mail ids listed above).

I'm not sure I'm completely understanding the way your processing is supposed to work. You should be able to include a statement that evaluates whether a department head's box has been checked, but I don't know the names of your form values, so would you mind posting the code for the corresponding form as well?

PS- Remember to use [code] tags : )

good thought i will try

<h1 align="center">Supply Request Form</h1>
<p align="left" class="style1"><strong>Update:</strong> Accidentally added a field? Simply leave it blank, and it will no longer require you to start over.</p>
<form action="send.php" method="post" onSubmit="return verifyCharacters()">  
  <input type="hidden" name="ItemTotal" value="<? echo (int)$_SESSION["ItemTotal"]; ?>" id="ItemTotal" />
   <table>
    <tr>
      <td class="style1"><font color="red">*</font> <? writeField("Name", "name", ""); ?></td>
     <td><input name="name" type="text" class="style1" size="50" maxlength="50" value="<? echo $_SESSION["name"]; ?>"></td></tr>
    <tr>
      <td class="style1"><font color="red">*</font> <? writefield("Department", "department", ""); ?></td>
      <td>
     <select name="department" class="style1">
      <option value="">Select Dept.</option>
<!--To add or change a department and the email it goes to, also edit the file called send.php. gkm 7/2010 -->      
      <?
	  $departments = array("access"=>"Access Services", "advancement"=>"Advancement", "archives"=>"Archives", "digital"=>"Digital Library Management", "dmds"=>"DMDS",  "metadata"=>"Metadata Management", "office"=>"Office of the Dean", "research"=>"Research &amp; Instruction", "resourcem"=>"Resource Management","resource"=>"Resource Sharing", "scholarly"=>"Scholarly Communication", "closet"=>"Supply Closet", "systems"=>"Systems" , "others"=>"Other");
	  $friendly = array_values($departments);
	  $keys = array_keys($departments);
	  
	  for ($z = 0; $z < sizeof($departments); $z++)
	  {
	  	echo "<option value=\"" . $keys[$z] . "\"";
	  	if ($_SESSION["department"] == $keys[$z])
		{
			echo " selected=\"selected\"";
		}
		echo ">" . $friendly[$z] . "</option>";
	  }
	  ?>
     </select>   </td></tr>
   <tr>
     <td class="style1"><input type="checkbox" name="I am the Head of the Department.">
</td>
     <td>I am the Head of the Department</td>
   </tr>
    <tr>
      <td class="style1"><font color="red">*</font>
        <? writeField("E-mail", "email", ""); ?></td>
     <td><input name="email" type="text" class="style1" value="<? echo $_SESSION["email"]; ?>"></td></tr>
     <tr>
       <td><span class="style1"><span class="style1"><font color="red">*</font>
           <? writeField("Extension", "extension", ""); ?>
       </span></td>
     <td><input name="extension" type="text" class="style1" value="<? echo $_SESSION["extension"]; ?>"></td></tr>
     <tr>
       <td height="29"><font color="red">*</font>         <? writeField("When Needed", "whenneeded", ""); ?></td>
     <td><input name="whenneeded" type="text" class="style1" value="<? echo $_SESSION["whenneeded"]; ?>"></td></tr>
     <tr>
       <td height="29"><span class="style1">Additional info/comments</span></td>
       <td><input name="comments" type="text" class="style1" size="40" maxlength="120" value="<? echo $_SESSION["comments"]; ?>"></td>
     </tr>
    </table>
  </p>
<div id="Item"><?
if (isset($_SESSION["error"]))
{
	for ($x = 0; $x < $_SESSION["ItemTotal"]; $x++)
	{
		echo (include "row.php");
	}
}

Above is the code where im entering the form values and need to include the checkbox whether is the head of department or not. (filename for above code is index)
Then I guess i need to write the code to verify whether the checkbox is checked ornot in send.php thats the code i previously posted.
So am not getting how to send it to write the code in if statements to send to that respective head.Please reply asap.
Thank you.

I still am missing some of your code- the file that defines your writefield() function is missing (or you didn't include the part of index.php that defines that function), so I can't test this at all, but here are some issues you need to correct first:

On line 32 of index.php, you have this:

<td class="style1"><input type="checkbox" name="I am the Head of the Department.">

Change that to this:

<td class="style1"><input type="checkbox" name="dept_head" value="1">

Changing the name makes it easier to work with, and it should have a value of 1 so we can see if it was checked.

I'm assuming the way this works (again, I can't see the form as it displays in a browser) is that the users fills the form, and when they submit, an e-mail gets sent to both the user and the head of a department. BUT, if a department head is filling the form, they check the above-mentioned checkbox and the e-mail only goes to them. If I have this wrong, let me know, but if I'm correct...

Starting at line 227 in send.php, we currently have this:

$headers = "";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "Reply-to: <" . $_POST["email"] . ">\n";
$headers .= "From: " . addslashes($_POST["name"]) . " <" . $_POST["email"] . ">\n";
$headers .= "CC: " . addslashes($_POST["name"]) . " <" . $_POST["email"] . ">\n";
$headers .= "MIME-Version: 1.0\n";

// if (mail("wood.jac@husky.neu.edu", "Supply Form", $message, $headers))
if (mail($to[$_POST["department"]], "You have a new order", $message, $headers))
{
echo "<font color=\"#009900\">Order complete!</font> - <a href=\"index.php\">Order More</a>";
}
else
{

echo "<font color=\"#FF0000\">Order failed. Reason: Mail fail.</font> - <a href=\"index.php\">Order More</a>";
}

echo $message;

It looks as though it's only sending one e-mail, which is addressed to the head of the department. You need submit.php to say "IF 'dept_head' was checked, then send 1 e-mail to the department head. Otherwise, send 1 e-mail to the department head and send 1 e-mail to the specified e-mail address."

$headers = "";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "Reply-to: <" . $_POST["email"] . ">\n";
$headers .= "From: " . addslashes($_POST["name"]) . " <" . $_POST["email"] . ">\n";
$headers .= "CC: " . addslashes($_POST["name"]) . " <" . $_POST["email"] . ">\n";
$headers .= "MIME-Version: 1.0\n";

if ($_POST['dept_head'] != 1) { //The 'dept_head' box was not checked (does not evaluate to '1').  So 2 e-mails need to be sent
    $mails = mail($to[$_POST["department"]], "You have a new order", $message, $headers);  //Message addressed to department head
    $mails .= mail($_POST["email"], "You have a new order", $message, $headers); //The same message addressed to the user
} else { //The 'dept_head' box WAS checked (evaluates to '1').  Only 1 e-mail should be sent
    $mails = mail($to[$_POST["department"]], "You have a new order", $message, $headers);  //Message addressed to department head
}
// if (mail("wood.jac@husky.neu.edu", "Supply Form", $message, $headers))
if ($mails)
{
echo "<font color=\"#009900\">Order complete!</font> - <a href=\"index.php\">Order More</a>";
}
else
{

echo "<font color=\"#FF0000\">Order failed. Reason: Mail fail.</font> - <a href=\"index.php\">Order More</a>";
}

echo $message;

thank you loads for your help I made the changes as you said and WOLAAA!! it worked Thanks a lot now just one problem is there that if I dont check the checkbox then it sends 2 copies to the person who entered his e-mail and one copy(as it should) to department head.
But yes when the checkbox is checked it does send just one copy to the dept head. Thankssoooooooo much.
I'm extremely for the part of the code thats missing but if its fine with you can i please have ur e-mail id where I can send you both of my files. Due to soem reaosn I cnt post the code here.

Glad to be of help. I PMed you with my e-mail if you want to send those files over.

Hey hi hope you got my e-mail. with the attachments. i sent it to you on 21st.
Thank you. Waiting eagerly for your reply.

Hi I am sorry to bother you but please if you can provide me with some solution I tried but am not able to understand where did i go wrong I guess there is some bracket problem in the second if condition so if you can reply to me here or in e-mail. I hope you received it.
Thank you so much for all the help.

Thank you so much.Got it solved it works now:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.