Jump to content

[SOLVED] Javascript Help


GreenSmurf

Recommended Posts

Recently a script I have stopped working and I am a little baffled by it. If someone could help point out any errors or potential problems I would appreciate it. There is some PHP involved but the majority of the code is JS.

 

function MailSelection()
{

  var addresses = "";
  var names = "";
  var business = "";
  var dst_count = 0;
  for (i = 0; i < document.getElementsByName("selected[]").length; i++)

  {
      if( document.getElementsByName("selected[]")[i].checked == true)
      {
         if( document.getElementsByName("selected[]")[i].accept != ""
		&& document.getElementsByName("selected[]")[i].accept != null) {

		addresses = addresses + document.getElementsByName("selected[]")[i].accept + ";";

		if( document.getElementsByName("selected[]")[i].names != ""
			&& document.getElementsByName("selected[]")[i].names != null) {
				names = names + document.getElementsByName("selected[]")[i].names + ";";
				}

		if( document.getElementsByName("selected[]")[i].buss != ""
			&& document.getElementsByName("selected[]")[i].buss != null) {
				business = business + document.getElementsByName("selected[]")[i].buss + ";";
				}
		dst_count++;
         }
      }
  }

   if(dst_count == 0)
       alert("No address selected.");
    else
        location.href = "mail.php?email="+addresses+"&names="+names+"&bus="+business;
}
</script>

 

Then down my code a bit.

 

if ($myrow["firstname2"] != "" AND $myrow["email2"] == ""){
             $names=$myrow["firstname"]."-".$myrow["lastname"].";".$myrow["firstname2"]."-".$myrow["lastname2"];
             $emails=$myrow["email"].";".$myrow["email"];
             $biz=$myrow["boss"].";".$myrow["boss"];}
             
        else if ($myrow["firstname2"] != "" AND $myrow["email2"] != ""){
             $names=$myrow["firstname"]."-".$myrow["lastname"].";".$myrow["firstname2"]."-".$myrow["lastname2"];
             $emails=$myrow["email"].";".$myrow["email2"];
             $biz=$myrow["boss"].";".$myrow["boss"];}
             
        else if ($myrow["firstname2"] == "" AND $myrow["email2"] == ""){
             $names=$myrow["firstname"]."-".$myrow["lastname"];
             $emails=$myrow["email"];
             $biz=$myrow["boss"];}
             
        else{
            $names=$myrow["firstname"]."-".$myrow["lastname"].";".$myrow["firstname"]."-".$myrow["lastname"];
            $emails=$myrow["email"].";".$myrow["email2"];
             $biz=$myrow["boss"].";".$myrow["boss"];}

	echo "<TD><input type=checkbox id=".$id." name='selected[]' value='$id' title='Select ($firstname $lastname)' alt='Select ($firstname $lastname)' buss=".$biz." names=".$names." accept=". email_slash($emails)."></td>";

 

echo "<td><input type=button value=\"Send e-Mail\" onclick=\"MailSelection()\"/></td>";

 

I would be grateful for any help. Thank you.

 

-Brandon

Link to comment
Share on other sites

Uff that's not the 'sexiest' code I've ever seen, you could seriously shrink down (and improve readability)  by using

  var addresses = "";
  var names = "";
  var business = "";
  var dst_count = 0;
  for (var i = 0; i < document.getElementsByName("selected[]").length; i++)
  {
      var cSelected = document.getElementsByName("selected[]")[i];
      if( cSelected.checked )
      {
         if( cSelected.accept != "" && cSelected.accept != null) 
         {         
             addresses += cSelected.accept + ";";
         
             if( cSelected.names != ""&& cSelected.names != null)
               names += cSelected.names + ";";
             if( cSelected.buss != "" && cSelected.buss != null)
               business += cSelected.buss + ";";
             
             dst_count++;
         }
      }
  }

   if(dst_count == 0)
       alert("No address selected.");
    else
        location.href = "mail.php?email="+addresses+"&names="+names+"&bus="+business;
}
</script>

 

Only possible problem I'm seeing and fixed above is that you didn't use var i = 0; inside the first if statement in the javascript. Thus it will be created as a global variable.. possibly overwriting another variable named i elsewhere in your code.

 

Link to comment
Share on other sites

  • 3 weeks later...

I have had more time to get "into" the code and understand the issue. It appears that only the email variables are being passed through the script. What I mean is this:

else
        location.href = "mail.php?email="+addresses+"&names="+names+"&bus="+business;

 

Comes out as this:

mail.php?email=name@website.com&names=&bus=

 

The thing of the matter it that there are definite names and business variables that are defined and set in other parts of the code. I just can't find why the names and bus variables won't set or get skipped. It could be getting skipped anywhere in the code too but it has to be after the line of code:

 echo "<TD><input type=checkbox id=".$id." name='selected[]' value='$id' title='Select ($firstname $lastname)' alt='Select ($firstname $lastname)' buss=".$biz." names=".$names." accept=". email_slash($emails)."></td>";

 

As I have already verified that the variables are set properly after this line of code with some output.

Link to comment
Share on other sites

  • 2 weeks later...

<?
<form name=MainForm method="get" action="group<? echo $page_ext; ?>">
<input type="hidden" name="group_name" value="<? echo $group_name; ?>">
<table width=100% border=0 cellpadding="6" align="center">
<col width=1*><!--15 columns -->
<col width=24>
<col width=24>
<col width=2*>
<col width=3*>
<col width=4*>
<col width=5*>
<col width=150>
<col width=84>
<col width=24>
<col width=24>
<col width=24>
<col width=24>
<col width=24>
<col width=6*>
<tr padding="0" align="center">
              <td> </td>
		  <td> </td>
              <td> </td>
              <td><u><a href=<?php echo $link2 ?>sort=clientID&order=<?php echo $rOrder ?>>Client ID</a></u></td>
              <td><u><a href=<?php echo $link2 ?>sort=lastname&order=<?php echo $rOrder ?>>Lastname</a></u></td>
              <td><u><a href=<?php echo $link2 ?>sort=firstname&order=<?php echo $rOrder ?>>Firstname</a></u></td>
              <td><u><a href=<?php echo $link2 ?>sort=boss&order=<?php echo $rOrder ?>>Company</a></u></td>
              <td><u><a href=<?php echo $link2 ?>sort=email&order=<?php echo $rOrder ?>>E-mail</a></u></td>
              <td><u>Phone #</u></td>
		  <td> </td>
              <td> </td>
              <td> </td>
              <td> </td>
              <td> </td>
              <td><u><a href=<?php echo $link2 ?>sort=accnt&order=<?php echo $rOrder ?>>Accountant</a></u></td>
              <td><u>Activity</u></td>
</tr>
<?
$alternate = "2"; 

include ("guess.inc");

while ($myrow = mysql_fetch_array($result))
{

	$clientID = $myrow["clientID"];
	$firstname = $myrow["firstname"];
	$id = $myrow["id"];
	$lastname = $myrow["lastname"];
	$boss = $myrow["boss"];

	$email  = ($myrow["email"] != "" ? $myrow["email"] : ($myrow["email2"] != "" ? $myrow["email2"] : ""));
	$email2 = $myrow["email2"];
	$email = email_slash($email);
    $email2 = email_slash($email2);

	$home   = $myrow["home"];
	$mobile = $myrow["mobile"];
	$work   = $myrow["work"];
	$accnt  = $myrow["accnt"];
	$notes  = $myrow["notes"];

	// Phone order home->mobile->work
	$phone = ($myrow["home"] != "" ? $myrow["home"]
                                               : ($myrow["mobile"] != "" ? $myrow["mobile"]
                                                                         : $myrow["work"]));
	/*$phone = str_replace("'", "",
                         str_replace('/', "", 
                         str_replace(" ", "", 
                         str_replace(".", "", $phone))));*/
                         
        $phone = phone_number($phone);


	if ($alternate == "1") { 
		$color = "#ffffff"; 
		$alternate = "2"; 
	} 
	else { 
		$color = "#efefef"; 
		$alternate = "1"; 
	}
	echo "<TR bgcolor=$color>";
        if ($myrow["firstname2"] != "" AND $myrow["email2"] == ""){
             $names=$myrow["firstname"]."-".$myrow["lastname"].";".$myrow["firstname2"]."-".$myrow["lastname2"];
             $emails=$myrow["email"].";".$myrow["email"];
             $biz=$myrow["boss"].";".$myrow["boss"];}
             
        else if ($myrow["firstname2"] != "" AND $myrow["email2"] != ""){
             $names=$myrow["firstname"]."-".$myrow["lastname"].";".$myrow["firstname2"]."-".$myrow["lastname2"];
             $emails=$myrow["email"].";".$myrow["email2"];
             $biz=$myrow["boss"].";".$myrow["boss"];}
             
        else if ($myrow["firstname2"] == "" AND $myrow["email2"] == ""){
             $names=$myrow["firstname"]."-".$myrow["lastname"];
             $emails=$myrow["email"];
             $biz=$myrow["boss"];}
             
        else{
            $names=$myrow["firstname"]."-".$myrow["lastname"].";".$myrow["firstname"]."-".$myrow["lastname"];
            $emails=$myrow["email"].";".$myrow["email2"];
             $biz=$myrow["boss"].";".$myrow["boss"];}

	echo "<TD><input type=checkbox id=".$id." name='selected[]' value='$id' title='Select ($firstname $lastname)' alt='Select ($firstname $lastname)' buss=".$biz." names=".$names." accept=". email_slash($emails)."></td>";

	if($myrow["bustype"] == "Terminated")
	echo "<td><a href=edit${page_ext_qry}id=$id><img border=0 src=icons/term.png width=16 height=16 title='Terminated' alt='Terminated'/></a></td>";
	else echo "<td/>";

	if($myrow["comment"] != "")
	echo "<td><a href=edit${page_ext_qry}id=$id#attent><img border=0 src=icons/attention.gif width=16 height=16 title='Attention!' alt='Attention!'/></a></td>";
	else echo "<td/>";

	echo "<td>$clientID</td>";
	echo "<TD>$lastname</td>";
	echo "<td>$firstname</td>";
	echo "<td>".ShortenText($boss, 20)."</td>";
	echo "<td><a href='mailto:$email'>$email</a></td>";
	echo "<td align=right>$phone</td>";
	echo "<td><a href='view${page_ext_qry}id=$id'><img border=0 src=icons/status_online.png   width=16 height=16 title='Details' alt='Details'/></a></td>";
                if(! $read_only)
	  echo "<td><a href='edit${page_ext_qry}id=$id'><img border=0 src=icons/pencil.png width=16 height=16 title='Edit' alt='Edit'/></a></td>";
	echo "<td><font size=-2><a href='vcard${page_ext_qry}id=$id'><img border=0 src=icons/vcard.png   width=16 height=16 title='vCard' alt='vCard'/></a></font></td>";

        /*if( substr($phone, 0, 1) == "0" || substr($phone, 0, 3) == "+41" || substr($phone, 0, 1) == "")
	{
		$country = "Switzerland";
	}
	else 	$country = "";*/

	if($map_guess)
	{
	if($myrow["address"] != "")
	echo "<td><font size=-2><a href='http://maps.google.com/maps?q=".urlencode(trim(str_replace("\r\n", ", ", $myrow["address"]))." ".trim($myrow["city"]).", ".trim($myrow["state"])." ".trim($myrow["zip"]).", $country")."&t=h' target='_blank'>
                          <img border=0 src=icons/car.png width=16 height=16 title='Google Maps' alt='Google Maps'/></a></font></td>";
	else echo "<td/>";
	}

	$homepage = guessHomepage($email, $email2);
	if(strlen($homepage) > 0)
	{
		echo "<td><font size=-2><a href='http://$homepage' target='_blank'><img border=0 src=icons/house.png   width=16 height=16 title='Guessed Homepage ($homepage)' alt='Guessed Homepage ($homepage)'/></a></font></td>";
	} else
		echo "<td/>";
        echo "<td>".pic($accnt)."</td>";
        echo "<td>".ShortenText($notes,45)."</td>";
	echo "</TR>\n";
}

echo "<tr height=2/>";
echo "<TR >";
	echo "<TD><input type=checkbox id=MassCB onclick=\"MassSelection()\"></td><td colspan=\"3\"><em><strong>Select all</strong></em></TD>";
echo "</TR>\n";
echo "<tr height=9/>";
echo "</TR></TABLE>";
  echo "<TABLE width=100%><TR>";
        echo "<td><input type=button value=\"Send e-Mail\" onclick=\"MailSelection()\"/></td>";	

if(isset($table_groups) and $table_groups != "" and !$is_fix_group)
{

	// -- Remove from group --
	if($group_name != "" and $group_name != "[none]") 
	{
        	echo "<td align=center><input type=submit name=remove value='Remove from \"$group_name\"'/></td>";
	} else
        	echo "<td align=center/>";

	// -- Add to a group --
        	echo "<td align=right><input type=submit name=add value='Add to group'/>-";
        	echo "<select name=to_group>";

	$sql="SELECT group_name FROM $table_groups ORDER BY lower(group_name) ASC";
	$result = mysql_query($sql);
	$resultsnumber = mysql_numrows($result);

	while ($myrow = mysql_fetch_array($result))
	{
		echo "<option>".$myrow["group_name"]."</option>\n";
	}
        	echo "</select>";

}
echo "</TR></form>";

 

Here is the <form> code that was requested. Sorry it took so long to reply I have been working on multiple projects. Thanks again.

 

-B

Link to comment
Share on other sites

echo "<TD><input type=checkbox id=".$id." name='selected[]' value='$id' title='Select ($firstname $lastname)' alt='Select ($firstname $lastname)'></td>";
	echo "<input type=hidden id=".$id." name='accept' value='accept=". email_slash($emails)."'>";
	echo "<input type=hidden id=".$id." name='names' value='$names'>";
	echo "<input type=hidden id=".$id." name='biz' value='$biz'>";

 

This is what I tried to fix it with but I am not getting an error that says "No addresses selected." Which means that the code for the JavaScript portion is not seeing that the check boxes are clicked. Most likely due to a misunderstanding of the way the HTML input needs to communicate with the JavaScript.

 

-B

Link to comment
Share on other sites

I found the solution to my problem and made a few adjustments.

The first change in code is:

if( document.getElementsByName("selected[]")[i].accept != ""
		&& document.getElementsByName("selected[]")[i].accept != null) {

		addresses = addresses + document.getElementsByName("selected[]")[i].accept + ";";

		if( document.getElementsByName("names")[i].accept != ""
			&& document.getElementsByName("names")[i].accept != null) {
				names = names + document.getElementsByName("names")[i].accept + ";";
				}

		if( document.getElementsByName("bus")[i].accept != ""
			&& document.getElementsByName("bus")[i].accept != null) {
				business = business + document.getElementsByName("bus")[i].accept + ";";

 

The second adjustment is:

echo "<TD><input type=checkbox id=".$id." name='selected[]' value='$id' title='Select ($firstname $lastname)' alt='Select ($firstname $lastname)' accept=".email_slash($emails)."></td>";
	echo "<TD><input type=hidden name='names' accept=".$names."></td>";
	echo "<TD><input type=hidden name='bus' accept=".$bus."></td>";

 

Thanks to all those who helped.

 

-B

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.