Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

accessing functions via url

Options
  • 13-06-2008 1:41pm
    #1
    Registered Users Posts: 8,070 ✭✭✭


    is it possible to pass parameters via url to an ajax function


    i have an ajax function that loads an external html page in a div

    [PHP]
    function processAjax(url) {


    if (window.XMLHttpRequest) { // Non-IE browsers
    req = new XMLHttpRequest();
    req.onreadystatechange = targetDiv;

    try {
    req.open("GET", url, true);
    }
    catch (e) {
    alert(e);
    }

    req.send(null);
    }

    else if (window.ActiveXObject) { // IE
    req = new ActiveXObject("Microsoft.XMLHTTP");
    if (req) {
    req.onreadystatechange = targetDiv;
    req.open("GET", url, true);
    req.send();

    }
    }

    return false;




    [/PHP]

    etc etc
    so wondering if i can specify the url and div all in the url [web address] so that specific div/page loads via the link


    thanks


Comments

  • Closed Accounts Posts: 1,200 ✭✭✭louie


    Of course you can but you can also have the url set before hand
    [php]
    function processAjax(url,div){
    .............
    }
    //or
    var url = "http://..............."; //before the function itself
    [/php]


  • Registered Users Posts: 8,070 ✭✭✭Placebo


    why cant i just state the url

    so


    hxxp://example.com/hello.php?processAjax(./hello2.html);

    shud work?
    doesnt


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    Placebo wrote: »
    why cant i just state the url

    so


    hxxp://example.com/hello.php?processAjax(./hello2.html);

    shud work?

    Nope, it shouldn't.

    The URL doesn't know what's in the php file that it's calling.

    The query part of the URL is only available via $_GET or $HTTP_GET_VARS.

    How you use that within your php code is up to you, but it's not designed to do what you're trying and it won't work.

    You could possibly call something like

    http://example.com/hello.php?fn=processAjax&param=hello2.html

    and then within the php call the following:

    $_GET["fn"]($_GET["param"]);

    But I can think of a million security issues with that, such as (off the top of my head)

    http://example.com/hello.php?fn=unlink&param=hello.php

    P.S. Don't try the above if you've just created hello.php !!!!


  • Registered Users Posts: 8,070 ✭✭✭Placebo


    thanks liam, theres actually no php code,
    its a wordpress site with some ajax loading external pages,

    ##thanks


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    Placebo wrote: »
    thanks liam, theres actually no php code,
    its a wordpress site with some ajax loading external pages,

    ##thanks

    OK, now I'm REALLY confused because the URL in your example included "hello.php"


  • Advertisement
  • Registered Users Posts: 3,886 ✭✭✭cgarvey


    Placebo wrote: »
    hxxp://example.com/hello.php?processAjax(./hello2.html);

    You can use location.search to get the function name/arg from above
    example. You'll need to have some onLoad event JS firing in the hello.php HTML (that is; client-side, not server-side PHP) to actually check for the function/arg and execute it. So there's no way to call a HTML file (whether it's PHP-produced is irrelevant) and have that file execute JS from the URL. You can have JS in that HTML file check & execute whatever has been specified in the URL, but it has to be the JS in the HTML that initiates that, not your mere provision of JS in the URL. If you read that a few times it'll probably make more sense!


Advertisement