PHP, JSON and JavaScript in Web2.0 Applications - Part 2

Go To Part 1 of PHP, JSON and JavaScript in Web2.0 Applications

We will now move on to the server side script GiveAd.php

  • PHP Script on the server should reply in JSON – enabling JavaScript to receive the output. Here is an example of PHP Script doing just that. GiveAd.php serves as the back end to ShowAd.html shown in Part 1 of PHP, JSON and JavaScript:
    <?php
    /**
    * This is Ad serving file
    * @desc 

    */

    //This is the whole magic - calling DebugBreak() will automatically start debugging session
    DebugBreak();

    $clientId = (isset($_GET["clientId"]) ? $_GET["clientId"] : ""); 
    $callback $_GET["callback"];
     
    //DebugBreak();

    $ad null;
    //select corresponding advertisement
     
    switch ($clientId) {
        case 
    "really_cool_cars":
            
    $ad ads_for_grownups();
            break;
        case 
    "really_cool_toys":
            
    $ad ads_for_kids();
            break;
        default:
            
    $ad ads_for_everybody();
     }
     
     
    //encode output in JSON
     
    $output json_encode($ad);
     
     
    //  send it back to the caller
     
    print ($callback."(".$output.");");
     
     
     
     
    //Grownups love sale and cars
     
    function  ads_for_grownups() {
     
    $value =  array ('Title'=>'Cheapest Used Cars!'
                      
    'Ad_Body' => array ('Punch_line'=> 'On line super sale car store'
                                                
    'URL'=>'www.supercheapcars.com'));
     return 
    $value;
     }
     
     
     
    //Kids love Candy (at least we would like to think that)
     
    function  ads_for_kids() {
     
    $value = array('Ad' =>  array ('Title'=>'Best Candy store!'),
                    
    'Ad_Body' => array ('Punch_line'=> 'On line Candy supermarket!''URL'=>'www.allthingscandy.com'));
     return 
    $value;
     }
     
     
    //For now return grownups ad for everybody
     
     
    function  ads_for_everybody() {
     
    $value ads_for_grownups();
     return 
    $value;
     }
    ?>
  • We provide detailed description of the debugging techniques using the example of these two scripts in our article: Debugging PHP, JSON and JavaScripts Application. In that example we used NuSphere’s PhpED and dbg – PHP debugger to present the advantages of good PHP Editor/IDE. Following the instructions there you can step through JavaScript in FireFox and step through PHP Code in PhpED . You will see how when the following code segment is executed, GET request is being sent to PHP script, which you can debug using good PHP IDE and debugger:
    var script = document.createElement("script");
            script.setAttribute("id", "dynamic_script_injection_node");
            script.setAttribute("type", "text/javascript");
            script.setAttribute("src", request);
            // all set, add the script
            head.appendChild(script);

    Dynamic script injection doesn't suffer from the traditional limitations of XMLHttpRequest, such as prohibition of cross-domain scripting. For example, JavaScript on any of your partner's or customer's web site can use Dynamic script injection to send the request to PHP scripts on your site and your PHP can reply in JSON, providing dynamic content to multiple sites on different domains.

    Go To Part 3 of PHP, JSON and JavaScript in Web2.0 Applications
    Go To Part 1 of PHP, JSON and JavaScript in Web2.0 Applications
    © Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.