Solved

Unchecked Checkbox Still Checked In My Script

Posted on 2011-09-28
3
242 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
arrays and radio buttons 10 40
arrays and buttons with user input 2 34
restriction of entering a a page 5 34
Why is my implode function empty 8 18
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

726 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