我有一个AJAX脚本,我需要将可变数据传递到数据库,但是,脚本没有这样做。我在params选项卡的控制台窗口注意到变量实际上并未通过网络传递。谁能告诉我我做错了什么?
var Alerts = {
apu: [["APU Power Fail", "APU[1]"],
["APU Power Fault", "APU[2]"],
["APU Generator Fail", "APU[3]"],
["APU High Oil Temperature", "APU[4]"],
["APU Hot Start", "APU[5]"],
["APU Loss Overspeed Protection", "APU[6]"],
["APU Starter Engaged", "APU[7]"],
["APU Fire", "APU[8]"],
["APU Fails Bite Check", "APU[9]"],
["APU Door Fails to Open", "APU[10]"],
["APU No Flame", "APU[11]"],
["Left Fire Bottle Discharge", "APU[12]"]],
avionics:
[["ADS 1 Fail", "AVIONICS[1]"],
["ADS 2 Fail", "AVIONICS[2]"],
["ADS 3 Fail", "AVIONICS[3]"],
["AP 1 Fail", "AVIONICS[4]"],
["AP 2 Fail", "AVIONICS[5]"],
["Autopilots Fail", "AVIONICS[6]"],
["Baroset 1 Fail", "AVIONICS[7]"],
["Baroset 2 Fail", "AVIONICS[8]"],
["Baroset 3 Fail", "AVIONICS[9]"],
["CCD 1 Fail", "AVIONICS[10]"],
["CCD 2 Fail", "AVIONICS[11]"],
["Heading Comparison Monitor", "AVIONICS[12]"],
["Heading and Roll Comparison Monitor", "AVIONICS[13]"],
["Display Controller 1 Fail", "AVIONICS[14]"],
["Display Controller 2 Fail", "AVIONICS[15]"],
["IRS 1 Fail", "AVIONICS[16]"],
["IRS 2 Fail", "AVIONICS[17]"],
["IRS 3 Fail", "AVIONICS[18]"],
["Glideslope Antenna Fail", "AVIONICS[19]"],
["MAU 1A Fail", "AVIONICS[20]"],
["MAU 1B Fail", "AVIONICS[21]"],
["MAU 2A Fail", "AVIONICS[22]"],
["MAU 2B Fail", "AVIONICS[23]"],
["MAU 3A Fail", "AVIONICS[24]"],
["MAU 3B Fail", "AVIONICS[25]"],
["MRC 1 Fail", "AVIONICS[26]"],
["MRC 2 Fail", "AVIONICS[27]"],
["GPS Degrade", "AVIONICS[28]"],
["GPS #1 Fail", "AVIONICS[28]"],
["GPS #2 Fail", "AVIONICS[30]"],
["Display Unit 1 Fail", "AVIONICS[31]"],
["Display Unit 2 Fail", "AVIONICS[32]"],
["Display Unit 3 Fail", "AVIONICS[33]"],
["Display Unit 4 Fail", "AVIONICS[34]"],
["GPS - Unable RNP", "AVIONICS[35]"]]
}
var description, breaker;
for(var key in Alerts){
var system = key.toUpperCase();
$("#systems").append("<div class='systems' id='" +key +"'><div class='select_box'></div><h2>" +system +"</h2></div>");
}
$("#systems").on("click", ".systems", function(){
$("#malfunctions").children().remove();
$(this).find(".select_box").each(function(){
$(".selected").removeClass("selected");
$(this).addClass("selected");
})
$.each(Alerts[this.id], function(ind,item){
description = item[0];
breaker = item[1];
$("#malfunctions").append("<div class='systems' data-key='" +key +"' data-breaker='" +breaker +"' data-id='" +description +"'><div class='select_box'></div><p>" +description +"</p></div>");
})
})
$("#malfunctions").on("click", ".systems", function(){
var key = $(this).data("key");
var breaker = $(this).data("breaker");
var id = $(this).data("id");
$(this).find(".select_box").each(function(){
if($(this).hasClass("selected")){
$(this).removeClass("selected");
}
else{
$(this).addClass("selected");
$.post("../php/processing.php", {system: key, description: description, breaker: breaker}, function(response){
if(response.success == "1"){
}
})
console.log(key);
console.log(id);
console.log(breaker);
}
})
})
PHP标题
//在头文件
中 $create_table_malfunctions = "CREATE TABLE IF NOT EXISTS $malfunctions(
scenario_id VARCHAR(100),
system VARCHAR(10),
description VARCHAR(50),
breaker VARCHAR(10))";
//处理文件
ob_start();
require("../includes/header.php");
if($_POST["save"]){
header("Location: ../pages/instructor.php");
$instructor = $_POST["name"];
$initials = $_POST["initials"];
$date = $_POST["date"];
$center = $_POST["center_menu"];
$pair1 = $_POST["pair1"];
$pair2 = $_POST["pair2"];
$scenario = $pair1 .$pair2;
$id = $scenario .substr(strtoupper($center, 0,4)) .$date .$initials;
$system = $_REQUEST["system"];
$description = $_REQUEST["description"];
$breaker = $_REQUEST["breaker"];
ob_clean();
$insert_malfunctions = "INSERT INTO `$malfunctions`
(`scenario_id`, `system`, `description`, `breaker`)
VALUES
('" .$id ."', '" .$system ."',
'" .$description ."', '" .$breaker ."')";
mysqli_query($connect, $insert_malfunctions);
echo json_encode(array("success" => 1));
}
答案 0 :(得分:0)
PHP脚本中有一个检查$ _POST ['save']的条件,但我没有看到你通过$ .post从你的Javascript传递这个post变量,因此你的大部分PHP代码都不会触发。
这可能会使您的PHP脚本正常工作:
$ .post(“../ php / processing.php”,{ save:true ,system:key,description:description,breaker:breaker},function(response){.. 。}
但是,您有一个header()重定向,可能会产生不需要的结果。 302黄色箭头(我指的是你的另一个重复的线程)表示对processing.php的初始请求被重定向到instructor.php因为你有标题(“Location:../ pages / templates.php”);在那里。可能是这个重定向导致了额外的GET请求(而不是POST),因此您会立即丢失原始请求中的$ _POST变量。
我注意到的另一件事是你的PHP脚本使用了许多没有从Javascript传递的$ _POST变量,例如$ _POST [“pair1”]将为空,因为您只是从Javascript传递“system”,“description”和“breaker”变量。然后在PHP脚本中使用$ _REQUEST来提取已发布的变量,例如$ _REQUEST [“system”]只需$ _POST [“system”]就可以正常工作并且更加一致。
你有一些不必要/杂乱的代码。我建议将其减少以通过AJAX POST获得最少量的代码,然后再输入其余的变量。