www.webdeveloper.com
Results 1 to 10 of 10

Thread: Parse the query string

  1. #1
    Join Date
    Jun 2014
    Posts
    10

    Parse the query string

    Hello,

    I'm trying to parse the querystring but not having much luck, tried several methods to no avail. Can anyone see what I'm doing wrong? Here is the querystring I'm trying to parse.

    file:///C:/Program%20Files%20(x86)/Apache%20Group/Apache2/cgi-bin/popup.html?TxtName=Rick&Organization=GSD+AMS&Organization1=&Sponser=Mike+Eberhardt&Sponser1=&reason= IDSS+Sales+Training&reason1=&Country=840

    and this is the page I call:

    <Code>
    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    function getValue()
    {
    // alert("hello");
    // First, we load the URL into a variable
    // write.document("Made it in");
    //var url = window.location.href;
    //write.document(url);
    var query = (window.location.search || '?').substr(1),
    map = {};
    query.replace(/([^&=]+)=?([^&]*)(?:&+|$)/g, function(match, key, value) {
    (map[key] = map[key] || []).push(value);
    });
    write.document(map);
    return map;


    }
    </script>
    <Center>

    <body>
    <script>
    window.onload=getValue();
    </script>
    <h1>Terms and Conditions</h1>
    <p>Bags are subject to search upon entrance and exit.</p>
    <p>Photo ID is Required.</p>
    <p>You must be escorted at all times.</p>
    <FORM ACTION="http://localhost:80/cgi-bin/sqlconfig.pl" METHOD="POST">
    <td><INPUT STYLE="font-size:20px" TYPE="SUBMIT" VALUE="I accept" align="center" ></td>
    </head>
    </body>
    </html>
    </code>


    I'm trying to get:
    name
    organization
    organization1
    sponser
    sponser1
    reason
    reason1
    country

    off the query string.

    Thanks
    Rick

  2. #2
    Join Date
    Jun 2014
    Posts
    27
    Here is one option for you Try this function.

    function parseQuery(qstr)
    {
    var query = {};
    var a = qstr.split('&');
    for (var i in a)
    {
    var b = a[i].split('=');
    query[decodeURIComponent(b[0])] = decodeURIComponent(b[1]);
    }

    return query;
    }

  3. #3
    Join Date
    May 2014
    Posts
    915
    @shophiarajan47's advice is good -- though it's a bad idea to use for (var in object) on an array. (it's also slower), and I'd probably put a LIMIT on the split inside the loop... and the VAR inside the loop would cause some unnecessary memory thrashing. You also don't have to say var on every blasted line...

    Code:
    function parseQuery(qstr) {
    	var
    		result = {},
    		a = qstr.split('&'),
    		l = a.length,
    		b, i;
    	for (i = 0; i < l; i++) {
    		b = a[i].split('=', 1);
    		result[decodeURIComponent(b[0])] = decodeURIComponent(b[1]);
    	}
    	return result;
    }
    Same general idea, but should run faster/more efficiently.
    Java is to JavaScript as Ham is to Hamburger.

  4. #4
    Join Date
    Jun 2014
    Posts
    10
    Thanks shophiarajan47 and Deathshadow,

    I think I figured out what you're doing so I did it manually to see if I was right.

    <code>
    <html>
    <head>
    <Center>
    <script type="text/javascript">
    function parseQuery()
    {
    var myurl = window.location.href;
    return (myurl);
    }
    </script>

    <body>
    <script>
    var myurl=window.onload=parseQuery();
    var qs = myurl.split('?')[1];

    var rawTxtName = qs.split('&')[0];
    var TxtName = rawTxtName.split('=')[1];
    var TxtName = TxtName.replace(/\+/g, ' ');

    var rawOrganization = qs.split('&')[1];
    var Organization = rawOrganization.split('=')[1];
    var Organization = Organization.replace(/\+/g, ' ');

    var rawOrganization1 = qs.split('&')[2];
    var Organization1 = rawOrganization1.split('=')[1];

    var rawSponser = qs.split('&')[3];
    var Sponser = rawSponser.split('=')[1];
    var Sponser = Sponser.replace(/\+/g, ' ');

    var rawSponser1 = qs.split('&')[4];
    var Sponser1 = rawSponser1.split('=')[1];
    var Sponser1 = Sponser1.replace(/\+/g, ' ');

    var rawResponse = qs.split('&')[5];
    var Response = rawResponse.split('=')[1];
    var Response = Response.replace(/\+/g, ' ');

    var rawResponse1 = qs.split('&')[6];
    var Response1 = rawResponse1.split('=')[1];
    var Response1 = Response1.replace(/\+/g, ' ');

    var rawCountry = qs.split('&')[7];
    var Country = rawCountry.split('=')[1];
    var Country = Country.replace(/\+/g, ' ');
    </script>

    <p id="TxtName"></p>
    document.getElementById("TxtName").innerHTML = TxtName;










    <h1>Terms and Conditions</h1>
    <p>Bags are subject to search upon entrance and exit.</p>
    <p>Photo ID is Required.</p>
    <p>You must be escorted at all times.</p>
    <FORM ACTION="http://localhost:80/cgi-bin/sqlconfig.pl" METHOD="POST">
    <td><INPUT STYLE="font-size:20px" TYPE="SUBMIT" VALUE="I accept" align="center" ></td>
    </body>
    </html>
    </code>


    My question is now, I have to pass this information to my perl script, I'm using
    <code>
    <p id="TxtName"></p>
    document.getElementById("TxtName").innerHTML = TxtName;
    </code>

    and its not working, what am I doing wrong? (BTW I know my code isn't indented, the indention got lost in the cut and paste)

    Thanks
    Rick

  5. #5
    Join Date
    May 2014
    Posts
    915
    PERL runs server side, how are you passing it from client-side to server-side? I think you're not quite grasping what JavaScript is, or not grasping what PERL is, or just not grasping the separation of client from server... I'm assuming you'd want to pass it as a value in the FORM, in which case you should be assigning it to INPUT[hidden].

    Of course if it's in the requested URL of the page, that should probably either have been stored server-side and not sent client side in the first place, or dumped into hidden inputs with no JavaScript involved in the first place.

    Again the unwritten rule of JavaScript: If you can't make a page that works without scripting FIRST, you likely have no business adding scripting to it.

    Starting to sound like you're using JavaScript for something that shouldn't even HAVE scripting involved.
    Java is to JavaScript as Ham is to Hamburger.

  6. #6
    Join Date
    Jun 2014
    Posts
    10
    Deathshadow,

    Thanks again for your help. I'm simply passing info from the first page to the second page via the query string. I want to access this information on the second page I collected on the first page. My perl script worked just fine accessing this info on the first page but my boss wanted me to popup a window warning visitors that they are subject to search upon exit. I want to use the same method in my perl script accessing the information from the second page. I don't mind putting hidden fields on the second page, but I can't seem to populate them with my javascript variables. I have tried several methods and nothing seems to work.

    Any help would be appreciated
    Thanks
    Rick

    (BTW I'm a AIX and San (30 years) administrator that got tasked with this so I'm learning as I go! Sorry if I'm asking stupid questions)

  7. #7
    Join Date
    May 2014
    Posts
    915
    If all you're trying to do is a popup when the first page's form is submit, I'd suggest using javascript's "confirm" message.

    Opening an ACTUAL new window for a confirmation is bad practice -- if you make it an ACTUAL new window, cross communication is hard and what are you doing with the original window? The real choices is to either fake a new window with a DIV manipulated by JS, or to use "confirm".

    In your original FORM tag, just put:

    onsubmit="return confirm('Your confirmation message goes here');"

    Changing out that center string as needed. Normally I don't advocate the onxxx attributes, but for something this small and simple there's no reason to get all crazy with it... unless you need to style it. No extra window/page needed if all you're doing is a yes/no accept/decline type message on a form submit.

    This recent thread:
    http://www.webdeveloper.com/forum/sh...19#post1332219

    Discusses the more complex "make a DIV" approach, if you need to style it to match your page or have more complex content than plaintext.

    Actually doing a page-load to a new window just for a confirmation? NOT how it's done.

    Also, if this is something that warrants a confirmation dialog, I sure as shine-ola wouldn't be passing it via GET.
    Java is to JavaScript as Ham is to Hamburger.

  8. #8
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,348
    recut01, could you please wrap your code elements in the appropriate forum tags to help people help you.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  9. #9
    Join Date
    Jun 2014
    Posts
    10
    \\.\ I tried, I put <code></code> around it, I must be doing something wrong. where do I put these forum tags?

    Rick

  10. #10
    Join Date
    Jun 2014
    Posts
    10
    Deathshadow,

    Thank you I will give that a try, your guidance has been most helpful and very appreciated.

    Rick

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles