[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1365
  • Last Modified:

Insert variables from querystring into form fields using Javascript

I am using getQSParm to get variables.

I want script to insert variables into form fields.

I want script to then "submit" onLoad.


function parseQString() {
             var qs = new Array();
             var loc = location.search;
             if (loc) {
                 loc = loc.substring(1); // loose the leading ?
                 var parms = loc.split('&');
                 for (var i = 0; i < parms.length; i++) {
                     nameValue = parms[i].split('=');
                     qs[nameValue[0]] = (nameValue.length == 2) ? unescape(nameValue[1]) : null; // use null or ""
                 }
             }
             return qs;
         }
         function getQSParm(parm) {
             return (qs[parm]) ? qs[parm] : null
         }
         var qs = parseQString();
 
var Variable1 = getQSParm("Variable1");
 
// Then I want to insert Variable1 in to the form below:
 
form action="https://securedsite.com/Addform" method='POST'>
<input value="30" type="hidden" id="formid" name="formid" />
<input value="CustomFormWeb" type="hidden" id="type" name="type" />
 
<input type='text' name='Contact0FirstName' value=''>
 
// Then instead of pressing "Submit", I want the data to pass when the page finishes loading.
 
<input value="Submit" type="submit" class="button np inf-button" id="Submit" name="Submit" />

Open in new window

0
3rdLifeWebDev
Asked:
3rdLifeWebDev
  • 9
  • 7
1 Solution
 
3rdLifeWebDevAuthor Commented:
Am I getting close?

Anyone?
<head>
    <title></title>
 <script type="text/javascript" language="javascript">
     function submitform() {
         document.getElementById('formid').submit();
     }
</script>  
    
</head>
<body onload="submitform()>
 
 
function parseQString() {
             var qs = new Array();
             var loc = location.search;
             if (loc) {
                 loc = loc.substring(1); // loose the leading ?
                 var parms = loc.split('&');
                 for (var i = 0; i < parms.length; i++) {
                     nameValue = parms[i].split('=');
                     qs[nameValue[0]] = (nameValue.length == 2) ? unescape(nameValue[1]) : null; // use null or ""
                 }
             }
             return qs;
         }
         function getQSParm(parm) {
             return (qs[parm]) ? qs[parm] : null
         }
         var qs = parseQString();
 
var Variable1 = getQSParm("Variable1");
 
// Then I want to insert Variable1 in to the form below:
 
form action="https://securedsite.com/Addform" method='POST'>
<input value="30" type="hidden" id="formid" name="formid" />
<input value="CustomFormWeb" type="hidden" id="type" name="type" />
 
<input type='text' name='Contact0FirstName' value=''>
 
// Then instead of pressing "Submit", I want the data to pass when the page finishes loading.
 
<input value="Submit" type="submit" class="button np inf-button" id="Submit" name="Submit" />
 
</script>
<script type="text/javascript" language="javascript">
    document.getElementById('formid').submit();
</script>

Open in new window

0
 
3rdLifeWebDevAuthor Commented:
OK...I have solved the onload question.

What I really need now is the following:

I have defined variables from URL string:

var[1] = getQSParm("b_fname");

What I now need is to place var[1] in to the form value =' '

Help?

0
 
Michel PlungjanIT ExpertCommented:
You mean


<head>
    <title></title>
<script type="text/javascript" language="javascript">
 
function parseQString() {
  var qs = new Array();
  var loc = location.search;
  if (loc) {
    loc = loc.substring(1); // loose the leading ?
    var parms = loc.split('&');
    for (var i = 0; i < parms.length; i++) {
      nameValue = parms[i].split('=');
      qs[nameValue[0]] = (nameValue.length == 2) ? unescape(nameValue[1]) : null; // use null or ""
    }
  }
  return qs;
}
function getQSParm(parm) {
  return (qs[parm]) ? qs[parm] : null
}
var qs = parseQString(); // initialise
 
function submitform() {
  document.forms[0].Contact0FirstName.value=getQSParm("b_fname"); // get ?b_fname=Fred and put it into the Contact0FirstName in the form 
  document.forms[0].submit(); // submit it
}
 
 
window.onload=function() {
  submitform();
}
 
</script>  
    
</head>
<body>
  
<form action="https://securedsite.com/Addform" method='POST'>
<input value="30" type="hidden" id="formid" name="formid" />
<input value="CustomFormWeb" type="hidden" id="type" name="type" />
<input type="hidden" name="Contact0FirstName" value="">
</form>

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
3rdLifeWebDevAuthor Commented:
This works well...except...I am using this to do a "Silent Post"...passing variables from my shopping cart to my crm using a form action.

The data is not passing. Is there another command for "POST"...other than window.onload?

When I manually type the URL with ?b_fname=Fred, this is fully functional.

0
 
Michel PlungjanIT ExpertCommented:
I do not know what you mean. Sorry

What do you mean by POST and window.onload?

If the previous page POSTs values, then they are NOT available in the url
Then you need a cookie, assuming the same server

0
 
3rdLifeWebDevAuthor Commented:
The shopping cart does a "silent post" via HTTP POST to a 3rd party server.

I assumed that when they said HTTP POST, that all data would be sent to the receiving URL with a string attached of all data.

The page that I built( with your help) parses that data and completes the form action.
0
 
Michel PlungjanIT ExpertCommented:
method="get" puts all data in the query string
method="post" puts all data on stdin for the server's eyes only.
0
 
3rdLifeWebDevAuthor Commented:
Can you direct me to knowledge to set this up?

0
 
Michel PlungjanIT ExpertCommented:
I am a little confused

How do you get to this page? from a page of your own or the 3rd party.
I see you POST the form on THIS page, that does not matter.
It is how you arrive at THIS page that matters. If from a 3rd party and via POST, then you are out of luck unless you can set a cookie on a page BEFORE THEIR page.

1 - your page posts to
2 - their page which posts to
3 - your seconds page (THIS page)

then we can do

Your page 1 - set a cookie and sends something off to 3rd party with a POST
However - if they have a "page to redirect to after processing" field where you specify THIS page, then you can tack on the value on that url instead of a cookie

Tell me more
0
 
3rdLifeWebDevAuthor Commented:
Let me clarify:

Shopping Cart Checkout(SCC) on successful purchase transaction does a silent post "POST HTTP" to server cgi-bin. (Originally I did not understand this...I thought it was just loading another page via URL and string).

So the SSC does a post to a CGI-BIN script (which I now need to build).

Then, I need the CGI-BIN script to pass all the variables to my webpage, that have the window.onload Javascipt. Then data will be passed via form action to my CRM.

1 - your page posts to  ***Post originates FROM shopping cart TO CGI-BIN***
2 - their page which posts to  ****CGI-BIN needs to call URL?Variables of my page(this page)
3 - your seconds page (THIS page)  ***This page works well and posts data via form action to my 3rd party crm...which is web-based.

then we can do

Your page 1 - set a cookie and sends something off to 3rd party with a POST  ***What you have below is available***
However - if they have a "page to redirect to after processing" field where you specify THIS page, then you can tack on the value on that url instead of a cookie...yes.

Tell me more
0
 
Michel PlungjanIT ExpertCommented:
1. have your cgi-bin pass the value you need in the querystring OR
2. have your cgi-bin set a cookie that is read in the page (only if cgi-bin lives on the same server) OR
3. make the cgi-bin BE the page you need, then you can fill in the form already and present it to the user


0
 
3rdLifeWebDevAuthor Commented:
I need to do number one...and I need to build the cgi-bin file.

Can you point me to a resource that tells me how to build the file? My scripting knowledge is limited.
0
 
Michel PlungjanIT ExpertCommented:
That depends on the scripting language - what is the languages you have access to on the server?
for example asp, php, perl?
And what size is the possible site?

Michel
0
 
3rdLifeWebDevAuthor Commented:
Perl or PHP is fine.

The "site" is nothing more than the one file that needs to site on my server, receive the POST HTTP from shopping cart (cart is already built and operational) and then send data via URL string to another web page. It would be nice for the data to be saved mysql or something of the sorts.
0
 
Michel PlungjanIT ExpertCommented:
So like this



<?php
 
function make_safe($str) {
//  $str = mb_convert_encoding($str, 'UTF-8', 'UTF-8'); // needs to be enabled
  $str = htmlentities($str, ENT_QUOTES, 'UTF-8');
  return $str;
 
}
   
// get the values from the post
$firstname     = make_safe($_POST["firstname"]);  
$comments      = make_safe($_POST["comments"]);
 
 // here we can write to database 
 
 // and here we can forward if successful write
 
 header("Location:http://www.server.com/next.php?firstname=".$firstname."&comments='.$comments);
 
?>

Open in new window

0
 
3rdLifeWebDevAuthor Commented:
Thanks for your help with this!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now