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 4 of 4
  1. #1
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    333
    Thanks
    2
    Thanked 47 Times in 46 Posts

    JSON / JSONP / XML cross-domain data delivery

    I have some values that i get from the database, then import to variables like:

    PHP Code:
    <?php
    $value1 
    "Value1 returned by database";
    $value2 "Value2 returned by database";
    $value3 "Value3 returned by database";
    $value4 "Value4 returned by database";
    ?>
    So the problem is getting these variables and displaying them in other domains. It's supposed to be a user self-marketing thing where they can put code like below in any website:

    PHP Code:
    <script src="http://mywebsite.com?id=2">
     
    </script>
    //Where id=2, 2 is the user ID 
    So then the variables $value1, $value2, $value3,$value4 can be displayed at the other(users website or wherever they decide to post that code).

    I think this can be done using JSON, XML but i think JSONP is suitable because it is touted as cross-domain. It will need to be cross-domain as the user can be able to post this code wherever they choose. I

    I have researched but clear JSONP documentation on how to do such a simple thing and manipulate the received variables in order to display them as you want is not if ever easily found.

    Explain below which tech i can use for this and how to go about it - it will be appreciated. Remember what will be used will need to be cross-domain as this will be like those affiliate links although this time this deliver some data to the website the code is at.


  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    here is one way ...

    PHP Code:
    <?php
    $value1 
    "Value1 returned by database";
    $value2 "Value2 returned by database";
    $value3 "Value3 returned by database";
    $value4 "Value4 returned by database";

    echo <<<JAVASCRIPT
    var value1 = "$value1";
    var value2 = "$value2";
    var value3 = "$value3";
    var value4 = "$value4";
    JAVASCRIPT
    ?>

  • #3
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    333
    Thanks
    2
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by DaveyErwin View Post
    here is one way ...

    PHP Code:
    <?php
    $value1 
    "Value1 returned by database";
    $value2 "Value2 returned by database";
    $value3 "Value3 returned by database";
    $value4 "Value4 returned by database";

    echo <<<JAVASCRIPT
    var value1 = "$value1";
    var value2 = "$value2";
    var value3 = "$value3";
    var value4 = "$value4";
    JAVASCRIPT
    ?>
    So how do i write the code that interprets that on the client-side where the code was posted?

    It needs to be as simple as:
    PHP Code:
    <script src='http:/mywebsite.com/display_info.js' >
    </script> 
    Thanks.

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Code:
    <script src="http://mywebsite.com/js.php">
     </script>
    <script>
    alert(value4)
     </script>
    js.php
    PHP Code:
    <?php 
    $value1 
    "Value1 returned by database"
    $value2 "Value2 returned by database"
    $value3 "Value3 returned by database"
    $value4 "Value4 returned by database"

    echo <<<
    JAVASCRIPT 
    var value1 "$value1"
    var 
    value2 "$value2"
    var 
    value3 "$value3"
    var 
    value4 "$value4"
    JAVASCRIPT 
    ?>
    Last edited by DaveyErwin; 08-29-2012 at 08:40 PM.

  • Users who have thanked DaveyErwin for this post:

    Redcoder (08-30-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •