因为我是ajax的新手并且对此不太了解...我创建了2个php文件,其中一个是 six.php ,另一个是 six-cuf.php ... 根据six.php中的用户输入和点击提交,这将调用六个cuf。 php使用ajax并进行计算。
但是,当我通过chrome中的网络选项卡执行并监视代码时,我知道这些值被传递给了6-cuf.php,但是它没有进入页面内部的if条件它没有得到任何回应。
它没有进入if()在上面的代码中开始的六个cuf.php是什么错误
当我使用json_decode时它返回null所以使用json_last_erro并返回以下错误消息....语法错误,格式错误的JSON
Six.php
<body>
<div class="six">
<form onsubmit="callphp()">
<label>CUF PERFORMANCE</label>
<select id="select" name="options">
<option>Select The Value</option>
<option id="hourly" value="a">Hourly</option>
<option id="daily" value="b">Daily</option>
<option id="monthly" value="c">Monthly</option>
<option id="yearly" value="d">Yearly</option>
</select><br/><br/>
<label id="from" style="display:none;">Enter starting Date and Time</label>
<label id="start" style="display:none;">Enter Starting Date</label>
<input id="dateInputone" name="dateipone" type="datetime-local" step="600" onblur="validate_time(this.value)" style="display:none;" >
<input id="dateIpone" name="dateone" type="date" style="display:none;">
<label id="to" style="display:none;">Enter Ending Date and Time</label>
<label id="end" style="display:none;">Enter Ending Date</label>
<input id="dateInputtwo" name="dateiptwo" type="datetime-local" step="600" style="display:none;">
<input id="dateIptwo" name="datetwo" type="date" style="display:none;" >
<br/><br/>
<label for="Inverter">Inverter</label>
<select name="ino" id="ino">
<option>Select The Value</option>
<option value="1">Inverter 1</option>
<option value="2">Inverter 2 </option>
<option value="3">Inverter 3</option>
<option value="4">Inverter 4</option>
<option value="5">Inverter 5</option>
<option value="6">Inverter 6</option>
</select><br/>
<input type="submit" value="submit" id="submit" name="submit" onclick="myFunction()">
<br><br>
</div>
<script>
function callphp(){
var dataa = {};
dataa.dateipone = jQuery("#dateInputone").val();
dataa.dateiptwo = jQuery("#dateInputtwo").val();
dataa.ino = jQuery("#ino").val();
dataa.options = jQuery("#select").val();
dataa.dateone = jQuery("#dateIpone").val();
dataa.datetwo = jQuery("#dateIptwo").val();
dataa.submit = "submit";
$.ajax({
url : "six-cuf.php",
type: 'POST',
data : JSON.stringify(dataa),
contentType : "application/json",
success:function(data)
{
if(data){
alert(data);
//console.log("Data from Server"+JSON.stringify(data));
}
else{
console.log("Data is empty");
}
},
error: function(xhr) {
alert('Error! Status = ' + xhr.status + " Message = " + xhr.statusText);
//console.log('Error! Status = ' + xhr.status + " Message = " + xhr.statusText);
}
});
}
</script>
</body>
</html>
下面的是6-cuf.php
include 'db.php';
if(isset($_POST["dateipone"],$_POST["dateiptwo"],$_POST["ino"],
$_POST["options"],$_POST["dateone"],$_POST["datetwo"],$_POST["submit"]))
{
$format = $_POST["options"];
if($format == 'a')
{
date_default_timezone_set('GMT');
$fromdate = $_POST["dateipone"];
$start =new Datetime('@'.strtotime( $fromdate));
$todate = $_POST["dateiptwo"];
$end = new Datetime('@'.strtotime( $todate));
$inverter = $_POST["ino"];
$count = 1;
$query = "SELECT timestamp, gridpowertotal FROM inverterlog where inverter = '" . $inverter . "' AND timestamp BETWEEN '" . $fromdate . "' AND '" . $todate . "'";
if ($result = mysqli_prepare($con, $query))
{
mysqli_stmt_execute($result);
mysqli_stmt_bind_result($result, $timestamp, $gridpowertotal);
$arr1= array();
$arr2=array();
$array_associate = array();
$hourArray = array();
while(mysqli_stmt_fetch($result))
{
$obj = new stdClass();
$obj->timestamp = $timestamp;
$obj->gridpowertotal = $gridpowertotal;
$array_associate[$timestamp]=$obj;
}
mysqli_stmt_close($result);
mysqli_close($con);
$interval = new DateInterval('PT10M');
$daterange = new DatePeriod($start, $interval ,$end);
$sum = 0;
foreach($daterange as $date){
$index = $date->format("Y-m-d H:i:s");
if($count%6 == 1) {
$backupIndex = $index;
}
if($count%6==0) {
$count++;
if(array_key_exists($index,$array_associate) && $array_associate[$index]!=null){
$sum = $sum+$array_associate[$index]->gridpowertotal;
}
else {
$sum = $sum+0;
$obj2 = new stdClass();
$obj2->timestamp = $index;
$obj2->gridpowertotal = 0;
$array_associate[$index] = $obj2;
}
$obj1 = new stdClass();
$obj1->timestamp = $backupIndex;
$obj1->sum = $sum;
array_push($hourArray,$obj1);
$sum = 0;
} else {
if(array_key_exists($index,$array_associate) && $array_associate[$index]!=null) {
$sum = $sum+$array_associate[$index]->gridpowertotal;
} else {
$sum = $sum+0;
$obj2 = new stdClass();
$obj2->timestamp = $index;
$obj2->gridpowertotal = 0;
$array_associate[$index] = $obj2;
}
$count++;
}
}
var_dump($hourArray);
$narray = array_values($hourArray);
foreach($narray as $object){
array_push($arr1,$object->timestamp);
array_push($arr2,$object->sum);
}
}
header('Content-type: application/json');
echo json_encode(array('format'=>"Helllo"));
}
else
if($format == 'b')
{
$fromdate = $_POST['dateone'];
$todate = $_POST['datetwo'];
$inverter = $_POST['ino'];
$dayArray = array();
$arr1 = array();
$arr2 = array();
$query = "SELECT DATE(`timestamp`) as timestamp, SUM(`gridpowertotal`) as gridpowertotal FROM inverterlog where inverter = '" . $inverter . "' AND DATE(`timestamp`) BETWEEN '" . $fromdate . "' AND '" . $todate . "' GROUP BY DATE(`timestamp`)";
if ($result = mysqli_prepare($con, $query))
{
mysqli_stmt_execute($result);
mysqli_stmt_bind_result($result, $timestamp, $gridpowertotal);
while(mysqli_stmt_fetch($result))
{
array_push($dayArray,$timestamp,$gridpowertotal);
array_push($arr1,$timestamp);
array_push($arr2,$gridpowertotal);
}
var_dump($nArray)."<br/>";
var_dump($arr1)."<br/>";
var_dump($arr2)."<br/>";
}
mysqli_stmt_close($result);
mysqli_close($con);
}
else
if($format == 'c')
{
}
}
?>
答案 0 :(得分:0)
由于您使用过json,因此需要使用 six-cuf.php 上的json_decode
解码参数。这里看一下php's tutorial for json_decode
答案 1 :(得分:0)
你说你的第一个陈述有问题 - 你是对的。
if(isset($_POST["dateipone"],$_POST["dateiptwo"],$_POST["ino"], $_POST["options"],$_POST["dateone"],$_POST["datetwo"],$_POST["submit"]))
您需要检查是否为每个变量设置了变量。
将其更改为:
if(isset($_POST["dateipone"]) && isset($_POST["dateiptwo"]) && isset($_POST["ino"]) && isset($_POST["options"]) && isset($_POST["dateone"]) && isset($_POST["datetwo"]) && isset($_POST["submit"]))