Got JS but need to add a redirect to it. Please help!

14 replies
I need to add a redirect to this Javascript code I have.

It's basically a zip code form submit where the user types in their zip code then hits submit.

However, after they hit submit I would like to redirect them to another page.

I've included the code below:

HEADER:

<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Brian Swalwell -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! JavaScript Source: Free JavaScripts, Tutorials, Example Code, Reference, Resources, and Help -->

<!-- Begin
function validateZIP(field) {
var valid = "0123456789-";
var hyphencount = 0;

if (field.length!=5 && field.length!=10) {
alert("Please enter your 5 digit or 5 digit+4 zip code.");
return false;
}
for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
alert("Invalid characters in your zip code. Please try again.");
return false;
}
if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.

Please try again.");
return false;
}
}
return true;
}
// End -->
</script>


BODY:

<center>
<form name=zip onSubmit="return validateZIP(this.zip.value)">
Zip Code: <input type=text size=20 name=zip>
<input type=submit value="Submit">
</form>
</center>

Could someone tell me where to put the URL I want to redirect the user to?

And if possible, is there a way to take the zip code they submitted and forward it to the new URL?

So it would say something like:

Your zip code (77120) is still available. Act fast!

Thanks.
#add #redirect
  • Profile picture of the author lisag
    You need to do this in Jquery and PHP, ASP, or whatever you're favorite scripting language is.

    Essentially, you accept and validate the zip code, pass it to your script that check for availability. If available, it returns a message and the link. Otherwise it returns a "Sorry" message.

    jquery.com/
    Signature

    -- Lisa G

    {{ DiscussionBoard.errors[1874830].message }}
    • Profile picture of the author aaallday2010
      Originally Posted by lisag View Post

      You need to do this in Jquery and PHP, ASP, or whatever you're favorite scripting language is.

      Essentially, you accept and validate the zip code, pass it to your script that check for availability. If available, it returns a message and the link. Otherwise it returns a "Sorry" message.

      jquery.com/
      Thanks.

      Do you know if jquery works if the visitor has javascript turned off?

      I also took a look at the website. Looks complicated. But I'll see what I can do.
      {{ DiscussionBoard.errors[1875125].message }}
      • Profile picture of the author lisag
        Originally Posted by aaallday2010 View Post

        Thanks.

        Do you know if jquery works if the visitor has javascript turned off?

        I also took a look at the website. Looks complicated. But I'll see what I can do.
        There are plenty of examples on the 'net. I wouldn't be surprised if you found an example that does exactly what you want. Google creatively, or just hire a coder. It's not a terribly complicated process for anyone who does it for a living.

        Jquery does not work if JavaScript is turned off, but a good coder will code defensively for that small percentage of people (mostly corporate surfers whose IT departments have banned JS) who do not have JS enabled.
        Signature

        -- Lisa G

        {{ DiscussionBoard.errors[1875164].message }}
        • Profile picture of the author aaallday2010
          Originally Posted by lisag View Post

          There are plenty of examples on the 'net. I wouldn't be surprised if you found an example that does exactly what you want. Google creatively, or just hire a coder. It's not a terribly complicated process for anyone who does it for a living.

          Jquery does not work if JavaScript is turned off, but a good coder will code defensively for that small percentage of people (mostly corporate surfers whose IT departments have banned JS) who do not have JS enabled.
          I actually did google this before I came here. But still came up with nothing. Actually, there was something that looked like it might work, but I'm an idiot when it comes to coding.

          I guess it's off to elance.

          Thanks again. You've been a great help.
          {{ DiscussionBoard.errors[1875259].message }}
  • Profile picture of the author aaallday2010
    By the way... does anyone know what would be a fair price to pay for something like this. I'm more than willing to pay for a coder, but don't want to over pay.

    I mean, on my end this looks really complicated to do, but to someone familiar with this it should be a fairly simple task.

    Any suggestions?
    {{ DiscussionBoard.errors[1875295].message }}
    • Profile picture of the author lisag
      Originally Posted by aaallday2010 View Post

      By the way... does anyone know what would be a fair price to pay for something like this. I'm more than willing to pay for a coder, but don't want to over pay.

      I mean, on my end this looks really complicated to do, but to someone familiar with this it should be a fairly simple task.

      Any suggestions?
      Riddle me this:

      A. What is the underlying programming language?
      B. What is the database used, or is the zip code not stored in a database.

      C. Is this the process?

      1. Visitor enter Zip code.
      2. Routine validates entry for correct format
      3. Search is execuites to see if zip code is available.
      4. If YES ->Redirect to where.
      5. If NO, display Sorry message.
      Signature

      -- Lisa G

      {{ DiscussionBoard.errors[1876437].message }}
    • Profile picture of the author jminkler
      Originally Posted by aaallday2010 View Post

      By the way... does anyone know what would be a fair price to pay for something like this. I'm more than willing to pay for a coder, but don't want to over pay.

      I mean, on my end this looks really complicated to do, but to someone familiar with this it should be a fairly simple task.

      Any suggestions?
      I would do it for $20
      {{ DiscussionBoard.errors[1877186].message }}
  • Profile picture of the author wayfarer
    I think this is being made much more complicated than it really is. The above JavaScript code is nothing but a validation script. It returns false (doesn't allow the form to submit) if the information is not valid, and returns true (allows the form to function normally) if the the information is valid.

    To "redirect" the form when it's been submitted, all you need to do is modify the form's action attribute:

    HTML Code:
    <form action="/path/to/redirect/to.php" method="post">
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[1876864].message }}
    • Profile picture of the author jminkler
      Originally Posted by wayfarer View Post

      I think this is being made much more complicated than it really is. The above JavaScript code is nothing but a validation script. It returns false (doesn't allow the form to submit) if the information is not valid, and returns true (allows the form to function normally) if the the information is valid.

      To "redirect" the form when it's been submitted, all you need to do is modify the form's action attribute:

      HTML Code:
      <form action="/path/to/redirect/to.php" method="post">
      Wayfarer is partially correct. What you want to do is ...

      1) in your onsubmit .. add ; return false;

      HTML Code:
      <form name=zip onSubmit="return validateZIP(this.zip.value); return false;">
      Zip Code: <input type=text size=20 name=zip>
      <input type=submit value="Submit">
      </form>
      </center>
      This will make the form not post ..

      2) Wrap the validation function around a new function processZip() and give the input an id of 'zip'

      HTML Code:
      <form name=zip onSubmit="return processZip(validateZIP(this.zip.value)); return false;">
       Zip Code: <input type=text size=20 name=zip id=zip>
       <input type=submit value="Submit">
       </form>
       </center>
      3) Create a new function in your script tag called processZip

      HTML Code:
      function processZip(isValid){
         if(isValid){
             window.location = 'http://example.com/form_submit.php?zipcode=' + $('#id').value
         } else {
             .. show not valid message ... 
         }
      
      }
      You don't want to show a message that it's valid before they are sent to a new page, then you need to setup a lot more code than is needed ...
      {{ DiscussionBoard.errors[1877179].message }}
  • Profile picture of the author imarketstuff
    here is a link from googly with an example of what you what can be done.

    it's quite simple, one line of code and u can append parameters as well:

    Javascript Tutorial - Redirect

    where you have (return true), you can replace that with the window.location statement, appended with any extra parameters if you want to pass them to a new script.

    peace
    Signature
    I MARKET STUFF

    {{ DiscussionBoard.errors[1876915].message }}
  • Profile picture of the author wayfarer
    Originally Posted by jminkler

    <form name=zip onSubmit="return validateZIP(this.zip.value); return false;">

    This is totally wrong. It's returning the value of the validateZIP function, and can't subsequently return false also. The validateZIP is already returning true or false.

    Of course the other side of this whole equation is what are you going to DO with the data once it has been posted to the page we're "redirecting" to. For that, you'll need any server side code, PHP ASP, etc.
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[1877530].message }}
    • Profile picture of the author jminkler
      Originally Posted by wayfarer View Post


      This is totally wrong. It's returning the value of the validateZIP function, and can't subsequently return false also. The validateZIP is already returning true or false.

      Of course the other side of this whole equation is what are you going to DO with the data once it has been posted to the page we're "redirecting" to. For that, you'll need any server side code, PHP ASP, etc.
      If you look, processZip does not return anything. the value of validatezip is passed into the process zip function. Read the whole post, I was only breaking it down into simple steps.
      {{ DiscussionBoard.errors[1877654].message }}
  • Profile picture of the author wayfarer
    Originally Posted by jminkler View Post

    If you look, processZip does not return anything.
    Didn't say anything about processZip, which isn't needed anyway. The onsubmit is returning the result of the function validateZIP, which already exists and appears to work.

    Look at what the code you posted says, the same as the original post, except you added return false, which is not needed.

    <form name=zip onSubmit="return validateZIP(this.zip.value); return false;">

    Originally Posted by jminkler View Post

    This will make the form not post ..
    No, it won't. Returning false after returning validateZIP will have no effect. Any return will cause a function to exit. The onsubmit assignment, even inline like this, is in fact a form of a function. It cannot return twice.

    Your processZip function isn't needed because validateZIP already works. If you read it, you'll see it does more than return true or false, it reports errors and everything that yours is trying to do. All that is needed is to give an action attribute to the form, as I originally said, then process the resulting data on that page. Nothing else is required. The only thing left is some server side programming to determine what to do with the resulting data.

    The resulting page could then say, for example:
    Originally Posted by aaallday2010

    Your zip code (77120) is still available. Act fast!
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[1878676].message }}
  • Profile picture of the author aaallday2010
    Thanks guys, got someone from rent a coder to do it for me. We'll see how it turns out.
    {{ DiscussionBoard.errors[1878773].message }}

Trending Topics