PDA

View Full Version : Trying to get external page to pass variable back ...



mussitsch
06-15-2006, 06:50 PM
I tried the iFrame example. My issue is that I'm executing an external javascript from an asp page. The result that I need back in ASP is in Javascript. I know the typical answer is to set a hidden form variable and reload the page. I can't reload the page because of it's complexity. I somehow need to seemlessly get the value back so that my ASP page can continue on the appropriate path depending on the result.

Can I do this with DHTML?
Please advise. The iFrame example did execute my code, but I still wasn't able to save my result, only display stuff.

Thanks.
Melissa

Twey
06-15-2006, 07:14 PM
Sounds to me as if you need to use AJAX. Google for tutorials on the subject.

mussitsch
06-15-2006, 07:57 PM
I looked at this briefly and am somewhat familiar, since my external page is actually using the xmlhttp object and this method to handle something else.
I have a javascript function that calls a server side asp page. Data is returned and left back in the javascript function.

If AJAX requires the calling script to be javascript, I still can't seem to get my result in ASP without storing it in some form variable.

Am I missing something?

Melissa

Twey
06-15-2006, 08:15 PM
Yes. If you use AJAX to access, for example,
asppage.asp?var1=val1&var2=val2... you can read off the GET variables on the ASP page. POST variables are possible too, by way of send().

mussitsch
06-19-2006, 03:54 PM
Sorry, I'm still not following.

My 1st ASP page calls a JavaScript function that uses xmlhttp to call a different ASP page. The result is set in the 2nd asp page and passed back to the JavaScript function. I need the result to be accessed by the 1st ASP page. I am passing variables as you mention in the javascript that are being accessed by the 2nd ASP page. But again -- I need this accessed by the 1st ASP page.

Thanks.
Melissa

Twey
06-19-2006, 04:02 PM
OK. Let's very originally call your pages one.asp and two.asp.
one.asp uses XMLHttpRequest to request two.asp, passing the data using GET or POST. two.asp outputs the result. one.asp then uses AJAX to request itself, passing that data.Of course, it would be even easier to just call one.asp straight from two.asp.

mussitsch
06-19-2006, 07:31 PM
Ok.
I think the problem is more that I really don't want to reload one.asp. I have a very complicated form filled out that has already been submitted. If I have to recall one.asp from two.asp, then I'll need to pass in all completed fields, which is what I was hoping to avoid.

If I'm going to do that, then I might as well have my javascript or two.asp page resubmit the form to get my result.

I hope I'm still not missing it!! :/

Twey
06-19-2006, 07:53 PM
I hope I'm still not missing it!! :/You are. The whole point of using XMLHttpRequest is to avoid reloading the page :) There's no need to.

mussitsch
06-19-2006, 08:41 PM
Ok.
I think I've got it. I just need to go ahead and give it a shot.

Thanks -- you may be hearing from me again.
:)

Melissa

mussitsch
06-20-2006, 12:35 PM
I tried a new test by having one.asp call two.asp. One.asp has a simple form. Two.asp sets a value and then passes it back. I tried both having the javascript call itself on one.asp and also having two.asp call one.asp.

In both cases, when one.asp is called again - no matter what method - my form data is wiped out. Is the only way around this to pass all my form data to two.asp and then pass it back? This is what I was trying to avoid because of a lot of my form and form data are generated dynamically based on what the user selected along the way.

The form in one.asp looks like:
<form>
<input type=text name="txt" value="">
<input type=button onClick="CallTwo();" name="btn" value="Click">
</form>

And for now two.asp just does a response.redirect to one.asp passing a variable. Should I try to open the two.asp in a new window so that one.asp's form is left intact?

Thanks.
Melissa

Twey
06-20-2006, 01:01 PM
You don't open two.asp at all.
Say two.asp looked like this:
<%
Dim var1 = "Thing"
Response.Write(var1)
%>(yes, that's about the whole of my knowledge of VBScript :p)
one.asp would call it like this:
<script type="text/javascript">
function getHTTPRequest() { /* HTTP request creation stuff */ }
function processData(data) { /* Your processing of the final data */ }
function ajaxpost(f) {
var xht = getHTTPRequest();
xht.open("POST", f.action, false);
for(var i=0;i<f.elements.length;i++)
if(f.elements[i].name)
xht.send(urlencode(f.elements[i].name) + "=" + urlencode(f.elements[i].value));
processData(xht.responseText);
return false;
}

<form onsubmit="return ajaxpost(this);" action="two.asp" method="post">
<!-- Your form elements go here -->
</form>

mussitsch
06-20-2006, 04:12 PM
Twey,

My code looks very similar to yours. Here is my call to two.asp using the xmlhttp object:
function CallTwo()
{
//instantiate correct object depending on browser
var xmlhttp, sURL1, sURL2, sURL3, iError;

try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}

xmlhttp.open("GET", "two.asp", false);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send();
iError = xmlhttp.responseText;
alert(iError);
window.location="one.asp?id=" + iError;
}

And I am getting my value back from two.asp. This is working correctly.
As you can see - it's the last line that is my problem. I need to retrieve the id that was returned in my one.asp. Or in your one.asp, I want a "response.write request("id") to show the value of xht.responseText. And I want to do this without clearing previously entered form field data or reloading the page which a sumit does or my window.location above does.

I'm sorry it's taking so long to explain this to me. Perhaps I need to really explaining what I'm accomplishing here.
A user is filling out a very complicated form complete with customer data, machine information and product selection. Much of the form is dynamic, especially the product selection section since we offer filtering because of so many products. Upon Submit of the form, the asp page needs to make the javascript/xmlhttp call to a server side script that will take the customer information and do an export compliance check on them -- basically to make sure Osama Bin Laden is not trying to do this. The task should basically return a 0 if the customer is ok or 1 if not. Back at my originating page, I need to know the outcome so I know whether to abort the process or continue with it. And if I need to continue, I need to keep all data that was previously entered so all processing works. There is a disconnect here that I can't seem to get around.
My last resort is to try and have my server side script write the value to a database and then my originating asp page wait for that value to be written.

Melissa

Twey
06-20-2006, 05:10 PM
Let me outline what I think you're trying to do, because your words and your code don't match up. :)

one.asp contains a form. You need one.asp to submit some data to two.asp, which will perform the check, and then return a boolean value. one.asp should then act upon this data to allow or prevent the transaction taking place. Correct?

mussitsch
06-20-2006, 06:46 PM
Your statement is correct in what I'm trying to do. The only addition I would make is that when one.asp acts on the boolean value that was returned, it still needs to have the previously submitted form data accessible to it. (In other words -- it can't be a fresh submit or reload).
Also -- the call to two.asp is occuring on the submit of one.asp. On Submit, I basically need to halt -- do my check via two.asp -- and come back to resume my submit code that uses that returned boolean.

Twey
06-20-2006, 06:49 PM
Well then, I see no problem with my code above. Obviously two.asp needs to be your checking script.

mussitsch
06-20-2006, 07:36 PM
Yes - but you're not displaying the returned value in ASP only JavaScript. This is my problem.
Two.asp is the checking script. Perhaps it's easiest if I condense this code and you try it yourself so we're on the same page?

Here is two.asp - very small like yours:
<%

Dim i
i = 6
Response.Write i

%>
'***************************
Now here is one.asp - still fairly small:
Only - in order to get this to work, I had to write the value to a hidden form field and resubmit. I don't want to do this, but I still need to get the result on the ASP side -- not JavaScript side and still need my originally entered form data.

<%
Response.CharSet="US-ASCII"
Response.Buffer = True
%>

<HTML>
<HEAD>

<SCRIPT language="JavaScript">
function CallTwo()
{
//instantiate correct object depending on browser
var xmlhttp, sURL1, sURL2, sURL3, iError;

try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}

xmlhttp.open("GET", "../ExportCompliance/two.asp", false);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send();
iError = xmlhttp.responseText;
//how do I get this back to my asp page without submitting???
alert(iError);
document.forms[0].id.value=iError;
document.forms[0].submit();
}
</SCRIPT>
</HEAD>
<BODY>

<%
response.write Request.Form("id") & " txt is " & Request.Form("txt")
%>
<form name="MainForm" method="post">
<input type=text name="txt" value="<%=Request.Form("txt")%>">
<input type=hidden name="id" value="<%=Request.Form("result")%>">
<input type=submit onClick="CallTwo();" name="Submit" value="Submit">
</form>

</BODY>
</HTML>

Twey
06-20-2006, 07:59 PM
Ah, I understand you now. I can't test your code -- IIS only runs on Windows and ChilliSoft is very expensive -- but I get what your problem is.
<%
' Check for the existence of Request.Form("id") then process it if so.
' Exit before any HTML is sent.
%>
function processData(data) {
var xht = getHTTPRequest();
xht.open("GET", window.location.href + "?id=" + data, false);
xht.send(null);
// do whatever with the final response
}

mussitsch
06-21-2006, 12:29 AM
Thank you for your time on this, however, I'm afraid I'm not going to find a resolution. I suppose I'll have to either resubmit my form or pass all needed data.
Too bad I can't execute this code outside of JavaScript.
:)

Melissa

Twey
06-21-2006, 06:00 AM
Err?
I could have sworn I offered one.