How to Pass a JS Variable to a PHP Variable

Posted by dmullins on Stack Overflow See other posts from Stack Overflow or by dmullins
Published on 2010-04-26T15:50:16Z Indexed on 2010/04/26 15:53 UTC
Read the original article Hit count: 338

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about AJAX

Related posts about variable