Results 1 to 10 of 10

Thread: Parse the query string

  1. #1
    Join Date
    Jun 2014

    Parse the query string


    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:

    <!DOCTYPE html>
    <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;
    var query = (window.location.search || '?').substr(1),
    map = {};
    query.replace(/([^&=]+)=?([^&]*)(?:&+|$)/g, function(match, key, value) {
    (map[key] = map[key] || []).push(value);
    return map;


    <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>

    I'm trying to get:

    off the query string.


  2. #2
    Join Date
    Jun 2014
    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
    @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...

    function parseQuery(qstr) {
    		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.

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

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

    <script type="text/javascript">
    function parseQuery()
    var myurl = window.location.href;
    return (myurl);

    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, ' ');

    <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>

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

    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)


  5. #5
    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.

  6. #6
    Join Date
    Jun 2014

    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

    (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
    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:

    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.

  8. #8
    Join Date
    Mar 2007
    recut01, could you please wrap your code elements in the appropriate forum tags to help people help you.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

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


  10. #10
    Join Date
    Jun 2014

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


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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.20204 seconds
  • Memory Usage 2,951KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (10)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates