Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 14th, 2007, 10:39 PM
Registered User
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Submit Form with QueryString Data

This might seem simple. I know how to do this in ASP.NET but want to try it in JavaScript:

Have an online application that only accepts "POST" form data (hidden in HTML header) I want to submit data to the application using a querystring instead.

So, I want to make an intermediate Javascript page that accepts a querystring URL, take the querystring, put it into the form and then "POST" the form to the application.

I know how to get the querystring:
document.location.search.substring(1,7)

I know how to submit the form:
document.frmFormName.submit()

I just don't know how to put the querystring into the form so it can be submitted

Sorry if this is exceedingly obvious.
 
Old June 15th, 2007, 01:49 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If you know what fields will be represented then it's not too difficult. Create an HTML page with the requisite form and text inputs representing your query string, e.g. if your querystring was forename=joe&surname=fawcett then have two textboxes named forename and surname.
Read the querystring using the search property and create an array using the split method with & as the separator. For each item in the array split agin on the = character and check the value of the first element, this will be forename or surname in the example. Assign the second element of the array to the appropriate textbox. Then call document.forms[0].submit() to submit the form. You can debug all this by having a confirm() in between assigning the values and submitting to see if everything is okay.

A completely different approach would be to use the XMLHttpRequest. Capture the querystring as before. Open a connection to the new URL with the POST method and send the entire querystring as the data argument to the send method. That way, as long as there's a one-to-one mapping between the originally querystring and the new parameters you don't need to parse the querystring at all.

--

Joe (Microsoft MVP - XML)
 
Old June 15th, 2007, 10:14 AM
Registered User
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is what I need help on:

"Assign the second element of the array to the appropriate textbox"

I need to get the querystring into the value= of the input box in the form. I know how to get the querystring and I know how to submit the form.

Thanks very much for your help. Again, sorry if this is exceedingly obvious.
 
Old June 15th, 2007, 10:45 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

document.forms[0].elements["forename"].value = <whatever>

--

Joe (Microsoft MVP - XML)
 
Old June 15th, 2007, 06:11 PM
Registered User
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Perfect thanks!

I tweaked a little after understanding the concept. Here is the complete code. Again, I had a POST type form page on our intranet that I was not going to be able to get the vendor to change to a querystring page. I wanted an intermediate page in Javascript that would take a querystring URL, put it into a POST form and submit the form:

<html>
<head>
<title>Intermediate Page</title>
</head>
<body>
<form name="frmSearch" method="POST" action="http://intranet.humenahumena.com/Search.jsp">
<input type="hidden" name="txtSearch" value="">
</form>
<script language="javascript">document.frmSearch.txtSearch .value = document.location.search.substring(1,7);
document.frmSearch.submit();
</script>
</body>
</html>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Refresh Data when Submit Form ruso77 Infopath 0 April 13th, 2007 02:30 PM
submit data to db with html form....(help) PeterJB Beginning PHP 2 August 6th, 2006 12:07 AM
Submit repeated data item in a form using Struts somnathp Javascript How-To 0 July 26th, 2005 12:34 AM
How to send form data without pressing SUBMIT eapsokha Classic ASP Professional 2 March 1st, 2004 09:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.