简体   繁体   English

如何获取引导模式的数据并发送 PHP

[英]How get data of for bootstrap modal and send PHP

I need to get the Modal form data via Ajax My code:我需要通过 Ajax 获取 Modal 表单数据我的代码:

    $(document).on('click', '#save-photo', function () {
    $.ajax({
        url: '/sistem/view/agend/photovisit.php',
        type: 'POST',
        data: $('#photo').serialize(),
        beforeSend: function () {
            $('#save-photo').val("Saving");
        },
        success: function (data) {
            $('#photo')[0].reset();
            $('#registerPhoto').modal('hide');
            //$('#call sucess message').html(data); 
        }
    });
});

code modal:代码模态:

    <div id="registerPhoto" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="registerPhotoLabel" aria-hidden="true" style="display: none;">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title text-xs-center" id="registerPhotoLabel">Register Photo Visit</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body"> <!-- FORM -->
                <form name="photo" id="photo" data-async data-target="#rating-modal" method="POST" action="/sistem/view/agend/photovisit.php" enctype="multipart/form-data">
                    <input type="hidden" name="_token" value="">
                    <div class="row justify-content-md-left">
                        <div class="col col-lg-3">
                            <label class="control-label" for="datavisit">Data Visit</label>
                        </div>
                        <div class="col col-lg-6">    
                            <input type="date" name="datavisit" class="form-control  form-control-sm form-group small" />
                        </div>
                    </div>

                    <div class="row justify-content-md-left">
                        <div class="col col-lg-3">
                            <label class="control-label" for="idvisit">Visit</label>
                        </div>
                        <div class='col col-md-6'>    
                            <select>
                                <option value="1">Arquitect</option>
                                <option value="2">Engineer</option>
                            </select>
                        </div>
                    </div>
                    <br />
                    <div class="row justify-content-md-left">
                        <div class="col col-lg-3">
                            <label class="control-label" for="percent">Percent Progress</label>
                        </div>
                        <div class="col col-md-6 slidecontainer">
                            <input type="range" name="percent" id="range_weight" min="1" max="100" value="1" class="form-control-range slider" oninput="range_weight_disp.value = range_weight.value">
                            <output  id="range_weight_disp"></output>
                        </div>
                    </div>
                    <div class="row justify-content-md-left">
                        <div class="col col-lg-3">
                            <label class="control-label" for="photofile">Photo</label>
                        </div>
                        <div>   
                            <input type="file" class="form-control-file form-control-sm form-group small" name='photofile' id="photofile" multiple="multiple" />
                            <span id="hidden_img_show"></span>
                        </div>
                    </div>
                    <div class="row justify-content-md-left">
                        <div class="col col-lg-3">
                            <label class="control-label" class="control-label" for="photoDescription">Description Photo</label>
                        </div>
                        <div class='col-md-auto'>                
                            <textarea name="photoDescription" cols="25" rows="3" class="form-control  form-control-sm form-group small" ></textarea>
                        </div>
                    </div> 

                    <div class="modal-footer">
                        <div>

                            <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                            <button type="button"  name="save-photo" id="save-photo" class="btn btn-primary">
                                <span class="fas fa-plus"></span>
                            Save</button> 
                        </div>
                    </div>

                </form>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<!-- fim modal foto -->
</div>
</div>

My goal is to get all fields typed and send to the PHP page editacli.php that already has the methods to receive the $ _POST [''];我的目标是输入所有字段并将其发送到已经具有接收 $_POST ['']; 方法的 PHP 页面 editacli.php; of each form element and do the save saving in the database and then I want to close the Modal and display a message on the Modal screen, "Data saved successfully".每个表单元素并在数据库中保存保存,然后我想关闭模态并在模态屏幕上显示一条消息,“数据保存成功”。

But this Ajax code is not working, when I click the submit button nothing happens.但是这个 Ajax 代码不起作用,当我点击提交按钮时没有任何反应。

NOTE: I have already tested my PHP insert page without the modal and it is saving normal, I just do not know how to solve with Modal.注意:我已经在没有模态的情况下测试了我的 PHP 插入页面并且它正在正常保存,我只是不知道如何使用模态解决。

1: Change your form submission method as following 1:更改您的表单提交方法如下
2: If your php file photovisit.php have linked from the main folder then use 2:如果您的 php 文件photovisit.php已从主文件夹链接,则使用
url: "../sistem/view/agend/photovisit.php", thervise url: "sistem/view/agend/photovisit.php", . url: "../sistem/view/agend/photovisit.php", thervise url: "sistem/view/agend/photovisit.php", .

$('#photo').submit(function(e){
    e.preventDefault();
var savePhoto = $(this).serialize();
$.ajax({
url: "../sistem/view/agend/photovisit.php",
type: "post",
data: savePhoto ,
dataType: 'JSON',
success: function(response) {
}

Check it and let me know if the problem still exists.检查它并让我知道问题是否仍然存在。

Try changing type: 'POST', to method: 'POST', and also add contentType: 'application/json尝试将type: 'POST',更改为method: 'POST',并添加contentType: 'application/json

' '

Please read my code it's far from yours but i think it will helps you.请阅读我的代码,它与您的代码相去甚远,但我认为它会对您有所帮助。

<script>
    $(document).ready(function(){

         $('#insert_open').on('submit',function(e)
{
    e.preventDefault();
    processing_show();  
        var form_data = new FormData(this);                  
        $.ajax
        ({
            url: "./scripts.php?action=insert_open",
            type: "POST",             
            data: form_data,
            contentType: false,       
            cache: false,             
            processData:false,
            success: function(data) {
                processing_hide();
                $("#message-success").slideDown("slow");
                setTimeout(function(){close_message_success();},3000);

            },
            error: function(data)
            {

                $("#message-warning").slideDown("slow");
                setTimeout(function(){close_message_success();},3000);
            }
        });
    });
});
</script>

And for form this is my html对于表单,这是我的 html

<form class="form-horizontal row-fluid" id="insert_open" name="insert_open" method="post" enctype="multipart/form-data" >

                                <div class="form-horizontal row-fluid">
                                    <div class="control-group">
                                        <label class="control-label" for="basicinput"></label>
                                        <div class="controls">
                                            <button class="btn btn-primary" type="submit">Insert all Open has the same id_list</button>
                                        </div>
                                    </div>

                                </div>
                                </form>

You have several typos in your code.您的代码中有几个拼写错误。 This line, for example:这一行,例如:

data: $('#photo').serialize(),

refers to this (and only this) element from your form:从您的表单中引用此(且仅此)元素:

<input 
    type="file" 
    class="form-control-file form-control-sm form-group small" 
    name='photo' 
    id="photo" 
    multiple="multiple" />

since that's the element with the ID photo .因为那是带有 ID photo的元素。 So, your AJAX call is serializing the info of that field only.因此,您的 AJAX 调用仅序列化该字段的信息。 The ID of the form is foto , so if you want to send the whole form, you have to change that line of your code to:表单的 ID 是foto ,所以如果你想发送整个表单,你必须将代码的那行更改为:

data: $('#foto').serialize(),

Also, this line:此外,这一行:

$('#savephoto').modal('hide');

does nothing, since there's no element with savephoto ID anywhere in your form.什么都不做,因为表单中的任何地方都没有带有savephoto ID 的元素。 The ID of your modal is actually registerphoto , and that's what you need to use in order for the modal to close:您的模态 ID 实际上是registerphoto ,这就是您需要使用的 ID 才能关闭模态:

$('#registerphoto').modal('hide');

Make those changes and try again.进行这些更改并重试。

You need to put id attributes to elements that you want to be POSTed to your PHP file, and get its value.您需要将 id 属性放在要发布到 PHP 文件的元素上,并获取其值。 Say, you need to get the value of photoDescription textarea.比如说,您需要获取 photoDescription textarea 的值。 You will first give an id attribute instead of name attribute.您将首先提供一个 id 属性而不是 name 属性。 So it will be like this:所以它会是这样的:

<textarea id="photoDescription"></textarea> 

And then you'll store the value of textarea to a JS variable.然后你将 textarea 的值存储到一个 JS 变量中。

var photoDescription = $('#photoDescription').val();

After that put all of your values that you need in your photovisit.php之后,将您需要的所有值放入 photovisit.php

data: photoDescription,  

Addition, inside your photovisit.php that will receive the values from your ajax, you will need to call it with the $_POST method.此外,在从 ajax 接收值的 photovisit.php 中,您需要使用 $_POST 方法调用它。

$photoDescription = $_POST['photoDescription']; //

The index of the POST variable should be the same from your AJAX call "data:". POST 变量的索引应该与您的 AJAX 调用“data:”相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM