View Full Version : Pass JavaScript (via AJAX) to PHP

09-17-2009, 06:07 PM
Hi - I'm running into a snag getting a variable to pass back to the php page in my AJAX engine.
The page displays the unfiltered values correctly, but isn't registering that the $_GET statement is being fulfilled.
Ideally, the page displays :
here you go 1
here you go 2
here you go 3
here you do 4

This part works ok.

Clicking on "here you go 1" would pass the "1" to the page entitled "textserver.php" which would serve back a table filtered on the appropriate 'PortfolioNumber' (1, 2, 3 etc) to the "Ajax1.php" page.

Right now clicking on any of the aforementioned links simply serves up the unfiltered table generate by textserver.php when it should filter that table by 1, 2, 3 etc as described above.

When this is running I'll of course pretty it up and add more functionality, but the crux of it is to get this part running.

Here is the Ajax1.php page (the 'front end' if you will)

<title>AJAX Application</title>
<script language="javascript">
function getXMLHTTPRequest(){
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject("Msxm12.XMLHTTP");
} catch(err2) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch(err3) {
req = false;
return req;
var http = getXMLHTTPRequest();

function getServerText(PortfolioNumber) {
var myurl = 'textserver.php';
myRand = parseInt(Math.random()*9999999999999999);
var modurl = myurl+"?"+PortfolioNumber+"&rand="+myRand;
alert (modurl);
http.onreadystatechange = useHttpResponse;

function useHttpResponse(){
if (http.readyState==4){
if (http.status ==200) {
var mytext = http.responseText;
document.getElementById('myPageElement').innerHTML = mytext;
else {
document.getElementById('myPageElement').innerHTML = "";

Here is the text returned by the server: <br>
$qMaster = "SELECT DISTINCT PortfolioNumber FROM art WHERE 1 ORDER BY PortfolioNumber";
$rMaster = @mysqli_query($dbc,$qMaster);

//$row = mysqli_fetch_array($rMaster,MYSQLI_NUM);

while ($row = mysqli_fetch_array($rMaster,MYSQLI_ASSOC)){
?><p onclick="getServerText('PortfolioNumber=<?=$row['PortfolioNumber']?>');"> here you go <?=$row['PortfolioNumber']?></p><br>
} else {
echo mysqli_error($dbc);
<div id="myPageElement"></div>

And now here is the 'textserver.php' page which feeds into the first page and should be filtered based on what is clicked on the 'Ajax1.php' page.


$q = "SELECT * FROM art WHERE PortfolioNumber = $id ORDER BY IDNumber";

} else {
$q= "SELECT * FROM art ORDER BY IDNumber";
$r = @mysqli_query($dbc,$q);

echo $q;?>
<table border="1px"><tr><b><td>IDNumber</td><td>Title</td><td>Portfolio Number</td><td>Medium</td><td>On web</td></b></tr>
while ($row = mysqli_fetch_array($r,MYSQLI_ASSOC)){
{echo mysqli_error($dbc);}
<?php mysqli_close($dbc); ?>
Hopefully my explanations are fullsome. Up to this point I've tried tweaking this and that but to no effect. Help!

09-17-2009, 07:14 PM
You've misspelled a variable name... if(isset($_GET['PorfolioNumber'])){

09-17-2009, 07:16 PM
...I hope that's all it is! I'll make the correction and then give it another whirl. Thanks!

09-17-2009, 07:18 PM
...yeah, that was it, a stupid spelling error. D'oh!
Thanks again