我有一个包含2个按钮的页面:一个用于编辑,一个用于添加。当我单击添加时,我具有ID -1,当我单击编辑时,我从数据库中获得了ID。问题是,当我单击“提交”按钮时,我得到一个空白页。这是我的AJAX调用:
$(document).on("click", ".concurs-addedit", function (e) {
e.preventDefault();
var form = document.getElementById('concurs-form');
var formData = new FormData(form);
console.log(formData);
$.ajax({
type: "POST",
url: "ajax/concurs-addedit.php",
data: formData,
processData: false,
contentType: false,
cache : false,
dataType: "json"
}).done(function(msg) {
swal({
title: '<?php echo $txt_succes[$lang];?>',
text: '<?php echo $txt_inregistrare_salvata[$lang];?>',
type: 'success'}
,function () {
location.reload()
}
);
});
});
这是我的addit.php页面:
if (intval($_POST["id_concurs"])==0){
//insert
$concurs = new tableConcurs($db);
$idconcurs_inserat = $concurs->Insert(isset($_POST['denumire']),isset($_POST['locatia']), isset($_POST['dela']), isset($_POST['la']));
$json['idconcurs'] = $idconcurs_inserat;
$json['numeconcurs'] = getNumeConcurs($db, $idconcurs_inserat);
}
else{
//update
$concurs = new tableConcurs($db, $_POST["id_concurs"]);
$concurs->Update(isset($_POST['denumire']),isset($_POST['locatia']), isset($_POST['dela']), isset($_POST['la']));
$json['idconcurs'] = $_POST["id_concurs"];
$json['numeconcurs'] = getNumeConcurs($db, $_POST["id_concurs"]);
}
我从输入中获得id_concurs,添加时为0:
<input type="hidden" name="id_concurs" style="padding:0px;" value="0" />
从数据库中,我使用以下方法获得id的值:
$concurs = new tableConcurs($db,$_POST["id"]);
$concurs = $db->DbGetRow("SELECT * FROM tconcurs WHERE id = ".$_POST["id"]);
<input type="hidden" name="id_concurs" style="padding:0px;" value="'.$concurs->id.'" />