Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie - Launch another ASP page using AJAX

Status
Not open for further replies.

Mighty

Programmer
Feb 22, 2001
1,682
US
Hi Folks,

I have absolutely no experience using AJAX. Had posted a question in relation to my problem on the HTML forum and wass directed to use AJAX. Basically I have an ASP page which runs on my webserver. When this page completes I use window.open on the page load to open an ASP page on another server. The problem is that the user is prompted whether they want to open this second page or not. However, it is imperative that this page is opened so I don't want to give the user the chance to say No to opening the page.

Apparently I can use AJAX to open this page. Can anyone point me in the right direction.

Mighty
 
ajax relates to client server interaction, rather than the purely client-side aspects of simply opening a browser window.

additionally ajax is difficult to implement if you are going cross domain. you _can_ do it use server side intermediation but not directly.

however you have not really given enough information to be able to help you. can you explain what end effect you are trying to obtain? why is it necessary that another window be opened? why is it necessary that a user not be prompted (this is a user configurable setting on many browsers).

 
Basically I have a module on an Intranet site that a user enters to pick a work order to run on our production line. Once the user selects the work order a certain amount of processing is done on the webserver. However, I then need to run a module that resides on my ERP server so I need to redirect the user to an ASP page that resides on my ERP server so that my ERP database gets updated.

Whether I use Response.Redirect or just pop up a new window with the ERP ASP page, it prompts the user whether they want to open the page or not.

As I know the address of the page that I want to open, is there even some way to use group policy to set it so that these pages never ask the user for permission when they are opened.

Mighty
 
why don't you send the request to your erp server from the web server? just pass the post variables through as needed. if there are messages that need to be displayed to the user then these can be captured and fed back to the browser. you could do this with a server side xmlhttprequest object or through curl or similar.
 
jpadie,

Thanks for the suggestions but it is going over my head to a certain extent. Have no experience with "xmlhttprequest object or through curl" as you suggest.

Works as follows - user loads ASP page, selects work order and submits form. Certain amount of processing done on webserver and then ERP server asp page is launched. How do I post form variables to ERP page without a form and without prompting user?

Mighty
 
in php it would look something like this. the script on the server would be the action of the form.

in server side javascript or vbscript you'd probably use xmlhttpobject. i'd suggest enquiring in those fora if you need specific help on the language.

Code:
<?php
//establish framework
$url_ERP = "[URL unfurl="true"]http://123.456.789.001/myerpapp/myapi.cgi";[/URL]

//check for form submission
if (isset($_POST['submit'])){ 	//has submit button been pressed
	webserverProcess();
	erpProcess();
}

function webserverprocess(){
 //do whatever you want with the $_POST data
}	

function erpprocess(){
	global $url_ERP;
	
	//establish the curl session
	$ch = curl_init($url_ERP);
	//tell cURL to use POST
	curl_setopt($ch, CURLOPT_POST, true);
	//ignore the headers
	curl_setopt($ch, CURLOPT_HEADER, false);
	//tell cURL to forward on the $_POST array (i.e. all the form fields)
	curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
	//execute the curl
	//result will be sent to the browser.
	//if you don't want this set CURLOPT_RETURNTRANSFER
	curl_exec($ch);
	
	//close the curl connection
	curl_close($ch);
}
?>

you could also change the process around a bit and use ajax to send the form data to your web server which would not cause a cross-domain error. you would make the request synchronous rather than asynchronous. if the web server process worked ok the form would then be allowed to execute naturally. so making the erp process work is as simple as putting the address of the erp server as the action of the form in question

something like this
Code:
<script>
function sendtoWebServer(){
	//get the form object
	var form = document.getElementById('myform');
	//get all the elements in the form
	var elems = form.elements;
	//get the number of elements
	var numElems = elems.length;
	var _postData;
	//iterate through the elements, creating a postdata array of variables
	for (var i=0; i<numElems; i++){
		_postData[] = encodeURI(elems[i].name) + '=' + encodeURI.elems[i].value;
	}
	//join the data into a post string
	var postData = _postData.join('&');
	
	var webserverurl = '[URL unfurl="true"]http://mywebserver.com/myreceivingscript.asp';[/URL]
	
	//this is a bit half-hearted...
	var xmlhttp=false;
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	//create the request
	xmlhttp.open("POST", webserverurl, false); //note the false.  this makes it synchronous
	xmlhttp.onreadystatechange=function() {
  		if (xmlhttp.readyState == 4) {
   			if (xmlhttp.responsetext == 'OK'){
				//the webserver parsed the data ok
				return true;
			} else {
				alert ('there has been a problem.  Cannot continue');
				return false;
			}
  		}
 	}
 	xmlhttp.send(postData);
}
</script>

<body>
<form action="erpurl.cgi" method="post" onsubmit="return sendtoWebServer(this);">
<!--whole bunch of form fields -->
</form>
</body>

for the above to work you need to make sure that the webserver outputs JUST 'OK' if it has successfully parsed and dealt with the incoming data.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top