I wanted to ask a question because i have been going mad why my code does not work.. I mean the post request is sent by ajax to the backend file where the data should be checked and then sent to the database.. I checked if i have an error in the mysql database but the stored procedure works just fine. Also checked if the post request is sent to the backend which it is (proof showed in the pictures..). And i cant find the mistake i am making.. Why the sent json variables are not written into the new generated php variables with $_POST? Also tried $_GET and still does not work.. Here is the code of the php file.. Thanks
<?php
function isPOSTBack(){
return ($_SERVER['REQUEST_METHOD'] == 'POST');
}
if(!isPOSTBack()){
header("location: /security/assetThreatScenarios1.php");
exit;
}
include("configfile.php");
$tabela; $id; $par1; $par2;
if(isset($_POST["tabela"] ) ) {
if (empty($_POST["tabela"]) || strlen($_POST["tabela"]) <2){
echo "{\"error\":true, \"errorText\":\"Table is not set\"}";
exit;
}
$tabela = $_POST["tabela"];
}
if(isset($_POST['id'] ) ) {
if (empty($_POST['id'])) {
echo "{\"error\":true, \"errorText\":\"id is not set\"}";
exit;
}
$id = $_POST['id'];
}
if(isset($_POST['par1'] ) ) {
if (empty($_POST['par1']) || strlen($_POST['par1']) < 2 ) {
echo "{\"error\":true, \"errorText\":\"id is not set\"}";
exit;
}
$par1 = $_POST['par1'];
}
if(isset($_POST['par2'] ) ) {
if (empty($_POST['par2']) || strlen($_POST['par2']) < 2 ) {
//$par2 = NULL;
echo "{\"error\":true, \"errorText\":\"id is not set\"}";
exit;
}
$par2 = $_POST['par2'];
}
else{
$par2 = "";
}
$sql = "CALL updateATSTables('$tabela', '$id', '$par1', '$par2')";
if (mysqli_query($conn, $sql)){
echo "{\"error\":false, \"errorText\":\"The assessment was updated successfully!\",\"affectedRow\":".$rowId."}";
}
else{
echo "{\"error\":true, \"errorText\":\"An error occured, please contact the security/safety section!\"".$tabela."}";
exit;
}
?>
The var_dump response
array(1) { ["{"tabela":"assetCategories","id":"1","par1":"Physical1"}"]=> string(0) "" }.. and with a variable i get NULL
The ajax request
function saveRowWithAjax(inputs){
var jsonstring = "{";
jsonstring+="\"tabela\":"+"\""+tabela+"\""+","+"\"id\":"+"\""+id+"\""+",";
inputs.forEach(function(el, i, arr){
jsonstring+="\""+el.name+"\""+":"+"\""+el.value+"\"";
if(arr.length-1!=i){
jsonstring+=",";
}
});
jsonstring+="}";
$.ajax({
url:"backend/ATSSaveEdit.php",
method:"POST",
data:jsonstring,
beforeSend:function(){
},
success:function(data){
var res = $.parseJSON(data);
if(res.error){
alert(res.errorText);
}
else {
input.innerHTML=res.htmltext;
addeventlisteners();
}
},
error:function(data){
},
always: function(data){
}
}); return false;
}