Home > Net >  how i detect submit btn name using ajax php
how i detect submit btn name using ajax php

Time:10-15

I need help! i need to know how i can detect button name for upload.php file so i can put if(isset($_post['aa'))

    <script type="text/javascript">
        $(document).ready(function (e) {
            $("#uploadForm").on('submit',(function(e) {
                e.preventDefault();
                var form = $(this);
                var url = form.attr('action');
                $.ajax({
                    url: "upload.php",
                    type: "POST",
                    data:  new FormData(this),
                    contentType: false,
                    cache: false,
                    processData:false,
                    success: function(data)
                    {
                        $("#targetLayer").html(data);
                    },
                    error: function() 
                    {
                    }             
                   });
            }));
        });
    </script>
    

The Form

<form id="uploadForm" action="upload.php" method="post">
                        <div class="row">
                            <div class="col-md-12 mb-1">
                                <div id="targetLayer">No Image</div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <input name="text" type="text" class="inputFile form-control" />
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <input name="userImage" type="file" class="inputFile form-control" />
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group text-center">
                                    <input type="submit" value="Save" name="aa" class="btnSubmit btn btn-success" />
                                </div>
                            </div>
                        </div>
                    </form>     

CodePudding user response:

In this form you mentioned that the name of the button is aa so it will become

if(isset($_POST['aa']) && $_POST['aa'] == 'Save') ...

CodePudding user response:

Submit buttons are not generally/automatically sent in ajax requests.
They aren't added to FormData or .serialize().
In your code, add this field manually.

<script type="text/javascript">
    $(document).ready(function (e) {
        $("#uploadForm").on('submit',(function(e) {
            e.preventDefault();
            var form = $(this);
            var url = form.attr('action');
            var formData = new FormData(this);
            formData.append('aa', 'Save'); 
            $.ajax({
                url: "upload.php",
                type: "POST",
                data:  formData,
                contentType: false,
                cache: false,
                processData:false,
                success: function(data)
                {
                    $("#targetLayer").html(data);
                },
                error: function() 
                {
                }             
               });
        }));
    });
</script>
  • Related