SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot shiftless's Avatar
    Join Date
    Mar 2005
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form submission/redirect not working

    I have a page with a form. One function checks that all fields are filled out and another checks the email field syntax. These functions work in regards to finding missing info or bad email syntax, but now it seems it's not getting submitted when you click the submit button and the redirect isn't working either (also it clears the form when you click the button). Can anyone tell me why? This is one of my first forays into js.

    the page can be see/tested here:

    http://www.clubspaces.com/signuptest.htm

    here is the relevant javascript:

    Code:
    function emailCheck(emailStr) {
    
    // checks if the e-mail address is valid
    var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
    var matchArray = emailStr.match(emailPat);
    if (matchArray == null) {
    alert("Your email address seems incorrect.  Please try again (check the '@' and '.'s in the email address)");
    return false;
    }
    // make sure the IP address domain is valid
    var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
    if (IPArray != null) {
    for (var i=1;i<=4;i++) {
    if (IPArray[i]>255) {
    alert("Your email destination IP address is invalid!");
    return false;
          }
       }
    }
    return true;
    }
    
    function validate_form() {
    
    var message="Please complete the following: \n------------------------------------------------\n";
    var noerrors=message;
    
    if (document.getElementById('template').value == "") {
    	message=message + "Please choose a template for your site! \n";
    }
    if (document.getElementById('clubtype').value == "") {
    	message=message + "Please tell us what type of club you have! \n";
    }
    if (document.signup.company.value == "") {
    	message=message + "Please choose a name for your club! \n";
    }
    if (document.signup.first_name.value == "") {
    	message=message + "Please fill in your first name! \n";
    }
    if (document.signup.last_name.value == "") {
    	message=message + "Please fill in your last name! \n";
    }
    if (document.signup.email.value == "") {
    	message=message + "You must provide us with an email address! \n";
    }
    if (document.signup.phone.value == "") {
    	message=message + "Please fill in your phone number! \n";
    }
    if (document.signup.city.value == "") {
    	message=message + "Please fill in your city of residence! \n";
    }
    if (document.signup.state.value == "") {
    	message=message + "Please fill in your state of residence! \n";
    }
    if (document.signup.country.value == "") {
    	message=message + "Please fill in your country of residence! \n";
    }
    if (document.getElementById('numbers').value == "") {
    	message=message + "Please fill in the number of members you have! \n";
    }
    if (document.getElementById('refer').value == "") {
    	message=message + "Please tell us how you found us! \n";
    }
    if (!document.signup.tac.checked){
    	message=message + "You must agree to the Terms and Conditions before proceeding! \n";
    } 
    if(message==noerrors){
    return true;	
    }
    else{
    alert(message);	
    return false;
    }
    }
    
    function OnSubmitCheck() {
    var one = emailCheck(emailStr); // change functionOne() to onsubmit function name
    var two = validate_form(); // change functionTwo() to onsubmit function name
    if((one == false) || (two == false)) { return false; }
    return true;
    }
    and this is what I have in the opening form tags:

    Code:
    <form name="signup" method="post" action="" onsubmit="return OnSubmitCheck();">
     <input type="hidden" value="http://www.clubspaces.com/welcome.htm" name="redirect" />

  2. #2
    SitePoint Member
    Join Date
    Jan 2007
    Location
    Cologne, Germany
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the function you call onsubmit you have
    Code:
    emailCheck(emailStr); //line 92
    where emailStr is not defined. Try changing it to
    Code:
    emailCheck(document.getElementById("email").value); //retrieve the value from the email field  to check

  3. #3
    SitePoint Zealot shiftless's Avatar
    Join Date
    Mar 2005
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    didn't seem to work

    not sure if i implemented your idea correctly, but in any case it didn't work. did i miss something? it still clears the form when you hot submit, but no alert window is triggered. is it because i have removed the action from the form? I didn't want to send a zillion test emails to my client via their form service, so I took the action out of this testing page. Does a redirect only work if there's an action?

  4. #4
    SitePoint Zealot shiftless's Avatar
    Join Date
    Mar 2005
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    changed code

    I have changed the onSubmit in the form to be:

    Code:
    onsubmit="emailCheck(this.email.value) && validate_form(); return false"
    Now, if I just call one or the other of the functions, it works, and redirects (and catches errors as per whichever function it is I call), but adding two functions as I have it above breaks it and it doesnt work and the redirect doesn't work. I am tearing my hair out here and could really use some help.

  5. #5
    SitePoint Zealot shiftless's Avatar
    Join Date
    Mar 2005
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone? please?!?

  6. #6
    SitePoint Zealot shiftless's Avatar
    Join Date
    Mar 2005
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    the solution

    well I finally figured this out and here is the solution for anyone searching on this topic:

    my functions were written just fine. You can call 2 functions on the onSubmit like this:
    Code:
    onsubmit="return (emailCheck(this.email.value) && validate_form()); return false"
    Theoretically you could also use the function I wrote that checks for a false from each of the original functions, ie. this part:

    Code:
    function OnSubmitCheck() {
    var one = emailCheck(emailStr); // change functionOne() to onsubmit function name
    var two = validate_form(); // change functionTwo() to onsubmit function name
    if((one == false) || (two == false)) { return false; }
    return true;
    }
    and call only one function in the onSubmit, but the thing works now and I need to get away from this project for a while... Maybe I'll test it out another day.

    The other problem, was the redirect not working, and this was because:

    1. I had taken the action out of the form for testing, so the form info was not getting submitted (posted) and so nothing came back from a server with the redirect information.

    2. The 3rd party service that was getting posted to required that the redirect be called "retURL" and not "redirect". Once I changed it to "retURL", it knew what that info was and what to do with it and the redirect started working properly.

    lasltly for anay new-to-javascript people here, my function for checking blank fields is most likely way too cumbersome and could and should be simplified a great deal, don't follow my example! he he


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •