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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Javascript/PHP/Sajax issue

    Hi Guys,
    I'm somewhat of a n00b at Ajax, and wanted to add ajax functionality to a new website I'm developing, and I wanted to use a relatively lightweight framework, so I've started with Sajax. I'm running into a slight problem, however. I've got a page with four different divs to which I want to send different content (depending on what the user selects), and the content is being served in a bunch of different pages, but Sajax only allows a single destination ($sajax_remote_uri). I've been wracking my brains trying to figure out a way to include a different destination url for each function, which I thought I could do by making $sajax_remote_uri an array, composed of "func_name"=>"remote_uri", then setting up an array (function_urls) in the Javascript portion of Sajax.php, which would then get populated by echoing the value from php, but when I try to echo the value into the array by:
    function_urls['<?=$func_name;?>']='<?php echo $sajax_remote_uri[$func_name];?>';

    I get nada. Any thoughts? I know it is able to get the $func_name part - that comes through just fine, and when I just put $sajax_remote_uri into there, it says it's an array, (so I'm assuming it's getting the right thing for that), but I can't seem to echo a specific value from the array...

    Any thoughts? Any/all help would be hugely appreciated. I've been agonising over this for days...

  • #2
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Found the solution

    Never mind - I found a fairly simple solution, I will post it here in case others want to modify Sajax to do this (seems a little funny to me that they haven't before, but I guess nobody has worked on Sajax for a while now). Anyway, the key thing which I failed to remember was that PHP can talk to Javascript, but not the other way around. So the thing to do is to set up the array in PHP and then echo (or print) those values into the javascript code to populate the array in Sajax.

    Here are the changes I made:

    At the top of the PHP page, instead of having:

    Code:
    require("Sajax.php");
    $sajax_remote_uri = "targetpage.php";
    sajax_init();
    #$sajax_debug_mode=1;
    sajax_export("func_name1");
    sajax_export("func_name2");
    sajax_export("func_name3");
    sajax_handle_client_request();
    ?>
    I now have:

    Code:
    require("Sajax.php");
    #$sajax_remote_uri -- declaration is no longer required here
    sajax_init();
    #$sajax_debug_mode=1;
    sajax_export("func_name1");
    sajax_export("func_name2");
    sajax_export("func_name3");
    sajax_handle_client_request();
    ?>
    Then, after these tags:

    Code:
    ?>
    <script type="text/javascript">
    <?php
    and just before these tags:

    Code:
        sajax_show_javascript();
    ?>
    I have inserted the following code:

    Code:
    $function_urls = array("func_name1"=>"targetpage1.php","func_name2"=>"targetpage2.php","func_name3"=>"targetpage3.php");
    print " var function_urls = new Array();\n";
    foreach ($function_urls as $func_name => $url)
    {
        print " function_urls[\"$func_name\"] = \"$url\";\n";
    }
    Once you've done that (and populated the php $function_urls with the correct function names and target page names), you just replace the following line in Sajax.php:
    replace:

    Code:
    uri = "<?php echo $sajax_remote_uri; ?>";  //(in sajax_do_call function)
    with:

    Code:
    uri = function_urls[func_name];
    This seems to work just fine. :-)


  •  

    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
    •