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);

1 Answer 1

2

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

header('Content-Type: application/json');
echo json_encode($data);
Sign up to request clarification or add additional context in comments.

7 Comments

ok, working on getting the data now, but is not populating the fields.
In the console I am getting "{\"titledata\":\"TITLE OF SITE\",\"descriptiondata\":\"DESCRIPTION OF SITE\",\"keywordsdata\":\"KEYWORDS OF SITE\"
Tried on doing an alert(titledata); And I get the alert box containing object HTMLInputElement
So now you need to fix your backend code. It depends on what you're doing before printing the data.
All of the variables in php mentioned contain the data, but it's not outputted correctly, don't understand why not.
|

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.