#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0

    GetJSON response empty from PHP file


    When sending the request from the jQuery script to the specified PHP file, nothing is returned, nothing is appended to the html file. Here's the URL of the page:

    localhost/basket/newstext.html?url=basket-planet.com/ru/news/9235

    newstext.html:

    <head>
    <script src="js/newstext.js"></script>
    </head>
    <body>
    <div data-role="page" id="newstext">
    <div data-role="content">
    <div id="textcontent"></div>
    </div>
    </div>
    </body>

    newstext.js:

    var serviceURL = "http://localhost/basket/services/";
    $('#newstext').bind('pageshow', function(event) {
    var url = getUrlVars()["url"];
    $.getJSON(serviceURL + 'getnewstext.php?url='+url, displayNewsText);
    });

    function displayNewsText(data){
    var newstext = data.item;
    console.log(newstext);
    $('#textcontent').text(newstext);
    $('#textcontent').trigger('create');
    }

    function getUrlVars(){
    //it displays in the alert perfectly, shortening the message here
    }

    getnewstext.php:

    <?php
    include_once ('simple_html_dom.php');
    $url = $_GET['url'];
    $html = file_get_html(''.$url.'');

    $article = $html->find('div[class=newsItem]');
    $a = str_get_html(implode("\n", (array)$article));
    //parse the article
    header("Content-type: application/json");
    echo '{"item":'. json_encode($a) .'}';
    ?>

    I think my problem is how I'm encoding the $a variable in the PHP script. The $a variable contains html tags of all kind...how can I append it in the html file?
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    there are some simple ways to find out where the problem is:

    Check the output of your PHP script: Open the developer tools of your brower, open the "Network" tab and filter by "XHR". You'll see your "getnewstext.php". Click on it and check the response. You can also test your PHP script separately by simply executing it directly (of course you have to pass the "url" parameter).

    Paste the output here.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0
    Hi Jacques,

    I tried this and I saw there is no PHP file after the page is loaded. There is nothing in XHR. Does that mean the response isn't working, nothing is getting returned?

    I inserted the link directly. I got a huge output. I get this warning probably more then 100 times:

    <b>Warning</b>: json_encode(): recursion detected in <b>C:\xampp\htdocs\Basket\services\getnewstext.php</b> on line <b>30</b><br />

    Line 30 is the echo '{"items":.....}';

    After all these warnings, I get {"item":{"root":{"nodetype":5,"tag":"root","attr":[]...

    The output is very very big.

    Maybe to clear things up...the user clicks on a news link in news.html. The link of the news article gets appended to the url. On pageshow in jQuery, I'm getting that news article url and passing it to php. Initially in the XHR, news.html returns the PHP file with the news. When I click on an article, "newstext.html?url=basket-planet.com/...." gets returned, not the PHP script. I don't understand what this means...
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Obviously it wasn't a good idea to JSON-encode your HTML object directly.

    The more I think about it, the stranger I find this whole approach. OK, so you have an array of DOM objects (I guess) in $article. You concatenate the objects' HTML representations, parse this again as a DOM object (why??) and then encode this object as JSON (why??). This way you end up with a big JSON object.

    Why not stop after the string concatenation and simply output the HTML? Why do you even need JSON?

    PHP Code:
    $articles $html->find('div[class=newsItem]'); 
    echo 
    implode("\n"$articles); 
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0
    I know this seems strange. I'm using a web service called buzz touch to make an app for my friends basketball website. I'm separating my HTML from PHP so I can load the HTML CSS and js files into.the source code and it will speed up the app since the files will be local. I'm using json encoding so I can get the content back to the js script and load it in my HTML. So the only http call made will be to the PHP file on my server. I hope that cleared things up a little. And the reason why I'm not leaving it as $article is because I modify the content to eliminate images, iframes, etc...I turn $articles to a string and encode it so I can use .getjson in my jquery script.
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    You do not need JSON to fetch a resource through AJAX. Just get the raw HTML as a string.

    The purpose of JSON is to transfer actual data. Something like:

    Code:
    {
    	"name": "Peter"
    	, "age": 44,
    	, "married": true
    }
    But you don't have data. Your JSON object just wraps the HTML string, which isn't very useful.

    The same goes for turning $article into a string, parsing it and turning it again into a string. That does nothing but keep your CPU busy. If you wanna edit $article, do it right away and then stringify the final content.

    You know, you're doing a lot of back-and-forth parsing. Since you said that you're doing this all for speed, it's pretty odd to waste your resources like that.
    Last edited by Jacques1; April 5th, 2013 at 08:44 AM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0
    I understand that this may be a bad way to do things but I'm using PHP Simple DOM parser and in their documentation the only way to modify the parsed HTML is to turn it into a string. This I'd the first way I found. Can you tell me how I can return this $a content to my js and append it to my div? I'm not sure what Ajax request jquery mobile accepts with the .bind(page show)...

IMN logo majestic logo threadwatch logo seochat tools logo