Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Unchecked Checkbox Still Checked In My Script

Posted on 2011-09-28
3
Medium Priority
?
250 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 1000 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Ready to get certified? Check out some courses that help you prepare for third-party exams.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

571 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