Home > Technical & Creative Skills > Programming, Servers & Scripts

Simple JS Variable Question (4)


02-11-2012 04:21 PM #1 profitable ()
Simple JS Variable Question

Hi,
I have a form that when submitted opens 2 windows. One through the form action and another through javascript. It's the standard auto insurance site functionality you see being done everywhere.

I'm attempting to pass a few variables (age, zipcode, insured) from the form with the javascript. The form's action opens url1.php and the js opens url2.php.

The form passes all the data fine to url1.php but the javascript can only pick up one variable from the form. In the code below, it picks up age but does not pick up zipCode. Is there a way to pick up multiple vars? (I'll actually need to pass all 3).

Below is the JS code and the form. Thanks!

Javascript:

Code:
<script type='text/javascript'>
        $(document).ready(function () {
            $("#auto-form").submit(function () {
                var zipCode = $('#qb-zipcode');
				var age = $('#qb-age');
                if (/\b[0-9]{5}(?:-[0-9]{4})?\b/.test(zipCode.val())) {
                    window.open("url2.php?src=<?php echo $_GET['src']; ?>&ZipCode=+zipCode.val();&age="+age.val());
                    return true;
                }
                else {
                    alert("Please enter a valid zip code.");
                    zipCode.focus();
                    return false;
                }
            });
        });
        $(window).load(function () { $('#qb-zipcode').focus(); });
    </script>
Form:
Code:
        <form method="get" accept-charset="utf-8" id="auto-form" action="url1.php">
        <input type='hidden' name='src' value="<?php echo $_GET['src']; ?>">
          <div id="qb-one">
            <label for="qb-zipcode">Label 1 Copy</label>
            <input type="text" name="ZipCode" value="" id="qb-zipcode" />
          </div>
          <div id="qb-two">
            <label for="qb-insured">Label 2 Copy</label>
            <select name="insured" id="qb-insured">
              <option value="yes">Yes</option>
              <option value="no">No</option>
            </select>
          </div>
          <div id="qb-three">
            <label for="qb-age">Label 3 Copy:</label>
            <input type="text" name="age" value="" id="qb-age" />
          </div>
          <input type="submit" name="submit" value="" id="qb-submit5" />
        </form>


02-11-2012 06:51 PM #2 jmgraff (Member)

Your quotes in the window.open function are screwed up..

Code:
window.open("url2.php?src=<?php echo $_GET['src']; ?>&ZipCode=+zipCode.val();&age="+age.val());
should be

Code:
window.open("url2.php?src=<?php echo $_GET['src']; ?>&ZipCode="+zipCode.val()+"&age="+age.val());


02-11-2012 06:59 PM #3 profitable ()

thanks jmgraff - worked perfectly.


02-11-2012 07:18 PM #4 jmgraff (Member)

no problem. also, i'd recommend using notepad++ or some other editor with syntax highlighting for html/javascript/php stuff, it'll make simple mistakes stand out better

http://notepad-plus-plus.org/


Home > Technical & Creative Skills > Programming, Servers & Scripts