-2

I have used an example table from X-Editable, this uses an array in JQuery to populate a drop down options box. This is great but I want to use the database to feed the array.

This below is the code from the X-Editable demo

var jsStringFromPHP = "<?php echo json_encode($r); ?>";

$.mockjax({
url: 'myphppage.php',
response: function (settings) {
    this.responseText = jsStringFromPHP;
    log(settings, this);
}
});

This is what I've done to get the array from my database

<?php
require 'assets/functions/core/init.php';
$p = db::getInstance()->query('SELECT id as value, name as text FROM information', array());
    $r = $p->results();
    echo json_encode($r);
?>

How can I incorporate the php array into the jquery and replace the static list?

6
  • remove the <pre> tag, and use the result of your php output as a json object via ajax. Commented Oct 28, 2014 at 11:22
  • Thanks but after this.responseText = in the jquery how do I call it? Commented Oct 28, 2014 at 11:26
  • you receive your response json in settings argument of your ajax callback. Commented Oct 28, 2014 at 11:29
  • this.responseText = {settings} i guess Commented Oct 28, 2014 at 11:30
  • Sorry don't understand the {settings}, what does that represent, I'm trying to understand how I can call the php code to run within the jqUery Commented Oct 28, 2014 at 11:59

1 Answer 1

2

Just encode your array to JSON using json_encode & return that in response. Then in success callback of ajax function, use that JSON string.

To know more about how to handle it, read it: Load JSON Data Using jQuery AJAX

Use in this way,

$.mockjax({
    url: "hello.php",
    proxy: 'sample.json',
    responseTime: 0,
    dataType: 'json'
});

$.ajax({
    url: "hello.php",
    dataType: 'json'

}).done(function (json_response) {

    var names = [];
    for (i in json_response.employees) {
        names.push(json_response.employees[i].firstName);
    }
    output = names.join("<br/>");
    $('#output_fake_json').html(output);

    $.mockjaxClear();
});

More details can be found here on plugin page: http://www.vikaskbh.com/jquery-fake-ajax-requests-for-ajax-testing-using-mockjax-plugin/

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

4 Comments

Thanks, I've had a look at that and amended my original post, I don't understand where I can call the actual php page from to run the echo of json_encode?
code & tutorial added in the last of my answer. This will help you.
@539 You are calling the php with the url in ajax (e.g hello.php)! Do some reading to get the concepts right :)
Thanks for the help, I had tried the php inside the url first, with it showing errors in the console log and then after much reading changed it to the /groups as that was how the X-Editable site had shown it. After changing it back, some reason it has worked.

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.