简体   繁体   中英

Using jQuery or Ajax get function to pull in text from another page

The short story is our CMS limits us to using solely html/js. I'm creating a dynamic graph page that has a lot of data that will be updated monthly. There is a separate php form on the server the staff member will fill out, that content is stored in the database, and there's another php file that displays the information. Jquery/Ajax needs to pull that info from the outside php page, which I've done on one other project. This difference this time is that I need to target specific sections of code to pull in, because the results will be going into a javascript variable for use in populating a graph.

With me so far? What I need jquery/ajax to do is target specific div ID's on the populating php page so I can direct it to the proper js variable.

I've looked at jston, but am not strong with JS, and thus that looks like Greek. Thoughts, Ideas, Suggestions?

EDIT:

I've tried this code:

$('#hiddenDiv').load('test.php #newdata');

Which imported nothing, even though there is text on the test.php page with a div with the id "newdata".

You can use the jQuery load function:

$('#loadIntoMe').load('somepage.php #onlyLoadMe');

If there is a space in the URL provided as a parameter to the load function, it's assumed to be a jQuery selector and is used to extract the specified elements, instead of returning the entire document. See the docs for more information.

Update (based on comments)

To store the result in a variable, you could use a hidden div and a callback function:

var yourVariable;
$('#loadIntoMe').load('somepage.php #onlyLoadMe', function() {
    yourVariable = $(this).html();
});

All javascript graphing libraries I have employed have required a javascript object to be passed to them. In this instance I would be use jQuery $.get to get a json_encode d set of data.

PHP

<?php
header('Conent-type: application/json');
$array_of_data = array(
    'axes1' => array( // data goes here
    ),
    // etc and so on
);
echo json_encode($array_of_data);
?>

JS

$.getJSON('/url/to/php.php', function(data) {
    // console.log(data);
    // generate graph from data
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM