Passing input values between pages.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DLP35
    New Member
    • Sep 2007
    • 11

    Passing input values between pages.

    I've been referred here from the HTML forum as I'm told the answer lies in javascript.

    I'm sure this should be simple enough but I'm really struggling to get my mind around this. I don't have access to a server but am developing a site and I need to pass information from input boxes on one form to a paragraph of text on another.

    The simplest example I could think of would be a first page with input boxes for Title and Surname, The pages will be within the same browser session and I would like it to all be client side.

    [HTML]<head>

    <title>Test</title>

    <table>

    <tr>
    <td>Title:</td>
    <td><p>
    <input name="Title" type="text" id="Title" size="20" maxlength="4">
    </p> </td>
    </tr>
    <tr>

    <td>Surname:</td>
    <td><input name="Surname" type="Text" id="Surname" size="35" maxlength="50"> </td>
    </tr>


    <td><input name="Reset" type="Reset"
    value="Clear Form" />
    <a href="Test_Page 2.html">Next</a>
    </body>
    </html>
    [/HTML]

    and a second page which would then show the values in text.

    [HTML]<head>

    <title>Untitl ed Document</title>
    </head>

    <body>
    Hello
    </body>

    </html>
    [/HTML]
    So if I open the first page and put in Mr and Smith the second page reads "Hello Mr Smith"

    What would the javascipt be on the two pages in the example above?

    Thanks.
    Last edited by gits; Sep 2 '07, 01:37 PM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    let me give you an idea:

    you may append your values to the url you want to call ... so that the url looks like:

    Code:
    'Test_Page2.html?value1=val1&value2=val2'
    now on page 2 you use:

    [CODE=javascript]var url = window.location ;[/CODE]
    to retrieve the url and now you have to parse it to get the value out of it. you may use a regEx or js builtin stringoperation s for that.

    are you familiar with javascript? try something and then post back if you have more questions.

    kind regards

    Comment

    • DLP35
      New Member
      • Sep 2007
      • 11

      #3
      No, i'm not used to Javascript and only just getting used to the HTML that web editors create. If I was doing this in MS Access using forms and VB I would be OK to muddle through.

      I know there are a lot of examples on the net but they seem complicated and I'm just not "getting it"! Can we take it a step at a time?

      Are you saying I change the link A href code to something like:

      [HTML]<a href="Test_Page 2.html?Title=va l1&Surname=val2 ">Next</a>[/HTML]

      which carries forward a querystring on the url on the second page - although with the above it contains the text of "Title" and "Suraname" and not the values of Mr & Smith.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        exactly :) ... now we need a javascript that builds the querystring for you ;)

        [CODE=javascript]function goto_page(page) {
        var title = document.getEle mentById('Title ').value;
        var surname = document.getEle mentById('Surna me').value;

        var q_str = '?Title=' + title + '&Surname=' + surname;

        var url = page + q_str;

        window.location = url;
        }
        [/CODE]
        and now your link should look like:

        [HTML]<a href="#" onclick="goto_p age('Test_Page2 .html');">Next</a>[/HTML]
        kind regards

        Comment

        • DLP35
          New Member
          • Sep 2007
          • 11

          #5
          Excellent.

          I googled and found that I had to place the function inside script markers and now my url on page 2 includes the words Mr and Smith.

          sorry, but this could be a slow night for you!

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #6
            heya ...

            you are doing well with the bits i show you ;) ... what we have to do now is to retrieve the url and put it on your page:

            [CODE=javascript]<script type="text/javascript">
            function get_params() {
            var url = window.location .href;
            var q_str_part = url.match(/\?(.+)$/)[1];

            var val_pairs = q_str_part.spli t('&');
            var params = {};

            for (var i = 0; i < val_pairs.lengt h; i++) {
            var tmp = val_pairs[i].split('=');
            params[tmp[0]] = typeof tmp[2] != 'undefined' ? tmp[2] : '';
            }

            return params;
            }

            function write_params() {
            var params = get_params();

            var txt = ' ';

            for (var i in params) {
            txt += params[i] + ' ';
            }

            var body = document.getEle mentsByTagName( 'body')[0];
            body.innerHTML += txt;
            }
            </script>
            [/CODE]

            call the write_params()-function in onload of your page2-body :)

            kind regards

            Comment

            • DLP35
              New Member
              • Sep 2007
              • 11

              #7
              Hi thanks again gits.

              I'm not there yet, am I right putting the text e.g the word hello in the function?
              I found the body onload option and this seems to reference back ok but doesn't pick up the parameters, so i think I'm still missing something (apart from brain cells!)

              [CODE=html]<html>
              <head>
              <title>Untitl ed Document</title>

              <script type="text/javascript">
              function get_params() {
              var url = window.location .href;
              var q_str_part = url.match(/\?(.+)$/)[1];

              var val_pairs = q_str_part.spli t('&');
              var params = {};

              for (var i = 0; i < val_pairs.lengt h; i++) {
              var tmp = val_pairs[i].split('=');
              params[tmp[0]] = typeof tmp[2] != 'undefined' ? tmp[2] : '';
              }

              return params;
              }

              function write_params() {
              var params = get_params();

              var txt = 'Hello ';

              for (var i in params) {
              txt += params[i] + ' ';
              }

              var body = document.getEle mentsByTagName( 'body')[0];
              body.innerHTML += txt;
              }
              </script>
              </head>

              <body onLoad="write_p arams()">

              </body>

              </html>
              [/CODE]

              Cheers
              Last edited by gits; Sep 2 '07, 09:11 PM. Reason: fix code tags

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5388

                #8
                ahrg, sorry my bad ... replace the params line with the following:

                [CODE=javascript]params[tmp[0]] = typeof tmp[1] != 'undefined' ? tmp[1] : '';[/CODE]
                kind regards

                ps: you note the difference? please tell me ... because i think you are doing well and learn quickly :)

                Comment

                • DLP35
                  New Member
                  • Sep 2007
                  • 11

                  #9
                  Thanks gits for all your help. Got it now! at least to a stage where I can try some different variations on fields etc.

                  The next step for me to learn is "to pass input values or parameters to a client side cookie and then extract them for use in other pages or send them to a server later". Is this still javascipt or am I back to HTML? and do I need to start a new thread?

                  Any suggestions or good (easy) links where i can start.

                  Cheers

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5388

                    #10
                    hi :)

                    it stays javascript ... ;) and may be to start with cookies have a look here

                    kind regards

                    Comment

                    • DLP35
                      New Member
                      • Sep 2007
                      • 11

                      #11
                      Thanks for the Cookies link.

                      One last question on parameters - if i wanted to just call the parameters directly into the <Body> as part of a block of text how would i do this?

                      eg "Thank you for calling Mr Smith, I hope that we will talk again". Where Mr is called from the Title parameter and Smith from the Surname parameter.

                      to replace the concatenation I would do in MS Access. e.g.
                      "Thank you for calling " & [Title]& " " & [Surname] & ", I hope that we will talk again".

                      Regards
                      Last edited by DLP35; Sep 4 '07, 08:57 PM. Reason: typo

                      Comment

                      • gits
                        Recognized Expert Moderator Expert
                        • May 2007
                        • 5388

                        #12
                        hi ...

                        we have the params-read method already ... ok? we call it onload and put it in the body already ... ok? ... now we want to put it at a specific position in a floating text ... right?

                        do something like the following:

                        [HTML]<body onload="write_p arams();">
                        Thank you for calling <span id="title"></span> <span id="surname"></span>, I hope that we will talk again
                        </body>[/HTML]

                        in our write_params function you need:

                        [CODE=javascript]var title_container = document.getEle mentById('title ');
                        var surname_contain er = document.getEle mentById('surna me');
                        [/CODE]
                        and you have to assign the parameters we already have as innerHTML to the appropriate container we refered the above way :)

                        kind regards

                        Comment

                        • DLP35
                          New Member
                          • Sep 2007
                          • 11

                          #13
                          Hi gits.

                          Nope, thought I was doing OK but you lost me with:

                          "and you have to assign the parameters we already have as innerHTML to the appropriate container we refered the above way :)"

                          Comment

                          • gits
                            Recognized Expert Moderator Expert
                            • May 2007
                            • 5388

                            #14
                            hi ...

                            ok ... we have our two span containers ... right. you have to put that into the 'write_params() '-method ... and now we have our params to assign to the particular container. we don't need the loop we have there already ... delete it, and put something like the following there:

                            [CODE=javascript]title_container .innerHTML = params.Title;[/CODE]

                            kind regards

                            Comment

                            • DLP35
                              New Member
                              • Sep 2007
                              • 11

                              #15
                              Thanks for your help, but I find your answers are becoming cryptic, "put something like this in somewhere!" doesn't help.

                              I would have been able to learn from this by putting the principle from this "simple" query into practice in my actual web pages - but this is way too slow a process for me. I'll pay for someone to do it!

                              Please consider this thread closed.

                              Comment

                              Working...