GreenSmurf Posted April 15, 2009 Share Posted April 15, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/154232-solved-javascript-help/ Share on other sites More sharing options...
Axeia Posted April 15, 2009 Share Posted April 15, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/154232-solved-javascript-help/#findComment-811024 Share on other sites More sharing options...
GreenSmurf Posted April 19, 2009 Author Share Posted April 19, 2009 I tried the new code an it would seem that the button stopped working entirely. When I click it nothing happens the pages just sits there. I am wondering if there is something wrong in/or before the return statement. -Brandon Quote Link to comment https://forums.phpfreaks.com/topic/154232-solved-javascript-help/#findComment-813609 Share on other sites More sharing options...
GreenSmurf Posted May 7, 2009 Author Share Posted May 7, 2009 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: [email protected]&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. Quote Link to comment https://forums.phpfreaks.com/topic/154232-solved-javascript-help/#findComment-828734 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Can you post the HTML for the form? I don't know why you are using getElementsByName() when you can just reference the form directly. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/154232-solved-javascript-help/#findComment-829222 Share on other sites More sharing options...
GreenSmurf Posted May 8, 2009 Author Share Posted May 8, 2009 I can but my code is really sloppy and a bit large, is that ok? Quote Link to comment https://forums.phpfreaks.com/topic/154232-solved-javascript-help/#findComment-829252 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Just the form itself. I don't need everything. Quote Link to comment https://forums.phpfreaks.com/topic/154232-solved-javascript-help/#findComment-829432 Share on other sites More sharing options...
GreenSmurf Posted May 19, 2009 Author Share Posted May 19, 2009 <? <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 Quote Link to comment https://forums.phpfreaks.com/topic/154232-solved-javascript-help/#findComment-836876 Share on other sites More sharing options...
Ken2k7 Posted May 19, 2009 Share Posted May 19, 2009 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>"; You just gave an INPUT tag attributes that don't exist. ??? http://www.w3schools.com/TAGS/tag_input.asp Quote Link to comment https://forums.phpfreaks.com/topic/154232-solved-javascript-help/#findComment-836905 Share on other sites More sharing options...
GreenSmurf Posted May 19, 2009 Author Share Posted May 19, 2009 This is where I thought the problem was located. What is proper way of resolving this? -B Quote Link to comment https://forums.phpfreaks.com/topic/154232-solved-javascript-help/#findComment-836972 Share on other sites More sharing options...
GreenSmurf Posted May 27, 2009 Author Share Posted May 27, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/154232-solved-javascript-help/#findComment-842802 Share on other sites More sharing options...
GreenSmurf Posted May 29, 2009 Author Share Posted May 29, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/154232-solved-javascript-help/#findComment-844754 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.