views:

105

answers:

1

Hello:

I am working on a web application that currently provides a list of documents in a MySql database. Each document in the list has an onclick event that is suppose to open the specific document, however I am unable to do this.

My list gets popluated using Ajax. Here is the code excerpt (JS):

function stateChanged()
  {
if (xmlhttp.readyState==4)
  {
  //All documents//
  document.getElementById("screenRef").innerHTML="<font id='maintxt'; name='title'; onclick='fileopen()'; color='#666666';>" + xmlhttp.responseText + "</font>"; 
   }
  }
 }

The onclick=fileopen event above triggers the next code excerpt, which downloads the file (JS):

function stateChanged()
 {
if (xmlhttp.readyState==4)
 {
  //Open file
  var elemIF = document.createElement("iframe"); 
  elemIF.src = url; 
  elemIF.style.display = "none"; 
  document.body.appendChild(elemIF);  
 }
}

}

Lastly, the openfile onclick event triggers the following php code to find the file for downloading (php):

$con = mysql_connect("localhost", "root", "password");
 if (!$con)
   {
     die('Could not connect: ' . mysql_error());
   }
 mysql_select_db("Documents", $con);    
 $query = mysql_query("SELECT name, type, size, content FROM upload WHERE name = NEED JS VARIABLE HERE");
 $row = mysql_fetch_array($query);   
 header ("Content-type: ". $row['type']);
 header ("Content-length: ". $row['size']);
 header ("Content-Disposition: attachement; filename=". $row['name']);
 echo $row['content'];

This code works well, for downloading a file. If I omit the WHERE portion of the Sql query, the onclick event will download the first file. Somehow, I need to get the screenRef element text and change it into a variable that my php file can read. Does anyone know how to do this? Also, without any page refreshes.

I really appreciate everyone's feedback and thank you in advance.

DFM

+2  A: 

You can't reference a client side (js) variable from server side (php) code.

You need to pass the ID of the document to be displayed in the post back to the server by including it in the querystring or form values that are posted back.

Zarigani
Thank you for your input - Is it possible to do this with Ajax, such as converting the xmlHTTP.responseText into a variable or some other Ajax specific criteria?
I think you are thinking about this the wrong way around.AJAX still runs on the client.Whatever you send is through the Querystring to the server (PHP code) and then the PHP code is processed and it returns a plain html page.Just send whatever you want through an ajax post request.
the_drow