Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2009
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy Pass JavaScript (via AJAX) to PHP

    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)
    Code:
    <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.

    Code:
    <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!

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You've misspelled a variable name... if(isset($_GET['PorfolioNumber'])){

  • #3
    New Coder
    Join Date
    Sep 2009
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    ...I hope that's all it is! I'll make the correction and then give it another whirl. Thanks!

  • #4
    New Coder
    Join Date
    Sep 2009
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    ...yeah, that was it, a stupid spelling error. D'oh!
    Thanks again
    -Pindi


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •