0

I need to POST form values from one page to another using Javascript.

Now, I know that I could use a server-side technology like ASP.Net or PHP to post values but I am not allowed to use any server side script. I am aware that using the GET method, I can pass the form values as a query string but the values will not be passed securely (which is an important requirement!)

The conditions listed below:

  1. This code should take the values that are posted to the page and repost to target page. HTTP POST only (not get).
  2. In no cases, even error, the request should not stop on this bridge page.
  3. The script needs to handle multiple posted values.
  4. Try to use standard javascript (no 3rd party library)
  5. Script needs to work in IE, FF, Safari, most standard browsers

Can anyone please help me find a solution to this or point me to some resource that will help me find the soln? Thanks in advance. Below is the code for passing values as a query string. Can I modify this so that my above requirements are satisfied?

FORM

<html>
<head>
<title>Test</title>
<script type="text/javascript">

function goto_page(page) {
    var usbnum = document.getElementById('usbnum').value;
    var usbcode = document.getElementById('usbcode').value;
    var q_str = '?usbnum=' + usbnum + '&usbcode=' + usbcode;

    var url = page + q_str;

    window.location = url;
}

</script>
</head>
<form id="form1" method="post">
<div>
    USB No: <input name="usbnum" id="usbnum" type="text" size="80" /><br />
    USB Code: <input name="usbcode" id="usbcode" type="text" size="80"/>
</div>
<a href="#" onclick="goto_page('bridgepage.html');">Next</a>
</form>

</body>
</html>

BRIDGE PAGE

<html>
<head>
<title>Bridge Page</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.split('&');
    var params = {};

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

    return params;
}

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

    var txt = 'Hello ';

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

    var body = document.getElementsByTagName('body')[0];
    body.innerHTML += txt;
}

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

  var num_container = document.getElementById('usbnum');
  var code_container = document.getElementById('usbcode');

  num_container.innerHTML = params.usbnum;
  code_container.innerHTML = params.usbcode;
}
</script>
</head>

<body onLoad="write_params()">

</body>

</html>
Deepa Thalikar
  • 129
  • 1
  • 3
  • 16
  • 1
    Answer: SSL + AJAX. This sounds a bit too much like a homework assignment, so that's all I'm going to say. – Diodeus - James MacFarlane Jan 30 '13 at 22:15
  • I agree, Diodeus! But thanks a lot for your quick response. It is not a home-work assignment, btw. It is part of an application that we are building. I atleast know that AJAX is the way to go. – Deepa Thalikar Jan 30 '13 at 22:20

1 Answers1

1

POST data can only be handled by server side code. There is no way you can use them in your javascript without help from a server side code.

You can only use GET or you can think about cookies. But at other hand, why do you want to change current page?! you can use AJAX to load more data without refreshing and no need of posting or getting variables.

Soroush Falahati
  • 2,196
  • 1
  • 27
  • 38
  • The post "http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit" comes close to what I need. But the values do not pass to the destination page in my case. Can anybody suggest a working example of this approach. Please help, I am new to this! Thanks in advance. – Deepa Thalikar Jan 31 '13 at 01:37
  • @DeepaThalikar There is two thing here. First POSTING data using javascript. This can be done easily with the link you give us. But there is an other topic here too about reading this posted data from javascript without help from any server side code. This is not possible. Of course you can encrypt your GET data or use cookies to send data between two pages that both are possible via JS. But before that please tell us what you are going to do with this data after that?! Please tell us little about your project so we can suggest better ways if any. – Soroush Falahati Jan 31 '13 at 14:30
  • I only have to display the data on another page, that's it. Can you tell me how to do that using cookies or by encrypting GET data? – Deepa Thalikar Jan 31 '13 at 15:02
  • @DeepaThalikar Read more about cookies at: http://www.w3schools.com/js/js_cookies.asp And for encrypting and decrypting data you can use this library: http://code.google.com/p/jscryptolib/ – Soroush Falahati Jan 31 '13 at 15:12