无法使用formData插入/编辑数据

时间:2019-07-23 07:36:58

标签: javascript php ajax form-data

我有一个包含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.'" />

当我单击提交时,我可以看到通过formData解析的数据,但是我不知道该如何将其插入数据库... formData

0 个答案:

没有答案