?
Solved

Multiple mailing list signup

Posted on 2007-10-20
8
Medium Priority
?
543 Views
Last Modified: 2010-08-05
Hi
Does anyone happen to have a javascript for handling signup for mutiple mailing lists (via checking 0 to many checkboxes)?

Thanks


Here's what I have:
<!--

function signUp(frm) {
var isError = false;
var emailAddress = frm.Email.value;
if (emailAddress  == "" || emailAddress.indexOf("@") == -1) \{
alert("Please enter your email address");

isError = true;

frm.Email.focus()
\}

var name = frm.Name.value;
if (name == "") \{
alert("Please enter name");

isError = true;

frm.firstName.focus()
\}


var checkNewsletter = "";

for (i=0; i<frm.AddressBookID.length; i++) \{

if(AddressBookID[i].checked) \{
   
   checkBlog += CheckNewsletter + frm.AddressBookID[i].value;
   \}
else \{
alert ("Please select the newsletters you wish to subscribe to"); isError = true;
\}
 

       
return !isError;

\}

//->
0
Comment
Question by:Lady_M
  • 4
  • 2
7 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 20115302
You could use:

var checkedAddressBooks = [];

for(var i = 0; i < frm.AddressBookID.length; i++) {
  if(frm.AddressBookID[i].checked) {
    checkedAddressBooks.push(CheckNewsletter + frm.AddressBookID[i].value);
  }
}

checkBlog = checkedAddressBooks.join(";");

if(checkedAddressBooks.length == 0) {
  alert("Please select at least one newsletter");
  isError = true;
}
0
 

Author Comment

by:Lady_M
ID: 20115358
Thankyou

So you mean like this?
function signUp(){
var checkedAddressBooks = [];

for(var i = 0; i < frm.AddressBookID.length; i++) {
  if(frm.AddressBookID[i].checked) {
    checkedAddressBooks.push(CheckNewsletter + frm.AddressBookID[i].value);
  }
}

checkBlog = checkedAddressBooks.join(";");

if(checkedAddressBooks.length == 0) {
  alert("Please select at least one newsletter");
  isError = true;
}
}


<form name="SignUpForm" action="myfile.asp" method="post" onsubmit="signUp()">
Name: <input type="text" name="Name">
Email:<input type="text" name="Email">
<label>One</label><input type="checkbox" name="AddressBookID" value="12345">
<label>Two</label><input type="checkbox" name="AddressBookID" value="23536">
<label>Three</label><input type="checkbox" name="AddressBookID" value="57272">
<label>Four</label><input type="checkbox" name="AddressBookID" value="57385">
etc...



All the selected checkboxes would be saved in the checkedAddressBooks variable presumably - as an array?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 20115372
Yes, the checkedAddressBooks would be an array.

I added the checkBlog = checkedAddressBooks.join(";"); line to show you how to convert it back to a string (values separated by a semicolon).

I wasn't sure if you actually would need the checkBlog or the checkAddressBooks variables or are just validating your form.

-r-
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Lady_M
ID: 20115421
Hi
Well I need to validate that the first name, the email address and at least one checkbox has been checked before the form is submitted.
Then I need to pass those plus all the checked values to http://www.dotmailer.co.uk/ 

It doesn't seem to be doing anything at the moment.  I took the form action out.
0
 

Author Comment

by:Lady_M
ID: 20115626
I have tried this and it's not working.  It doesn't like the for loop to add to the array near the start of the function.
Thanks

var checkedAddressBooks = [];

function signUp_onsubmit()
{
      var returnValue = false;

      for (var i = 0; i < document.formSignup.AddressBookID.length; i++)
      {
              if(frm.AddressBookID[i].checked) {
                  checkedAddressBooks.push(frm.AddressBookID[i].id="" value);/*add value to end of array*/
                  alert (AddressBookID[i].value);
              }
      }

      
      if (document.formSignup.txtFirstName.value == "")
      {
            alert ("Please enter your first name");
            document.formSignup.txtFirstName.focus();
      }
      
      else if (document.formSignup.txtSurname.value == "")
      {
            alert ("Please enter your surname");
            document.formSignup.txtSurname.focus();
      }
      
      else if (document.formSignup.txtEmail.value == "")
      {
            alert ("Please enter your email");
            document.formSignup.txtEmail.focus();
      }
      
      
      
      else if(checkedAddressBooks.length == 0)
      {
        alert("Please select at least one list");
        isError = true;
      }

      else
      {
            returnValue = true;
      }
      return returnValue;

}
</script>
</head>

<body>

<form ACTION="something.asp"
METHOD=post
NAME=formSignup
onsubmit="return signUp_onsubmit()">
<fieldset>
<input type="hidden" name="UserID" id="" value="1234">
<input type="hidden" name="ReturnURL" id="" value="/thanks.html">
<legend>Sign up for our e-newsletters</legend>
<label for="txtFirstName">First Name:</label> <input type="text" name="txtFirstName" />
<label for="txtSurname">Surname:</label><input type="text" name="txtSurname" />
<label for="txtEmail">Email:</label><input type="text" name="txtEmail" />
<ul>
<li><label for="one">One</label><input type="checkbox" name="AddressBookID"  id="one" value="12345" /></li>
<li><label for="two">Two</label><input type="checkbox" name="AddressBookID" id="two" value="67890" /></li>
0
 

Author Comment

by:Lady_M
ID: 20116041
In case anyone is interested, I got it working using this:
<SCRIPT type=text/javascript>
function validate(form) {
// Validate text boxes
      if (document.signUpForm.txtFirstName.value == "")
      {
            alert ("Please enter your first name");
            document.signUpForm.txtFirstName.focus();
            return false;
      }

      
      if (document.signUpForm.txtSurname.value == "")
      {
            alert ("Please enter your surname");
            document.signUpForm.txtSurname.focus();
            return false;
      }
      if (document.signUpForm.txtEmail.value == "")
      {
            alert ("Please enter your Email");
            document.signUpForm.txtEmail.focus();
            return false;
      }

//Validate checkboxes
      var total=""
      for(var i=0; i < document.signUpForm.AddressBookID.length; i++){
      if(document.signUpForm.AddressBookID[i].checked)
      //output selected values in alert box  checkbox id:value
      total +=document.signUpForm.AddressBookID[i].id  + "  :  "  + document.signUpForm.AddressBookID[i].value + "\n"
      }
      
      if(total=="")
       alert("Please select the emailers you wish to subscribe to")
      else
       alert (total)
      return false;
}
</SCRIPT>



<FORM name=signUpForm onsubmit="return validate(this)" action=file.asp method=post>

example checkbox:
<li><input type="checkbox" name="AddressBookID"  id="fred" value="12345" /><label for="fred">Fred</label></li>
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 20561837
PAQed with points refunded (250)

Computer101
EE Admin
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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

578 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