Have a php code selecting from SQL with "echo json_encode($array);" at the end.
Want to build a graph on D3.js, so I need to get that JSON from php.
How to make it with D3.request() or d3.json() or other? Please, help me with example.
(Here is guide, but I cant make it yet - https://github.com/d3/d3-request/blob/master/README.md#json)
AJAX request is simple:
$.ajax({
type: "GET",
url: "get_json.php",
data: "FirstName="+ name, //here is the parameter
success: function(data){
var data = $.parseJSON(data);
alert(data.Content);
}
});
In order for your javascript code to access the php script it will need to be run on a web server. A simple way to run a web server if you have PHP installed is:
Open command line
Navigate to the directory with this javascript file and your get_json.php file
Run the command php -S localhost:8080
Now your files are running on a local php web server which allows you to reach the file containing your AJAX request at: localhost:8080/[ajax js file]
var par = 23
d3.json("get_data2.php?par=" + par, function(error, json) {
if (error) return console.warn(error);
data = json;
});
solved.
Related
I am suposed to make report scheduling in Laravel. Let's say that I want to send a pdf file to a list of emails that I get from my database every morning. I have written a command that calls a function from a controller, and the function returns a view where I have written a script.
Rather than using snappy or any other php libraries, I would like to do the report in jspdf. In the script I would pass the generated data via ajax back to controller and save the file in storage.
<script type="text/javascript">
$(document).ready(function () {
var doc = new jsPDF();
doc.text('text', 80, 10);
var pdf = btoa(doc.output());
$.ajax({
method: "POST",
headers: {
'Access-Control-Allow-Headers' : '*',
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
},
url: url,
data: {data: pdf},
}).done(function(data){
console.log(data);
});
});
</script>
Then in the controller I would do this:
public function data(Request $request)
{
$data = base64_decode($request->data);
\Storage::put('app/public', $data);
}
Is there any way in which I could force my javascript to be loaded when my crontab is called?
I'm not a NodeJS expert but if you issue relies on a PDF, you certainly could do the same in a NodeJS script and then call this script from your cron
No, because your JavaScript is client sided, it cannot be called on the server. Not without making a Node.js script to generate your PDF.
Alternatively, you could look into some PHP PDF generation packages and then create an artisan command and setup a cron job and make your PDF be generated on the server side.
I have a variable within a laravel PHP page, held within Javascript. I currently print / output this as:
${data.id}
I now want to connect to PHPMyAdmin to check to see if the ID is within a table. However, I realise I can't use the ID number in PHP - as this is done on the server side and JS on the client side. A little stuck.
Unfortunately, I'm on a shared hosting package so node.js isn't a solution I can use.
Any help would be great! I think I can use AJAX, but unsure on how this would look.
here the AJAX sample.
$.ajax({
method: "POST",
url: "some.php",
data: { id: data.id}
})
.done(function( response) {
console.log(response);
//TODO:process your response here if receive response from php file.
});
I have this php file graph.php
$host = $_POST['hostname'];
echo $type=$_POST['type_char'];
include('rrdtools.inc.php');
include('graphs/'.$type.'.inc.php');
and I trying to send data to this file using this ajax code
var type_char='fortigate_cpu';//$('#graph').val();
var hostname='10.10.0.144';//$(this).attr('id');
//$('#device_host').val(id);
$.ajax({
type: 'POST',
url: 'SNMP/graph.php',
data: { hostname:hostname,type_char:type_char },
success: function(data) {
alert(data);
// show the response
$("#grph").attr("src", 'SNMP/graph.php');
console.log(data);
}
});
the result when I send data to that file is
fortigate_cpu as a value of type_char variable
when I opened error.log file in apache logs
I have this message
include(): Failed opening 'graphs/.inc.php' for inclusion (include_path='.:/usr/share/php')
as you see the value of fortigate not included in include function even if the char_type variable is send by ajax and printed in page
include file must be as this
include( 'graphs/fortigate_cpu.inc.php')
why type not included in the include session even if the variable is received from ajax
As was mentioned by other users in the comments, maybe your issue is that you are setting type to a different value after including rrdtools.inc.php .
Try randomizing ( changing the name), of the type variable:
$host = $_POST['hostname'];
echo $type123456=$_POST['type_char'];
include('rrdtools.inc.php');
include('graphs/'.$type123456.'.inc.php');
It's the only thing I can think of, since both I (and others) have tested your code.
(both front-end and back-end).
PS: Include using post param is a bad practice.
i'm writing a Firefox addon on sdk, i need to use in my main function an array. this array contains a list of data located in my BD. that’s why im using a php file to connect the BD and echo data in JSON format . then i call back the php file using request function and finally parse the JSON to an array. (my problem is how to request the php file and parse the json to array ) i tryed
var Request = require("sdk/request").Request;
var function = Request({
url: "file.php",
onComplete: function (response) {
var arrray = response.json;
}
});
but that didn't work :/
i tried a lot of thing too .
could someone help me with the right function to request the php file then parse the json to array
`
Recently i am learning json to create apps.I have a doubt in a Json , php based chat system .
In this , the code work fine for same origin policy.But for sending and receiving data from external url, it successfully sends data to external php.But not receiving any data from server.I search in internet to solve this problem , and found jsonp as alternative. I tried jsonp , but i m not sure if am correct because i am new to ajax itself.
Please don't mis understand my question.I want to load a index.html file from localhost , when i send request to external url (anysite.com/xx/ajax.php) .It process and returns the data back to index.html.But the problem is my data is sended finely and processed on the server but it doesn't return to remote file.But it works fine for same server.
$.tzPOST = function(action,data,callback)
{
$.post('http://anysite.com/xx/ajax.php?action='+action,data,callback,'json');
}
$.tzGET = function(action,data,callback){
$.get('http://anysite.com/xx/ajax.php?action='+action,data,callback,'json');
}
please help me with a code.
You cant receive JSON from external web by JavaScript, because of the policy.
But you can do AJAX request on your PHP file and there you can get the JSON by file_get_content http://cz2.php.net/file_get_contents function.
For using(working) with jsonp, u can take ready solution jquery-jsonp
from GitHub.
Example of using (by you question):
$.tzGET = function(action,data,callback){
var url = 'http://anysite.com/xx/ajax.php?action='+action;
$.jsonp({
type: 'GET',
url: url,
callbackParameter: callback,
dataType: 'jsonp',
data: data,
timeout: 10000,
success: function(json){
alert('success')
},
error: function(){
alert('error')
}
});