Solved

Unchecked Checkbox Still Checked In My Script

Posted on 2011-09-28
3
231 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
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP and Soap 3 27
Problem to page 4 17
Why does my array not "dump?" 5 16
jqplot Pie Chart - javascript 2 7
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:

743 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

14 Experts available now in Live!

Get 1:1 Help Now