Here's my problem: I created a json file with php and I need to access it with javascript (ajax?) in order to print some result on my map.
For me it's very hard, but I'm giving a try step by step.
Here's the first step:
function phptojson($prog = null , $prov = null, $slang = null , $fees = null) {
$posts = getData(null , null, null, null);
$data = [];
foreach($posts as $post) {
$data[$post->ID] = [
"program_type" => get_post_meta( $post->ID, 'program_type', true ),
"province" => get_post_meta( $post->ID, 'province', true ),
"language" => get_post_meta( $post->ID, 'language_of_study', true ),
"tuition" => get_post_meta( $post->ID, 'tuition_fees', true ),
"lenght" => get_post_meta( $post->ID, 'length_of_study', true ),
"title" => $post->post_title,
"location" => get_post_meta( $post->ID, 'location', true ),
"information" => get_post_meta( $post->ID, 'details', true ),
];
}
return json_encode($data);
}
getData is a function that will call some parameter in order to filter my query, and return data from the database that I need. The second step is the loop, which is working fine, and finally I return my json file.
Now my trouble, which is: what could I do with this file. The older code that I'm improving was using a script tag in order to print the file on the HTML document to access it.
I want to call my funtion phptojson from my javascript file in order to access it. I've aready created the google maps, which is working fine, so I only need to print the result from my javascript function. My file hierarcy looks like that:
wp-content -themes -mytheme myfunction.php (where I put phptojson() ) -js myscript.js
Essentially I want to call my function from myscript.js with ajax and I'm trying this approach that I found here, but it's not working:
function getJsonElement() {
$.get('/includes/custom-function.php', function(phptojson) {
console.log(data);
} );
}
Which kind of makes sense.. What could it be my next step?
PS. My json file looks like:
string(162036) "{"1439":
{"program_type":"long","province":"NY","language":"EN","tuition":"10","lenght":"full","title":"My School","location":"10 awesome street, NY, US"}}