PDA

View Full Version : AJAX variable not passing to ASP.net or PHP page!


Sedativechunk
03-10-2009, 02:10 AM
I need some help with a program I'm working on. I'm trying to pass a variable from one page to the next and it's not working. Here's the javascript (this code is for an AJAX comment script I'm making):

// Displays and hides the comment area
// Obtains the blog ID to display the right comments
// Can also be called to re-display the comments after one is added
function DisplayComments(blog_id) {
// Hide/show the comment area
comment_area = document.getElementById("comment_area");
if (comment_area.style.display == 'none') {
comment_area.style.display = 'block';
} else {
comment_area.style.display = 'none';
}

var xmlHttp;
try {
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch (e) {
// Internet Explorer
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange = function() {
if (request.readyState == 1) {
// If page is loading, display loading icon
document.getElementById("comment_area").innerHTML = '<img src="layout/grafix/ajax-loader.gif" alt="" />';
}
if (xmlHttp.readyState == 4) {
document.getElementById("comment_area").innerHTML = xmlHttp.responseText;
}
}
var url = "comments.aspx?blog_id=" + blog_id;
// DEBUGGING URL WORKS FINE
alert(url);


/** WHERE THE PROBLEM IS OCCURRING I BELIEVE **/
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
/**********************************/
}


As you can see, I'm creating a function called DisplayComments(blog_id) and from my main page, I am passing a variable to the function for the blog_id I want to get like so:

<li onclick="javascript:blog_id = 1; DisplayComments(blog_id);">Display Comments</li>


Passing the variable to the function works fine. It's between sending the value between the main page and the comments.aspx page where things get messed up. Before I go farther, I've tested the comments.aspx page by viewing the page in my web browser it's self and adding the blog_id value myself and it works fine:
http://whatever.com/comments.aspx?blog_id=1

The ASP file contains the following code:

<%
' Collect the variable and print it
Dim blog_id As String = Request.QueryString("blog_id")
Response.Write("COMMENT! The blog ID is <b>" & blog_id & "</b>")
%>


The main problem here is the javascript is not sending the variable to the asp.net page. I'm also having the same problem when trying to pass it to a PHP page, so I believe it's a javascript/AJAX related problem. Note, I've also tried escapcing the blog_id in the javascript:
var url = "comments.aspx?blog_id=" + escape(blog_id);

The variable still does not pass to the page! Does anyone know why my code is not working? Sorry for a long post, but I'm completely stumped on this one and need help. Note, the AJAX it's self works fine and the value on the ASP page is returned "COMMENT! The blog ID is " but the thing wrong there is the blog_id variable isn't passing. So the AJAX works, it's just I can't pass variables to it and my application is kind of useless without it.

Horus_Kol
03-10-2009, 08:13 PM
do you have firebug installed in firefox - you can actually monitor the AJAX request and response in the firebug console...


with the PHP page - what do you get when you var_dump the $_POST array when you get the ajax request?