Help with passing form values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stylishjm
    New Member
    • Jan 2010
    • 27

    Help with passing form values

    Im trying to send 3 bits of information to another page but I dont know how to do it.

    First page:


    In the first page, the user types in postcodes in two different form text boxes and upon submission the price is displayed below along with the route being plotted on a google map to the left as well as hiding the "calcualate price button" and showing the "confirm & book" button. Once the button is clicked it takes me to booking2.html with some paramaters at the end. An example is shown below



    One problem is that results2=result s2, (im assuming) there should be a price shown there as in the page quote3999.html, <p id="results2"> shows the price and I was advised to create a hidden input with the same id so that it would pass the name/value along with the addresses.
    Code:
    					<p id="results"></p>
     <p id="results2"></p>  <p id="results3"></p>
    
     <input name="price" id="results2" type="hidden" />
    Heres the script someone made for me which brings (some of) the results to booking2.html. Please note that quote is the name of the form that contains everything.

    Code:
    	function showLocation() {
    	if(document.getElementById('quote').innerHTML!='') { // need to wait for the result from gmaps
        document.forms[0].onsubmit = null;
        document.forms[0].action = 'http://edgwarelocalcars.co.uk/Booking2.html';
        document.getElementById('GMsubmit').style.display = 'none';
        document.getElementById('CBsubmit').style.display = '';
    [Second Page - http://edgwarelocalcars.co.uk/Booking2.html]

    This is the page I wish to display the results. To be more precise, above the table containing the cars. I want the price to be displayed along with the addresses, and also have the price underneath the first car, then for the second I need the price to be displayed with £2 added to it, third car should be the same but £10 added but this bits I will work on later, I just want to concentrate on the values being passed.
    At the moment I dont know how to get the address1 and address2 along with the price displayed.
  • harrierdh
    New Member
    • Jan 2010
    • 16

    #2
    I see in Booking2.html you have a function called getParams. I don't see it ever being called though. And I don't see it populating anything. It just parses the URL and does nothing else.

    Is your question how to use this function?

    Comment

    • stylishjm
      New Member
      • Jan 2010
      • 27

      #3
      Yes I need help with the function. Ive tried google and tried some codes but I can never figure out what to do. If you can help me I would be very grateful

      Comment

      • harrierdh
        New Member
        • Jan 2010
        • 16

        #4
        Here is some code for you to play with. It would replace the code in booking2. I'll look at the quote3999 a little later. Got get some work done.

        location.href would return your full query string.

        Code:
        <script type="text/javascript">
        function getParams(location) {
        	//var location = location.href;
        	var location = "http://edgwarelocalcars.co.uk/Booking2.html?address1=ha8+7ej%C2%A0UK&address2=nw 2+2nj%C2%A0UK&results2=results2&price=2.00"; 
        	var idx = location.indexOf('?');
            var str = location.substr(idx + 1, location.length - idx);
        	str = str.toString();
        	var name = "";
        	var value = "";
            var tempArr = new Array();	 
        	var params = str.split("&");
        	for (i = 0; i < params.length; i++) {
        		tempArr = params[i].split("=");
        		name = tempArr[0];
        		value = tempArr[1];
        		if (name == "price") {
        			document.getElementById("chk1").innerHTML = value;
        		}
        	}
        }
        </script>
        <body onLoad="getParams()">
        	<input type="checkbox" name="ckbox" />
        	<p id="chk1"></p>
        </body>

        Comment

        • stylishjm
          New Member
          • Jan 2010
          • 27

          #5
          Thanks! But now I have no idea what do to with this.
          Ive Put this on a new page to see how it works and it shows that it displays £2.00 when Using the
          Code:
          var location = "http://edgwarelocalcars.co.uk/Booking2.html?address1=ha8+7ej%C2%A0UK&address2=nw 2+2nj%C2%A0UK&results2=results2&price=2.00";
          .
          I deleted that and used the locaton.href, then added the
          Code:
          l?address1=ha8+7ej%C2%A0UK&address2=nw 2+2nj%C2%A0UK&results2=results2&price=2.00
          to the end of the page when previewing it but it didnt show the price! Ive even changed "price" to address1 etc but still cant get my head round it!

          Ive uploaded the updated version of the page http://edgwarelocalcars.co.uk/Booking2.html
          If you dont mind explaing what bit does what in the code you gave me that would greatly help me.

          Thanks

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            the getParams function could be simplified a bit:
            Code:
            function getParams() {
                var search        = top.location.search.replace(/^[?]/, '');
                var paramsStrings = search.split('&');
                var params        = {};
            
                for (var i = 0, l = paramsStrings.length; i < l; ++i) {
                    var p = paramsStrings[i].split('=');
            
                    alert(p[0] + ' = ' + p[1]);
            
                    params[p[0]] = p[1];
                }
            }
            instead of the alert do what you want with the params there. The code currently stores the params in a variable ... it might be that you don't need this or use it anywhere as you wish ...

            basicly it uses the querystring and extracts the params from it by splitting the string first at the '&' and second at the '='.

            kind regards

            Comment

            • stylishjm
              New Member
              • Jan 2010
              • 27

              #7
              Thanks, yes i think i can make sense of what the code does now, but i still have trouble actually getting it to display the values on the page.

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                where should what appear?

                Comment

                • stylishjm
                  New Member
                  • Jan 2010
                  • 27

                  #9
                  anywhere on the page. I just need to know how to actually get all the values from the previous page written there.

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5390

                    #10
                    in a previous post was already a hint ... just put a container to the page like:
                    Code:
                    <div id="anyIdYouWantHere"></div>
                    now in my above code change the alert-line to:
                    Code:
                    if ( p[0] == 'price' ) {
                        document.getElementById("anyIdYouWantHere").innerHTML = p[1];
                    }
                    as example for displaying the price in the div.

                    Comment

                    • stylishjm
                      New Member
                      • Jan 2010
                      • 27

                      #11
                      yes excellent works perfectly BUT now what would I change "+" into a space, as the postcode gets posted like NW2+2NH. Also to add more parameters how would I do this

                      Code:
                      if ( p[0] == 'price1' ) {
                          document.getElementById("anyIdYouWantHere1").innerHTML = p[1];
                      }
                      if ( p[0] == 'price2' ) {
                          document.getElementById("anyIdYouWantHere2").innerHTML = p[1];
                      }
                      would I do another if below it like this?
                      Last edited by gits; Jan 22 '10, 04:54 PM. Reason: added code tags

                      Comment

                      • gits
                        Recognized Expert Moderator Expert
                        • May 2007
                        • 5390

                        #12
                        you could use the replace-function:
                        Code:
                        'NW2+2NH'.replace('+', '&nbsp');
                        this replaces the + with a html-nonBreakingSpac e ... for example

                        and for the second part ... you could use more if -statements or use the switch-statement:
                        Code:
                        switch ( p[0] ) {
                        case 'price':
                            // do something for this case
                            break;
                        case 'price1':
                            // do something for this case
                            break;
                        }
                        which looks a bit cleaner

                        Comment

                        • stylishjm
                          New Member
                          • Jan 2010
                          • 27

                          #13
                          Thanks so much for your help it works perfectly just ONE las thing. the replace function, where do I put it exactly so that it does it for all values that would go in divbox 1 2 and 3?

                          Comment

                          • gits
                            Recognized Expert Moderator Expert
                            • May 2007
                            • 5390

                            #14
                            just add it where you need it ... to replace before setting it to the document use it like this:
                            Code:
                            document.getElementById("id").innerHTML = p[1].replace('+', '&nbsp');

                            Comment

                            • stylishjm
                              New Member
                              • Jan 2010
                              • 27

                              #15
                              Thanks for that, but instead of displaying a space it showed nw2&nbsp2NH but i just changed it to replace '+' with " " and it works fine. Thanks again!

                              Comment

                              Working...