为什么我的AJAX脚本不能传递变量到PHP传递到我的数据库? [英] Why would my AJAX script not pass variables to PHP to pass into my database?

查看:97
本文介绍了为什么我的AJAX脚本不能传递变量到PHP传递到我的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AJAX脚本,我需要可变数据传递到数据库中,但是,该脚本不这样做。我已经通知这两个变量实际上并没有被通过网络传递的PARAMS选项卡的控制台窗口。谁能告诉我什么,我做错了什么?

  VAR警报= {
    APU:[[APU电源故障,APU [1]]
          [APU电源故障,APU [2]]
          [APU发电机故障,APU [3]]
          [APU高油温度,APU [4]]
          [APU热启动,APU [5]]
          [APU损失超速保护,APU [6]],
          [APU入门订婚,APU [7]]
          [的APU火,的APU [8]],
          [APU失败咬检查,APU [9]]
          [APU门无法打开,APU [10]]
          [APU没有火焰,APU [11]]
          [左火瓶放电,APU [12]]],

    航空电子设备:
          [ADS 1失败,航空电子[1]]
          [ADS 2失败,航空电子[2]]
          [ADS 3失败,航空电子[3]]
          [美联社1失败,航空电子[4]]
          [美联社2失败,航空电子[5]]
          [自动驾驶仪故障,航空电子设备[6]],
          [Baroset 1失败,航空电子[7]]
          [Baroset 2失败,航空电子[8]]
          [Baroset 3失败,航空电子[9]]
          [CCD 1失败,航空电子[10]]
          [CCD 2失败,航空电子[11]]
          [标题比较箴言报,航空电子[12]]
          [标题和残疾人比较箴言报,航空电子[13]]
          [显示控制器1失败,航空电子[14]]
          [显示控制器2失败,航空电子[15]]
          [IRS 1失败,航空电子[16]]
          [IRS 2失败,航空电子[17]]
          [国税局3失败,航空电子[18]],
          [下滑道天线故障,航空电子[19]]
          [MAU 1A失败,航空电子[20]]
          [MAU 1B失败,航空电子[21]]
          [MAU 2A失败,航空电子[22]]
          [MAU 2B失败,航空电子[23]]
          [MAU 3A失败,航空电子[24]]
          [MAU 3B失败,航空电子[25]]
          [MRC 1失败,航空电子[26]]
          [MRC 2失败,航空电子[27]]
          [GPS的降级,航空电子[28]],
          [GPS#1失败,航空电子[28]]
          [GPS#2失败,航空电子[30]]
          [显示单位1失败,航空电子[31]]
          [显示单元2失败,航空电子[32]]
          [显示单位3失败,航空电子[33]]
          [显示单位4失败,航空电子[34]]
          [全球定位系统 - 无法RNP,航空电子[35]]]
}
Var描述,断路器;
为(在警报VAR键){
    VAR系统= key.toUpperCase();
    $(#系统)追加(< D​​IV CLASS ='系统'ID ='+键+'>< D​​IV CLASS ='select_box'>< / DIV>< H2>中+系统+< / H>< / DIV>中);
}
$(#系统)。在(点击,.systems,函数(){
    。$(#失灵)儿童()删除();
    $(本).find(select_box)。每个(函数(){
        $(选择)。removeClass移除(选择)。
        $(本).addClass(选择);
    })
    $每个(警报[this.id],功能(IND,项目){
        说明=项目[0];
        断路器=项[1];
        $(#失灵)追加(< D​​IV CLASS ='系统'数据键='+键+数据断路器='+断路器+数据-ID ='+说明书+ '>< D​​IV CLASS ='select_box'>< / DIV>< P>中+说明书+< / P>< / DIV>中);
    })
})
$(#故障)。在(点击,.systems,函数(){
    VAR键= $(本)的.d​​ata(钥匙);
    VAR断路器= $(本)的.d​​ata(断路器);
    VAR ID = $(本)的.d​​ata(ID);
    $(本).find(select_box)。每个(函数(){
        如果($(本).hasClass(选择)){
            $(本).removeClass(选择);
        }
        其他{
            $(本).addClass(选择);
            $。员额(../ PHP / processing.php,{系统:重点介绍:说明,断路器:断路器}功能(响应){
                如果(response.success ==1){
                }
            })
            执行console.log(键);
            的console.log(ID);
            执行console.log(断路器);
        }
    })
})
 

PHP头

//在头文件

  $ create_table_malfunctions =CREATE TABLE IF NOT EXISTS $故障(
          scenario_id VARCHAR(100),
          系统VARCHAR(10),
          描述VARCHAR(50),
          断路器VARCHAR(10));
 

//在处理文件

  ob_start()函数;
    要求(../包括/ header.php文件);
    如果($ _ POST [保存]){
        标题(位置:../pages/instructor.php);
        $指导员= $ _ POST [名称];
        $缩写= $ _ POST [缩写];
        $日期= $ _ POST [日];
        $中心= $ _ POST [center_menu];
        $ pair1 = $ _ POST [pair1];
        $ pair2 = $ _ POST [pair2];
        $场景= $ pair1 $ pair2。
        的$ id = $方案.substr(strtoupper($中心,0,4))$日期$缩写。;
        $系统= $ _REQUEST [系统];
        $描述= $ _REQUEST [说明]。
        $断路器= $ _REQUEST [断路器];
        ob_clean();

        $ insert_malfunctions =INSERT INTO`$ malfunctions`
                   (`scenario_id`,`system`,`description`,`breaker`)
                   VALUES
                   ('。$ id。''。$系统。,
                    '。$的描述,,$断路器。');

        mysqli_query($连接,$ insert_malfunctions);
        回声json_en code(阵列(成功=> 1));
    }
 

解决方案

您已经在PHP脚本检查$ _ POST ['保存']的条件,但我没有看到你从你的Javascript通过传递这个帖子变量$。员额,因此大多数PHP的code永远不会触发。

这可能会得到你的PHP脚本工作:

$后(../ PHP / processing.php,{保存:真正的,系统:重点介绍:说明,断路器:断路器}功能(响应){.. }

不过,你有一个头()重定向可能会产生意外的结果。 302黄色箭头(我指的是你的其他重复的线程)表示,最初的请求processing.php被重定向到instructor.php因为你有标题(位置:../pages/instructor.php);在那边。这可能是因为此重定向导致(而不是POST)一个额外的GET请求,让你马上从原来的请求失去了$ _ POST变量。

另一件事我注意到的是你的PHP脚本利用许多$ _ POST变量是不被从Javascript,例如通过$ _ POST [pair1]将是空的,因为你只有通过系统,说明,并从Javascript断路器的变量。然后,使用PHP脚本$ _REQUEST拉在发布变量如$ _REQUEST [系统当​​只需$ _ POST [系统]将做工精细和更加一致。

您有一些不必要/ cluttery code。我建议削下来得到的code通过AJAX POST运行的最小量,然后扔在后来的变量的其余部分。

I have an AJAX script that I need to pass variable data into a database, however, the script is not doing this. I've notice on the console window in the params tab that the variables aren't actually being passed over the network. Can anyone show me what I'm doing wrong?

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 header

//In header file

 $create_table_malfunctions = "CREATE TABLE IF NOT EXISTS $malfunctions(
          scenario_id VARCHAR(100), 
          system VARCHAR(10), 
          description VARCHAR(50), 
          breaker VARCHAR(10))";

//In processing file

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));
    }

解决方案

You have a condition in the PHP script that checks for $_POST['save'], but I do not see you passing this post variable from your Javascript via $.post, therefore the majority of your PHP code never fires.

This might get your PHP script working:

$.post("../php/processing.php", {save: true, system: key, description: description, breaker: breaker}, function(response){ ... }

However, you have a header() redirect that might produce undesired results. The 302 yellow arrow (I'm referring to your other duplicate thread) indicates that the initial request to processing.php is being redirected to instructor.php because you have header("Location: ../pages/instructor.php"); in there. It could be that this redirect is causing an additional GET request (instead of POST) so you immediately lose the $_POST variables from your original request.

Another thing I noticed is your PHP script making use of many $_POST variables that are not being passed from Javascript e.g. $_POST["pair1"] is going to be empty because you are only passing "system", "description" and "breaker" variables from Javascript. You then use $_REQUEST in the PHP script to pull in posted variables e.g. $_REQUEST["system"] when simply $_POST["system"] would work fine and be more consistent.

You have some unnecessary/cluttery code. I would suggest paring it down to get a minimum amount of code functioning via AJAX POST, then throw in the rest of the variables later.

这篇关于为什么我的AJAX脚本不能传递变量到PHP传递到我的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆