简体   繁体   中英

can not Send data to php file using ajax

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.

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