0

I am trying to get several variable results from php with jquery json. The problem is that I get null values in the console (on title for example) and script failed. With this script I am trying to populate some input fields with data that is being send from php. I am a noob in ajax json. Please help me.

HTML

<input id="titledata" type="text" name="siteTitle" value="" />
<textarea id="itemDescription" name="description"></textarea>
<input id="suggestkeyworddata" type="text" name="proposalForKeywords" value="" />

JQUERY - here I am trying to get the variables and store them into jquery variables: var title = data.titledata;....

<script type="text/javascript">
$(function checkdomain() {
    jq2('#metaTagButtonz').on('click', function (e) {
        $.ajax({
            type: 'post',
            url: 'getallinfos.php',
            data: $('#urlpr').serialize(), // sending data to php from this field
            dataType: 'json',
            success: function (data) {
                $("#domain-hits").html(data);
                         // here i am trying to get data from php
                var title = data.titledata; 
                var description = data.descriptiondata;
                var keywords = data.keywordsdata;
                         // here I am trying to populate the data into the 
                         // input fields
                $('#titledata').val(title); 
                $('#itemDescription').val(description);
                $('#keywordswebsite').val(keywords);
            }
        });
        e.preventDefault();
    });
});
</script>

PHP

    //$title, $descr, and $keywords are strings 
    (sometimes empty sometime have values depending on the website)
$data = array(
    'titledata' => $title,
    'descriptiondata' => $descr,
    'keywordsdata' => $keywords,
);
$data = json_encode($data);
user3650099
  • 131
  • 1
  • 7

1 Answers1

2

You just need to print the actual content from your php script:

header('Content-Type: application/json');
echo json_encode($data);
Stefano Ortisi
  • 5,288
  • 3
  • 33
  • 41