Friday 15 August 2014

php - Trouble adding data to database using AJAX -



php - Trouble adding data to database using AJAX -

i'm trying add together info table in database using ajax , i'm having trouble. script works now, there's 1 field beingness added, others not beingness added , i'm not sure why. know i'm posting has sql injection vulnerabilities, i'm trying create functionality work demo project.

html

<div id="form_emergencies" class="form_view"> <div class="half" id="systems"></div> <div class="half" id="malfunctions"></div> </div>

jquery

var alerts = { apu: [["apu powerfulness fail", "apu[1]"], ["apu powerfulness 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 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 comparing monitor", "avionics[12]"], ["heading , roll comparing 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 scheme = 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), scheme 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 `$malfunctions`(`scenario_id`, `system`, `description`, `breaker`) values('" .$id ."', '" .$system ."', '" .$description ."', '" .$breaker ."')"; mysqli_query($connect, $insert_malfunctions); echo json_encode(array("success" => 1)); }

i have 2 additional queries in processing script adds info other tables, don't think creating issue. also, having php check submit button's post value creating issue? works now, field that's eing added database scenario_id field.

here's network response i'm getting.

put header @ end after insert because header redirecting page before insert , hence losing post data.

header("location: ../pages/instructor.php");

also alter this;

$insert_malfunctions = "insert " . $malfunctions . "( scenario_id, system, description, breaker) values('" .$id ."', '" .$system ."', '" .$description ."', '" .$breaker ."')";

php jquery ajax

No comments:

Post a Comment