2

I am tring to write a page, which takes a RSS feed from a news site via AJAX and then sends it to PHP where I can work with it. The news feed is returned as an object array. I have tried posting it as it is, and also as a json string. The post method seems to be a success, but PHP gives an undefined index notice. This is my first time using AJAX and PHP and I seem to have problem with getting the data from the PHP side.

The error:

Notice: Undefined index: data in ...\index.php on line 33

Current code is the following:

ajax side

url = 'http://feeds.bbci.co.uk/news/rss.xml?edition=int';

$.ajax({
    type: "GET",
    url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(url),
    dataType: 'json',
    error: function(){
            alert('LOAD ERROR, INVALID URL');
    },
    success: function(xml){
            values = xml.responseData.feed.entries;
            var senddata = JSON.stringify(values);
            console.log(senddata);
            $.ajax({
                type: "POST",
                url: "index.php",
                data: {data : senddata},
                success: function(){
                    alert("postdone!");
                },
                error: function(){
                    alert("posterror!")
                }
            });
        }
});

php side

<?php
    $data = json_decode(stripslashes($_POST['data']));
    echo $data;         
?>
2
  • nevermind, I didn't notice the nested ajax. Does console.log(senddata); return anything? Commented Jul 21, 2013 at 14:48
  • Two suggestions: First, rename your parameter 'data' to something else. Perhaps there is some confusion since the parameter name matches the .ajax key name. Second, dump the contents of $_POST (var_dump($_POST);`) to see what it contains. Is your data there, perhaps in a place you didn't expect? Commented Jul 21, 2013 at 15:12

1 Answer 1

2

Wrap your code in an if to avoid that warning:

if (isset($_POST['data'])) {
    $data = json_decode(stripslashes($_POST['data']));
    echo $data;  
}

The problem is when you visit that index.php from browser, there is no POST request, so of course $_POST is empty and $_POST['data'] is not set.

Hope you get the point.

EDIT:

Hmm I can't see anything seriously wrong. And actually now I recommend you to use php.net/manual/en/book.curl.php to get the data directly from the RSS, instead of nesting 2 ajax calls.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the answer! Not sure if i get the point. The if condition will just ignore the warning. I can see via the console log that the ajax GET is done and I also get the alert that the ajax POST is done. I guess my question in a nutshell would be - how can I get the newsitems (values OR senddata) from the RSS into PHP, so i can work with these (put certain attributes into a database etc)?
So both ajax are sent successfully. You can do a var_dump($_POST); in PHP part to debug your call then.
var_dump($_POST) returns array(0) { } So I'd assume the problem is that I am sending an empty array, hence there is something wrong with the ajax POST method? console.log(senddata), which is before the ajax POST, returns me an array of key:value pairs, which most certainly are present. Any ideas what could be wrong with the POST method?
Hmm I can't see anything seriously wrong. ANd actually now I recommend you to use php.net/manual/en/book.curl.php to get the data directly from the RSS, instead of nesting 2 ajax calls

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.