passing URL from AJAX to PHP file: Urgent help required.

Discussion in 'PHP' started by smart, Oct 10, 2008.

  1. #1
    Hi guys i have small problem , plz help me.

    when i put the direct URL address, it works. If pass that URL address and try to use it wont work.


    here is the code.




    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>
    <title>Untitled</title>
    <script type="text/javascript">
    <!--

    //Create a boolean variable to check for a valid Internet Explorer instance.
    var xmlhttp = false;

    //Check if we are using IE.
    try {
    //If the Javascript version is greater than 5.
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    alert ("You are using Microsoft Internet Explorer.");
    } catch (e) {
    //If not, then use the older active x object.
    try {
    //If we are using Internet Explorer.
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    alert ("You are using Microsoft Internet Explorder");
    } catch (E) {
    //Else we must be using a non-IE browser.
    xmlhttp = false;
    }
    }

    //If we are using a non-IE browser, create a javascript instance of the object.
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    xmlhttp = new XMLHttpRequest();
    alert ("You are not using Microsoft Internet Explorer");
    }

    function callfunc(serverPage,num,objID) {
    var obj = document.getElementById(objID);

    //xmlhttp.open("GET", "serverPage?urlname=" + num, true); not working
    //if i give the URL name directly here, it works
    xmlhttp.open("GET", "content1.php?urlname=" + num, true); //works
    xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    obj.innerHTML = xmlhttp.responseText;
    }
    }
    xmlhttp.send(null);
    }

    //-->
    </script>
    </head>
    <body>

    <?php

    echo "This is my first example <br>";

    ?>

    <a href="#" onclick="callfunc('content1.php','6','url'); return false;">CLICK HERE</a>

    <div id="url"> </div>
    </body>
    </html>
     
    smart, Oct 10, 2008 IP
  2. itnashvilleCOM

    itnashvilleCOM Banned

    Messages:
    176
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are you calling the variable right in the php script? Example:

    <?php

    if (!empty($_REQUEST['VARIABLENAME'])) {
    $variable = trim($_REQUEST['VARIABLENAME']);
    } else {
    die('variable not sent correctly.');
    }

    ?>
     
    itnashvilleCOM, Oct 10, 2008 IP
  3. smart

    smart Active Member

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #3
    yes. I am calling that.
     
    smart, Oct 10, 2008 IP
  4. ZenOswyn

    ZenOswyn Peon

    Messages:
    50
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You'll save yourself a lot of trouble if you use jQuery, or a similar framework.
     
    ZenOswyn, Oct 11, 2008 IP
  5. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    If i understand well serverPage is a javascript variable passed by the function callFunc.

    Instead of
    xmlhttp.open("GET", "serverPage?urlname=" + num, true);
    do
    xmlhttp.open("GET", serverPage+"?urlname=" + num, true);

    Javascript doesn't parse like PHP!


    A nice way to avoid problems is... to create others!!!
     
    webrickco, Oct 11, 2008 IP
  6. smart

    smart Active Member

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #6
    @ webrickco

    done as you said, but still not working.

    Help please
     
    smart, Oct 11, 2008 IP
  7. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #7
    This should be working!

    What does it display when you write alert(serverPage); before calling the open function? does it display content1.php?

    You can also do like this to check if what you want is correctly called:

    var url = serverPage+"?urlname=" + num;
    alert(url);
    xmlhttp.open("GET", url, true);
     
    webrickco, Oct 11, 2008 IP
  8. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #8
    get firefox and firebug, look at error console (press f12), you can also set breakpoints and watch variables. what webricko said is correct and should work, if you are unsure, just copy and paste xmlhttp.open("GET", serverPage+"?urlname=" + num, true);

    http://getfirebug.com/ - firebug is a plugin for debugging JS,walking DOM and CSS / eidting on the fly. There is a plugin for firebug called firePHP which works on the basis of additional headers and can output debug information to the browser console.
     
    dimitar christoff, Oct 11, 2008 IP
    webrickco likes this.