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

3rdLifeWebDevAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
3rdLifeWebDevAuthor Commented:
Thanks for your help with this!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.