猿问

使用 Php ajax 记录而不是添加到数据库中

我正在使用 php ajax 创建简单的 crud 系统。在 crud 系统中,我还添加了图像。但是当我填写数据并浏览图像并单击添加按钮时,记录未添加到数据库中。到目前为止我尝试了什么我附加了下面。控制台上没有显示错误。我认为可能是 jquery 发送表单值和图像数据的问题:{form_data: form_data,data: data},


表格设计


 <form role="form" id="frmcompany" class="card" enctype="multipart/form-data">

        <div align="left">

            <h3>Company</h3>

        </div>


        <div align="left">

            <label class="form-label">Company name</label>

            <input type="text" class="form-control" placeholder="Patient No" id="cno" name="cno" size="30px" required>

        </div>


        <div align="left">

            <label class="form-label">Country</label>

            <input type="text" class="form-control" placeholder="Patient Name" id="coutry" name="coutry" size="30px" required>

        </div>


        <div align="left">

            <label class="form-label">Currency</label>

            <input type="text" class="form-control" placeholder="Phone" id="currency" name="currency" size="30px" required>

        </div>


        <div align="left">

            <label class="form-label">Address</label>

            <input type="text" class="form-control" placeholder="Address" id="address" name="address" size="30px" required>

        </div>

            <div align="left">

                <div class="fileuploader fileuploader-theme-default">

                    <input type="hidden" name="fileuploader-list-files_" value="[]">

                    <input type="file" id="file" name="file" >

                    <div class="fileuploader-items">

                        <ul class="fileuploader-items-list"></ul>

                    </div>


                </div>

            </div>


        </br>


        <div align="right">

            <button type="button" id="save" class="btn btn-info" onclick="addPatient()">Add</button>

            <button type="button" id="clear" class="btn btn-warning" onclick="reset()">Reset</button>


        </div>



    </form>


慕桂英3389331
浏览 129回答 2
2回答

慕哥6287543

你不能FormData在另一个对象中发送一个对象。所有 POST 参数都必须在form_data.function addPatient() {&nbsp; if ($("#frmcompany").valid()) {&nbsp; &nbsp; var url = '';&nbsp; &nbsp; var data = '';&nbsp; &nbsp; var method = '';&nbsp; &nbsp; var form_data = new FormData(document.getElementById("frmcompany"));&nbsp; &nbsp; var upload_date = $('#file').prop('files')[0];&nbsp; &nbsp; form_data.append('file', upload_date);&nbsp; &nbsp; if (isNew == true) {&nbsp; &nbsp; &nbsp; url = 'php/add_patient.php';&nbsp; &nbsp; &nbsp; data = $('#frmcompany').serialize();&nbsp; &nbsp; &nbsp; method = 'POST';&nbsp; &nbsp; }&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; type: method,&nbsp; &nbsp; &nbsp; url: url,&nbsp; &nbsp; &nbsp; dataType: 'JSON',&nbsp; &nbsp; &nbsp; cache: false,&nbsp; &nbsp; &nbsp; contentType: false,&nbsp; &nbsp; &nbsp; processData: false,&nbsp; &nbsp; &nbsp; data: form_data,&nbsp; &nbsp; &nbsp; success: function(data) {&nbsp; &nbsp; &nbsp; &nbsp; if (isNew == true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("Company Addedd");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; }}

凤凰求蛊

&nbsp;function addPatient(){&nbsp; &nbsp; var upload_date = $('#file').prop('files')[0];&nbsp; &nbsp; var form_data = new FormData();&nbsp; &nbsp; form_data.append('file', upload_date);&nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: 'php/add_patient.php',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cache: false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentType: false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; processData: false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: form_data,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error: function(error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(error);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });}
随时随地看视频慕课网APP
我要回答