Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    parse parts of a URL and insert into form fields

    Hello all,

    I am sending out a small email blast to my clients. I would like to offer them a free download of a report, but I don't want to have them re-fill all of their information. Therefore, I would like to have someone help me out with a bit of code that will parse the URL and place the separated items into the form fields. Then they can hit submit and be done.

    I have the following URL:

    http://www.test.com/land.html?id=tes...&city=pasadena

    I would like to have a form with the following fields:
    -id
    -first
    -last
    -city

    Once a person has landed on the page, the forms will automatically fill out and they will hit submit.

    This is what I have so far...but it is not parsing correctly. Can anybody help me out to add in the right code?


    Code:
    <HTML>
    <HEAD>
    <SCRIPT type="text/javascript">
    function getQuerystrings(inputId) {
    
    var urlString = parent.document.URL;
    var queryString = urlString.substring(urlString.indexOf('?'),urlString.length);
    
    inputObj = document.getElementById(inputId);
    inputObj.value = queryString;
    }
    </SCRIPT>
    </HEAD>
    <BODY onload="getQuerystrings('urlQuery')">
    <form id="form1">
    <input id="urlQuery" type="text" value="">
    <input id="urlQuery" type="text" value="">
    <input type="button" value="Submit">
    </form>
    </BODY>
    </HTML>
    Thanks

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    Code:
    function processQueryString( )
    {
        var qs = location.search.substring(1); // search is all of URL from ? onwards...so zap the ?
        var pairs = qs.split("&")
        for ( var p = 0; p < pairs.length; ++p )
        {
            var temp = pairs[p].split("=");
            var name = temp[0];
            var value = unescape( temp[1] ); // unencode, actually
            var formfield = document.form1.elements[name];
            if ( formfield != null ) 
            {
                // if a field by that name actually exists...
                formfield.value = value; // then shove in the value
            }
        }
    }
    Something like that, I would think.

    Note that it is POINTLESS to give an ID to a form field. You MUST give it a name if you want its value to be seen on the page that the <form> posts to (either GET or POST). And so since you must name it, why bother with the ID? And while you *can* give an ID to a <form>, it's more sensible to give it a name, so that you don't need to use document.getElementById.

    Also, you can *NOT* give the same ID to more than one object. [Well, you can, but it then needs special handling and getElementById will NOT work.]

    Finally, an <input type=button> will *NOT* submit a <form> unless you use javascript for the onclick event to do it. You need to use <input type=submit>.

    In short, start over.

    So your <form> should have looked like:
    Code:
    <form name="form1">
    <input name="id" type="text" value="">
    <input name="first" type="text" value="">
    <input name="last" type="text" value="">
    <input name="city" type="text" value="">
    <input type="submit" value="Submit">
    </form>

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    By the by, this would be better done with a server-side script. PHP or ASP or JSP or ASP.NET or whatever. Easier to do better error checking. Not necessary, but a good idea.

  • #4
    New Coder
    Join Date
    Aug 2007
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks Old Pedant - but I still have issues

    I know that it would be better off using PHP, but I do not have that option.

    I do however need this to work within a straight HTML environment...nothing fancy, I am just trying to capture the people's info in the link and save them the hassle of filling out the form. I am sending an email out based on the Salesforce database that I have.

    As for using "id" that was an overlook on my part. The fields that I will have are; first, last, email, company, telephone.

    Anyways, I appreciate your help, but the code that you provided does not populate the fields upon entry...any help would be greatly appreciated.

  • #5
    New Coder
    Join Date
    Aug 2007
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    got it to work

    I had to include an "onload" event.

    This is great. Thanks for your help.


  •  

    Posting Permissions

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