skip to Main Content

Am trying to submit data using modal bootstrap but i can’t get it to work. The page will contain more than one modal to enable user to easily update their details.Here the code.

    <div class="modal fade" id="overview-modal" role="dialog">
                <div class="modal-dialog">
                    <!-- Modal content-->
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">Overview</h4>
                        </div>
                        <form  id="overview_form" action="neuro/update_profile.php" class="form-horizontal" >
                            <div class="modal-body">
                                <div class="form-group">
                                    <label for="inputEmail3" class="col-sm-2 control-label">Overview</label>
                                    <div class="col-sm-10">
                                        <textarea class="form-control" id="overview_input" cols="20" rows="10" name="overview_textarea">

                                        </textarea>
<input type="hidden" name="url"/>
                                    </div>
                                </div>

                                <div class="form-group">

                                </div>
                            </div>
                            <div class="modal-footer">
                                <div class="btn-group">

                                    <input type="submit" id="save" class="btn btn-success" value="Save"/>
                                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
                                </div>

                            </div>
                        </form>
                    </div>
                </div>
            </div>

This is the javascript that will handle the form submission to the php file

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


            $("#save").submit(function () {
                var formData = $("form#overview_form").serialize();
                var my_url = "neuro/update_profile.php";
                $.ajax({
                    type: "POST",
                    url: my_url,
                    data: formData ,
                    success: function (msg) {
                        $("#overview-modal").modal('hide');
                    },
                    error: function () {
                        alert("Error encounted");
                    }
                });

               return false;
            });
        });
    </script>

The php is in a folder called neoro/update_profile ,since i will be using more than one modal in the profile page i have decided to append a url on the form attributes so that it can be easily processed in the php

    $profile = new Profile();
$url = $_POST['url'];
if (isset($url)) {
    if ($id == "overview") {

        $overview = $_POST['overview'];
        $profile->save_overview($user_id, $overview);
        return TRUE;
    }
} else {
    header("Location: ./profile.php");
}

2

Answers


  1. Do this:

    $('#overview_form').submit(// your code here);
    

    Instead of on #save, since you are submitting form not button 😉

    Or

    Even you can achieve same thing on #save but with click function as below

     $('#save').click( // here your code );
    
    Login or Signup to reply.
  2. This is full of errors

    1. You are sending the form via GET method and accessing it via POST method
    2. Your URL field is empty
    3. you are using action field and also submit simultaneously
    4. your jquery code is not correct
    5. you are using $id variable in your php code which is undefined

    Corrected code:

    1. <form id="overview_form" class="form-horizontal" method="post">

    2. <input type="hidden" name="url" value='yoururl'/>

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search