Solved

Unchecked Checkbox Still Checked In My Script

Posted on 2011-09-28
3
238 Views
Last Modified: 2012-05-12
Hi Experts,

Thanks for reading. I need some help with my script. It outputs a bunch of rows with info including an "email" address and allows you to check or uncheck a checkbox to specify which emails to get and click a "Send Email" button and to open the mail client with the email addresses in it.

The problem is when I select a checkbox and hit send email, the email comes up in the mail client fine. But when I close the message window, uncheck the box, and check a new row's box, both emails come up in the mail client. The issue is not with the mail client: it's with the script.

Can one of you Experts please help me implement a solution that will fix the uncheck issue? Here is my code:

The JS functions:

function handleEmail(formElement, email) {
	
	x = readCookie("emailString");
	if(x) {
		var theValues = x.split(',');
		var theOutput = "";
		for (i=0;i<theValues.length;i++) {
			if (theValues[i]!=email && theValues[i]!="") {
				theOutput = theValues[i] + ", " + theOutput;
				}
			}
			if (formElement.checked==true) {
				createCookie("emailString",theOutput + email, 1);
				}
			else {
				if (theOutput!="") {
					createCookie("emailString",theOutput, 1);
					}
				else {
					eraseCookie("emailString");
					}
				}
		}
	else {
		createCookie("emailString",email,1);
		}
	}
	
	
function sendEmail() {
	x = readCookie("emailString");
	if(x) {
		var theValues = x.split(',');
		var theOutput = "";
		for (i=0;i<theValues.length;i++) {
			if (theValues[i]!="") {
				theOutput = trim(theValues[i]) + "; " + theOutput;
				}
			}	
		location.href="mailto:?bcc=" + theOutput;
		}
	else {
		alert("no email addresses are selected");
		}
	}

Open in new window


The PHP code:

  <table class="results_table">
    <tr>
      <td class="header_row">Date</td>
      <td class="header_row">Email</td>
      <td class="header_row">Name</td>
      <td class="header_row">Phone</td>
      <td class="header_row">Experience</td>
    </tr>
    <?
$x = 1;
while ($myrow = mysql_fetch_array($result)) {
	$numid = $myrow['ID'];
?>
    <tr>
      <td><?=$myrow[Date];?></td>
      <td align="center"><? 
if ($myrow['Email']!="") { 
	if (strstr($_COOKIE['emailString'],$myrow['Email'])!="") { 
		$ch = "checked"; 
		}
	else {
		$ch = "";
		}
	  ?>
        <input type="checkbox" name="email<? echo $x; ?>"  value="<? echo $myrow['Email']; ?>" onclick="javascript:handleEmail(this, '<? echo $myrow['Email']; ?>');" <? echo $ch; ?>>
        <?
	} 
?>
      </td>
      <td><a href="search.php?id=<? echo $numid;
	  if ($keyword_searchstring!="") { echo "&keyword=$keyword_searchstring"; }
	  ?>"><? echo $myrow['LastName'].", ".$myrow['FirstName']; ?></a></td>
      <td><? echo $myrow['Phone1']; ?>&nbsp;</td>
      <td><? echo $myrow['Experience']; ?>&nbsp;</td>
    </tr>
    <?
	}
?>
  </table>
  <table class='num_menu'>
    <tr>
      <td colspan='2' align=center><div class='num_menu'>
          <?=writeNumberMenu(6, $pages, 100, $curr_page, $link_text)?>
        </div></td>
    </tr>
  </table>
  <table class="top_row">
    <tr>
      <td align="center" colspan="5"><input type="button" onclick="javascript:sendEmail();" value="Send Email to Checked" class="button">
      </td>
    </tr>
  </table>

Open in new window


Thank you so much.
0
Comment
Question by:SevenAteAnthony
3 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 250 total points
ID: 36740713
Put a text field on your form where you output the plain text from readCookie("emailString") on browser JavaScript side or $_COOKIE['emailString'] from PHP side.
You will see that you create triple and multiple cookies back and fort because you do not define a cookie Realm by setting the path=/; to: root

0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 36793489
You can use var_dump() to print out the values of the external variables in your script.

The ways that checkboxes work in HTML and PHP are described in this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_5450-Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html

I expect that the interplay of PHP and JavaScript and HTML is somehow causing the problem, but I am not sure.  The general design patterns that professional programmers would use exclude the intermix of logic and presentation.
0
 
LVL 1

Author Closing Comment

by:SevenAteAnthony
ID: 36977300
Thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now