...

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



pindi
09-17-2009, 05: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)

<html>
<head>
<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.open("GET",modurl,true);
http.onreadystatechange = useHttpResponse;
http.send(null);
}

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

</script>
</head>
<body>
Here is the text returned by the server: <br>
<?php
require_once('connect.php');
$qMaster = "SELECT DISTINCT PortfolioNumber FROM art WHERE 1 ORDER BY PortfolioNumber";
$rMaster = @mysqli_query($dbc,$qMaster);

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

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


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.


<html>
<?php
require_once('connect.php');
if(isset($_GET['PorfolioNumber'])){
$id=$_GET['PortfolioNumber'];

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

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

//$row=mysqli_fetch_array($r,MYSQLI_NUM);
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>
<?php
if($r){
while ($row = mysqli_fetch_array($r,MYSQLI_ASSOC)){
?>
<tr><td><?=$row['IDNumber']?></td><td><?=$row['Title']?></td><td><?=$row['PortfolioNumber']?></td><td><?=$row['Medium']?></td><td><?=$row['Web']?></td></td>
<?php
}
}
else
{echo mysqli_error($dbc);}
?>
</table>
<?php mysqli_close($dbc); ?>
</html>
Hopefully my explanations are fullsome. Up to this point I've tried tweaking this and that but to no effect. Help!

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

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

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum