0

I have a code that will show the value of form1 to another page. Now, This +(plus sign) keeps on showing and substituting the spaces. Is there anyone can help me here?

Thank you so much.

Below are my codes:

Page1.html

<form type=get action="page2.html">
  <table>
    <tr>
      <td>First Name:</td>
      <td><input type=text name=firstname size=10></td>
    </tr>
    <tr>
      <td>Last
        Name:
      </td>
      <td><input type=text name=lastname size=10></td>
    </tr>
    <tr>
      <td>Age:</td>
      <td><input type=text name=age size=10
        id="age"></td>
    </tr>
    <tr>
      <td colspan=2><input type=submit
        value="Submit"> </td>
    </tr>
  </table>
</form>

Page2.html

<script LANGUAGE="JavaScript"> 
  function getParams() {
    var idx = document.URL.indexOf('?');
    var params = new Array();
    if (idx != -1) {
      var pairs = document.URL.substring(idx + 1,
        document.URL.length).split('&');
      for (var i = 0; i < pairs.length; i++) {
        nameVal = pairs[i].split('=');
        params[nameVal[0]] = nameVal[1];
      }
    }
    return params;
  }
  params = getParams();
  firstname =
     unescape(params["firstname"]);
  lastname =
     unescape(params["lastname"]);
  age = unescape(params["age"]);

  ages = age;
  document.write("firstname = " + firstname + "<br>");
  document.write("lastname = " + lastname + "<br>");
  document.write("<input type=text name=age size=10 value=" + ages +
    ">");
</script>
jvperrin
  • 3,368
  • 1
  • 23
  • 33
  • kindly fromat your code properly.its really hard to read – Anubhab Dec 09 '13 at 05:17
  • I don't see where you get the lastname? but try decodeURIComponent(lastname). Also, see http://stackoverflow.com/questions/20116962/how-to-get-values-in-window-location-href/20117046#20117046 – Andrew Dec 09 '13 at 05:17
  • Thanks for the comment guys. @Andrew: It's in the page1.html – user3072785 Dec 09 '13 at 05:37

1 Answers1

0

You can use decodeURIComponent to decode the plus signs back into spaces, and encodeURIComponent to encode it on the first page.

You also don't need to be using substring so much to try and manually parse the URL:

var url = location.href.split("?");
var params = url[1].split("&");
var data = {};

for (var i = 0; i < params.length; i++)
{
    var param = params[i].split("=");
    data[param[0]] = param[1];
}

At the end of that code, you'll have an object data that contains keys of the parameter names you pass in :)

Collin Grady
  • 2,226
  • 1
  • 14
  • 14
  • Thanks Collin for the answer. When I tried your code it's not functioning. May you please elaborate it more? Thank you once again. :) – user3072785 Dec 09 '13 at 05:36
  • What do you mean by not functioning? Are you doing anything with the `data` object that is created? – Collin Grady Dec 09 '13 at 05:50
  • I actually copy pasted your code to my code in the page2.html and there are no content showed. – user3072785 Dec 09 '13 at 06:02
  • My code does not print anything; you need to `document.write` it still if you want that, but use `data.firstname` and `data.lastname` instead of your `firstname` and `lastname` variables. – Collin Grady Dec 09 '13 at 06:03
  • Sorry Collin. But I think I don't understand it anymore. i'm not that good in javascript but can you please write it here on how you'll implement it, I mean the codes? Because when I tried it there's no content showing. I hope you understand. Thanks. – user3072785 Dec 09 '13 at 06:19
  • You already have the code for writing it... `document.write("firstname = " + firstname + "
    ");` - just change it to this: `document.write("firstname = " + data.firstname + "
    ");`
    – Collin Grady Dec 09 '13 at 06:20
  • Okay I made it into this: http://pastebin.com/jwr76VuE Please look if I'm doing fine. Because i tried loading it, it shows still nothing in my content area.. Thanks. – user3072785 Dec 09 '13 at 06:24