0

The Photo.php file is expected to show a message.

But when I add ?page=2 to address it doesn't call Photos function to open the Photo.php file.

function Photos() {
            var opt = <?php echo $_GET['page'];?>
            alert(opt);
            if(window.XMLHttpRequest)
            {
                xmlhttp = new XMLHttpRequest();
            }
            else
            {
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
                if(xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("Results").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET","Photo.php?page="+opt,true);
            xmlhttp.send();
        }
            function showCat(option){
                 ....
           }


<?php 
                   if(isset($_GET['page'])){
            echo '<script type="text/javascript">'
            ,'myPhotos();'
            , '</script>';
        }
                echo 'Results go here'; 
                ?>

photo.php

if(isset($_GET["page"])){
    echo "photo";
}
13
  • 4
    var opt = '<?php echo $_GET['page'];?>'; Commented Feb 3, 2013 at 9:01
  • 5
    Don't echo user input directly out to the page. It makes you vulnerable to XSS attacks! Commented Feb 3, 2013 at 9:03
  • 2
    If you have PHP generating JavaScript/HTML and "it doesn't work". Then look at the source in your browser. Determine if the problem is that the client side code doesn't do what you expect (in which case, don't show us the PHP code) or if the PHP doesn't generate the client side code you expect. Commented Feb 3, 2013 at 9:05
  • 1
    Does the request fail or is it just blank? This is relevant to TJ's answer. Commented Feb 3, 2013 at 9:06
  • 1
    Then try installing firebug and see what you get in the console. It probably is a syntax error. Your post still shows var opt = <?php echo $_GET['page'];?> instead of var opt = '<?php echo $_GET['page'];?>' Commented Feb 3, 2013 at 9:31

2 Answers 2

6

xmlhttp.open("GET","Photo.php?page"+opt,true);

I think you're missing an = in there...

xmlhttp.open("GET","Photo.php?page="+opt,true);
// Here --------------------------^

At least, that's what it looks like. It looks like you're accepting a GET argument, and then calling Photo.php and passing in a query string where page is the name of the query parameter.

Side note: All URL parameters must be correctly encoded. So you should be using encodeURIComponent there as well:

xmlhttp.open("GET","Photo.php?page="+encodeURIComponent(opt),true);

(Technically both the key and value have to be URI encoded, but the URI-encoded version of "page" is "page", so...)


All modern browsers have fairly useful debugging tools now, by the way. For instance, in Chrome you can open the Developer Tools and look at the Network tab to see exactly what you're sending the server and what it's sending back. Very useful for this sort of thing.

Sign up to request clarification or add additional context in comments.

3 Comments

@I did both still same problem
@SaeedPirdost: Well, you definitely do need the =, but apparently there's also something else wrong. Only thing for it is to debug it (see the note I added to the end of the answer a couple of minutes ago).
@SaeedPirdost: Again: "...apparently there's also something else wrong..." and "Only thing for it is to debug it..."
0

As Akam noticed; you forgot to put quotes around the string variable:

var opt = <?php echo $_GET['page'];?>

Should be:

var opt = '<?php echo $_GET['page'];?>';

This should have cased a JavaScript error that could have been noticed in the console of Firebug. Firebug is a very handy plugin to work out JavaScript or network problems. In the console you can find xmlhttpreqest with request and response headers as well as JavaScript errors and if you need to see details of JavaScript objects you can console.log(myObject) to the console where it can be clicked on to show more details.

I'm not sure if chrome has the same features but if you don't have Firefox or just like Chrome better then pressing F12 will open developer tools, same as in Opera and Internet explorer although I personally prefer Firefox with firebug plugin.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.