Solved

Unchecked Checkbox Still Checked In My Script

Posted on 2011-09-28
3
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 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