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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help using URL Variables to write a form field

    Using the url test.html?a=john&b=doe

    I am using the following code:

    <script type="text/javascript" language="javascript">
    function getUrlVars() {
    var map = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi;
    function(m,key,value) { map[key] = value; }); return map;
    }
    var john=getUrlVars(map[a]);
    alert (getUrlVars(map[a]))
    alert (john)
    </script>

    I can't seem to get an alert to show me anything. The eventual result is not an alert, but a document.write to post the url contents to various form fields for firstname= and lastname= etc.

    I am really having a hard time using the document.write function, and am not sure how to employ it, whether in the same script statement or in a separate script in the body.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,243
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    I am afraid that I do not understand your code. alert (getUrlVars(map[a])) attempts to pass a variable to the function getUrlVars() { but the array map is only created by that function.

    document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.


    It is your responsibility to die() if necessary….. - PHP Manual
    Last edited by Philip M; 02-16-2010 at 08:20 PM.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,557 Times in 4,521 Posts
    No idea what that JS is supposed to do. Agree w/ Philip that it's just strange.

    Here's a straightforward implementation:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var qs = location.search.substring(1);
    var pairs = qs.split(/\&/g);
    var qsvalues = [];
    for ( var p = 0; p < pairs.length; ++p )
    {
        var temp = pairs[p].split("=");
        qsvalues[temp[0]] = unescape( temp[1] );
    }
    </script>
    </head>
    <body>
    Your name is <script type="text/javascript">document.write(qsvalues["name"]);</script>
    <br/><br/>
    Your age is  <script type="text/javascript">document.write(qsvalues["age"]);</script>
    </body>
    </html>
    That's expecting a URL of the form
    Code:
         pagename.html?name=bob&age=37
    or similar.

    You could refine it a bit to use a function to get the query string values, so that if no value was passed it comes back with a blank string instead of "undefined". But that's detail work.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    Feb 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How do I use <script type="text/javascript">document.write(qsvalues["email"]);</script>


    in an input statement like: <input name="email" type="hidden" id="email" value="+email+" />

  • #5
    New Coder
    Join Date
    Feb 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I used the following code, but can't seem to get the email to send for the

    <script type="text/javascript">document.write('<input name="fname" type="hidden" id="fname" value="qsvalues[+fname+]"/>'); </script>
    <script type="text/javascript">document.write('<input name="lname" type="hidden" id="lname" value="qsvalues[+lname+]"/>'); </script>
    <script type="text/javascript">document.write('<input name="phone" type="hidden" id="phone" value="qsvalues[+phone+]"/>'); </script>
    <script type="text/javascript">document.write('<input name="email" type="hidden" id="email" value="qsvalues[+email+]"/>'); </script>



    I am using FormMail cgi to send an email, and when I remove the lines above, the email sends with no data, but when I put the code in from above, the email doesn't send.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,557 Times in 4,521 Posts
    I would not try to use document.write.

    Instead, I'd do this:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function getQSinfo( )
    {
        var form = document.theForm; // or whatever the name of your form is
        var qs = location.search.substring(1);
        var pairs = qs.split(/\&/g);
        for ( var p = 0; p < pairs.length; ++p )
        {
            var temp = pairs[p].split("=");
            var fname = temp[0];
            var val = unescape( temp[1] );
            if ( form.elements[fname] != null ) form.elements[fname].value = val;
        }
        // and I guess you want to submit it, too?
        form.submit();
    }
    </script>
    </head>
    <body onload="getQSinfo()">
    <form name="theForm" ...>
    <input name="fname" type="hidden" />
    <input name="lname" type="hidden" />
    <input name="phone" type="hidden" />
    <input name="email" type="hidden" />
    </body>
    </html>
    Untested, but it's simple enough it should work.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    Feb 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Great job! Now, one small remaining question:

    The email portion, that obviously includes an @ symbol, is not coming through. Is there an easy way to fix that?

  • #8
    New Coder
    Join Date
    Feb 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To be more specific, the fname, lname, and phone are all coming through on the submitted email, but the last variable is not included in the email, and the variable name is not shown, so that part looks like it is truncated.

  • #9
    New Coder
    Join Date
    Feb 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Now more info:

    I took out the @ and it still wasn't included, and I tried putting an & at the end of the string, but still didn't get anything...

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,557 Times in 4,521 Posts
    Probably means that the querystring wasn't propery encoded.

    The @ symbol is one of many that need to be URL encoded.

    Dunno what is generating your querystring, but it's got a bug, methinks.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,557 Times in 4,521 Posts
    I just tried my test page using
    Code:
    test.html?email=joe%40bar.com
    and that worked properly.

    Now, to be fair, it also worked using
    Code:
    test.html?email=joe@bar.com
    but now we are into browser dependencies.

    Both of those worked properly in both MSIE 7 and FireFox 3.5
    Last edited by Old Pedant; 02-17-2010 at 01:37 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,557 Times in 4,521 Posts
    Quote Originally Posted by johnrivers View Post
    Now more info:

    I took out the @ and it still wasn't included, and I tried putting an & at the end of the string, but still didn't get anything...
    So show the actual query string that isn't working???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    New Coder
    Join Date
    Feb 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, so I am putting the following as my URL

    test.html?fname="John"&lname="Doe"&phone="5559991111"&email="fred@aol.com"

    And I am receiving:

    ------------------
    fname: "John"

    lname: "Doe"

    phone: "5559991111"

    submit: Submit

    ------------------


    The from address is fred@aol.com

    So I guess that now it is working? I would really like the email to be a field in the text, not the in the from address on the email, but that probably isn't a javascript thing....

  • #14
    New Coder
    Join Date
    Feb 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Also, I would like to use this form script on multiple forms, so have set the form names to be form1, document.theForm1 and document.theForm2, etc. and in the script, i have change it to be:

    <script type="text/javascript">
    function getQSinfo( )
    {
    var form = document.theForm1; // or whatever the name of your form is
    var qs = location.search.substring(1);
    var pairs = qs.split(/\&/g);
    for ( var p = 0; p < pairs.length; ++p )
    {
    var temp = pairs[p].split("=");
    var fname = temp[0];
    var val = unescape( temp[1] );
    if ( form.elements[fname] != null ) form.elements[fname].value = val;
    }
    // and I guess you want to submit it, too?
    // form.submit();
    }

    {
    var form = document.theForm2; // or whatever the name of your form is
    var qs = location.search.substring(1);
    var pairs = qs.split(/\&/g);
    for ( var p = 0; p < pairs.length; ++p )
    {
    var temp = pairs[p].split("=");
    var fname = temp[0];
    var val = unescape( temp[1] );
    if ( form.elements[fname] != null ) form.elements[fname].value = val;
    }
    // and I guess you want to submit it, too?
    // form.submit();
    }
    etc.

    Is this correct?

    My emails from the first script are fine, but the rest the emails come through blank....

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,557 Times in 4,521 Posts
    You are *not* supposed to have quote marks in there.

    I'm surprised it works at all with them there.

    Hmmm...

    But I'm guessing that's not the problem with the email address.

    Would you double check that the name in your <form> really matches the name in the querystring???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

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