Modal form not submitting user input into database












1















I have a modal form which allows user to add a new row to a table on a PHP webpage, then refreshes the page.



I've tried the following to no avail:



modal html code



<div class="modal-body mx-3">

<p class="statusMsg"></p>
<form role="form">
<div class="form-group">
<label class="label" for="inputName">Data Map Name</label><br>
<input type="text" class="form-control" name="name" id="inputName" placeholder="Enter data map name"/>
</div>

<div class="form-group">
<label class="label" for="inputDescription">Description</label><br>
<input type="text" class="form-control" name="description" id="inputDescription" placeholder="Enter description"/>
</div>

</form>
</div>

<div class="modal-footer d-flex justify-content-right">
<button type="button" class="btn btn-outline-info waves-effect " data-dismiss="modal">Close</button>
<button type="button" class="btn btn btn-success submitBtn" name="submit">Create New Data Map</button>

</div>


jquery code



var ajax_url = "<?php echo APPURL;?>/ajax.php" ;

$(document).on('click', '.submitBtn', function(event) {


var datamapname = $('#inputName').val();
var description = $('#inputDescription').val();


var data_obj=
{
call_type:'new_row_entry_vdm',
datamapname:datamapname,
description:description

};


$.post(ajax_url, data_obj, function(data)
{
var d1 = JSON.parse(data);
location.href=location.href;


if(d1.status == "error")
{

$('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
}
else if(d1.status == "success")
{
$('#inputName').val('')
$('#inputDescription').val('')

location.href=location.href;

}
}


ajax code



//--->new row entry for viewdatamap > start
if(isset($_POST['call_type']) && $_POST['call_type'] =="new_row_entry_vdm")
{

$row_id = substr(md5(microtime()),rand(0,30));
$datamapname = app_db()->CleanDBData($_POST['datamapname']);
$description = app_db()->CleanDBData($_POST['description']);



$newrowvdm = app_db()->select("select * from viewdatamap where
row_id='$row_id'");
if($newrowvdm < 1)
{

$strTableName = "viewdatamap";

$insert_arrays = array
(

'row_id' => $row_id,
'datamapname' => $datamapname,
'description' => $description

);

//Insert into viewdatamap.
app_db()->Insert($strTableName, $insert_arrays);
}


The form doesn't submit when I click on "Create New Data Map" and I'm not sure why.



P.S Ignore the app_db function inside the ajax code; it has nothing to do with the problem I'm facing.










share|improve this question





























    1















    I have a modal form which allows user to add a new row to a table on a PHP webpage, then refreshes the page.



    I've tried the following to no avail:



    modal html code



    <div class="modal-body mx-3">

    <p class="statusMsg"></p>
    <form role="form">
    <div class="form-group">
    <label class="label" for="inputName">Data Map Name</label><br>
    <input type="text" class="form-control" name="name" id="inputName" placeholder="Enter data map name"/>
    </div>

    <div class="form-group">
    <label class="label" for="inputDescription">Description</label><br>
    <input type="text" class="form-control" name="description" id="inputDescription" placeholder="Enter description"/>
    </div>

    </form>
    </div>

    <div class="modal-footer d-flex justify-content-right">
    <button type="button" class="btn btn-outline-info waves-effect " data-dismiss="modal">Close</button>
    <button type="button" class="btn btn btn-success submitBtn" name="submit">Create New Data Map</button>

    </div>


    jquery code



    var ajax_url = "<?php echo APPURL;?>/ajax.php" ;

    $(document).on('click', '.submitBtn', function(event) {


    var datamapname = $('#inputName').val();
    var description = $('#inputDescription').val();


    var data_obj=
    {
    call_type:'new_row_entry_vdm',
    datamapname:datamapname,
    description:description

    };


    $.post(ajax_url, data_obj, function(data)
    {
    var d1 = JSON.parse(data);
    location.href=location.href;


    if(d1.status == "error")
    {

    $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
    }
    else if(d1.status == "success")
    {
    $('#inputName').val('')
    $('#inputDescription').val('')

    location.href=location.href;

    }
    }


    ajax code



    //--->new row entry for viewdatamap > start
    if(isset($_POST['call_type']) && $_POST['call_type'] =="new_row_entry_vdm")
    {

    $row_id = substr(md5(microtime()),rand(0,30));
    $datamapname = app_db()->CleanDBData($_POST['datamapname']);
    $description = app_db()->CleanDBData($_POST['description']);



    $newrowvdm = app_db()->select("select * from viewdatamap where
    row_id='$row_id'");
    if($newrowvdm < 1)
    {

    $strTableName = "viewdatamap";

    $insert_arrays = array
    (

    'row_id' => $row_id,
    'datamapname' => $datamapname,
    'description' => $description

    );

    //Insert into viewdatamap.
    app_db()->Insert($strTableName, $insert_arrays);
    }


    The form doesn't submit when I click on "Create New Data Map" and I'm not sure why.



    P.S Ignore the app_db function inside the ajax code; it has nothing to do with the problem I'm facing.










    share|improve this question



























      1












      1








      1








      I have a modal form which allows user to add a new row to a table on a PHP webpage, then refreshes the page.



      I've tried the following to no avail:



      modal html code



      <div class="modal-body mx-3">

      <p class="statusMsg"></p>
      <form role="form">
      <div class="form-group">
      <label class="label" for="inputName">Data Map Name</label><br>
      <input type="text" class="form-control" name="name" id="inputName" placeholder="Enter data map name"/>
      </div>

      <div class="form-group">
      <label class="label" for="inputDescription">Description</label><br>
      <input type="text" class="form-control" name="description" id="inputDescription" placeholder="Enter description"/>
      </div>

      </form>
      </div>

      <div class="modal-footer d-flex justify-content-right">
      <button type="button" class="btn btn-outline-info waves-effect " data-dismiss="modal">Close</button>
      <button type="button" class="btn btn btn-success submitBtn" name="submit">Create New Data Map</button>

      </div>


      jquery code



      var ajax_url = "<?php echo APPURL;?>/ajax.php" ;

      $(document).on('click', '.submitBtn', function(event) {


      var datamapname = $('#inputName').val();
      var description = $('#inputDescription').val();


      var data_obj=
      {
      call_type:'new_row_entry_vdm',
      datamapname:datamapname,
      description:description

      };


      $.post(ajax_url, data_obj, function(data)
      {
      var d1 = JSON.parse(data);
      location.href=location.href;


      if(d1.status == "error")
      {

      $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
      }
      else if(d1.status == "success")
      {
      $('#inputName').val('')
      $('#inputDescription').val('')

      location.href=location.href;

      }
      }


      ajax code



      //--->new row entry for viewdatamap > start
      if(isset($_POST['call_type']) && $_POST['call_type'] =="new_row_entry_vdm")
      {

      $row_id = substr(md5(microtime()),rand(0,30));
      $datamapname = app_db()->CleanDBData($_POST['datamapname']);
      $description = app_db()->CleanDBData($_POST['description']);



      $newrowvdm = app_db()->select("select * from viewdatamap where
      row_id='$row_id'");
      if($newrowvdm < 1)
      {

      $strTableName = "viewdatamap";

      $insert_arrays = array
      (

      'row_id' => $row_id,
      'datamapname' => $datamapname,
      'description' => $description

      );

      //Insert into viewdatamap.
      app_db()->Insert($strTableName, $insert_arrays);
      }


      The form doesn't submit when I click on "Create New Data Map" and I'm not sure why.



      P.S Ignore the app_db function inside the ajax code; it has nothing to do with the problem I'm facing.










      share|improve this question
















      I have a modal form which allows user to add a new row to a table on a PHP webpage, then refreshes the page.



      I've tried the following to no avail:



      modal html code



      <div class="modal-body mx-3">

      <p class="statusMsg"></p>
      <form role="form">
      <div class="form-group">
      <label class="label" for="inputName">Data Map Name</label><br>
      <input type="text" class="form-control" name="name" id="inputName" placeholder="Enter data map name"/>
      </div>

      <div class="form-group">
      <label class="label" for="inputDescription">Description</label><br>
      <input type="text" class="form-control" name="description" id="inputDescription" placeholder="Enter description"/>
      </div>

      </form>
      </div>

      <div class="modal-footer d-flex justify-content-right">
      <button type="button" class="btn btn-outline-info waves-effect " data-dismiss="modal">Close</button>
      <button type="button" class="btn btn btn-success submitBtn" name="submit">Create New Data Map</button>

      </div>


      jquery code



      var ajax_url = "<?php echo APPURL;?>/ajax.php" ;

      $(document).on('click', '.submitBtn', function(event) {


      var datamapname = $('#inputName').val();
      var description = $('#inputDescription').val();


      var data_obj=
      {
      call_type:'new_row_entry_vdm',
      datamapname:datamapname,
      description:description

      };


      $.post(ajax_url, data_obj, function(data)
      {
      var d1 = JSON.parse(data);
      location.href=location.href;


      if(d1.status == "error")
      {

      $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
      }
      else if(d1.status == "success")
      {
      $('#inputName').val('')
      $('#inputDescription').val('')

      location.href=location.href;

      }
      }


      ajax code



      //--->new row entry for viewdatamap > start
      if(isset($_POST['call_type']) && $_POST['call_type'] =="new_row_entry_vdm")
      {

      $row_id = substr(md5(microtime()),rand(0,30));
      $datamapname = app_db()->CleanDBData($_POST['datamapname']);
      $description = app_db()->CleanDBData($_POST['description']);



      $newrowvdm = app_db()->select("select * from viewdatamap where
      row_id='$row_id'");
      if($newrowvdm < 1)
      {

      $strTableName = "viewdatamap";

      $insert_arrays = array
      (

      'row_id' => $row_id,
      'datamapname' => $datamapname,
      'description' => $description

      );

      //Insert into viewdatamap.
      app_db()->Insert($strTableName, $insert_arrays);
      }


      The form doesn't submit when I click on "Create New Data Map" and I'm not sure why.



      P.S Ignore the app_db function inside the ajax code; it has nothing to do with the problem I'm facing.







      javascript jquery mysql ajax modal-dialog






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 29 '18 at 9:02







      wei123

















      asked Dec 29 '18 at 8:29









      wei123wei123

      456




      456
























          2 Answers
          2






          active

          oldest

          votes


















          1














          from the comment I see that you have error




          Uncaught ReferenceError: $ is not defined




          it is because missing jQuery, add this line before the </head>



          <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>


          now you can submit the form using Ajax but your script is invalid



          $.post(ajax_url, data_obj, function(data) {
          var d1 = JSON.parse(data);
          location.href = location.href; <-- here


          It will refresh the page no matter if it success or error and here fixed code



          var ajax_url = "<?php echo APPURL;?>/ajax.php";

          $(document).on('click', '.submitBtn', function(event) {

          var datamapname = $('#inputName').val();
          var description = $('#inputDescription').val();

          var data_obj = {
          call_type: 'new_row_entry_vdm',
          datamapname: datamapname,
          description: description
          };

          $.post(ajax_url, data_obj, function(data) {
          var d1 = JSON.parse(data);
          if (d1.status == "success") {
          // its good reload the page
          location.href = location.href;
          }
          else {
          // its error, reset the input?
          $('#inputName').val('')
          $('#inputDescription').val('')
          $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
          }
          })
          })





          share|improve this answer































            1














            You need to wrap your modal logic into the shown.bs.modal event and then search for the fields inside your modal when the ajax button is clicked.



            $('.modal-body').on('shown.bs.modal', function() { // when modal is opened

            var modal = $(this);
            var btnSubmit = modal.find('button.submitBtn');

            var ajax_url = "<?php echo APPURL;?>/ajax.php";

            btnSubmit.on('click', function(event) {

            event.preventDefault(); // prevent form submit

            var datamapname = modal.find('#inputName').val();
            var description = modal.find('#inputDescription').val();


            var data_obj = {
            call_type: 'new_row_entry_vdm',
            datamapname: datamapname,
            description: description

            };


            $.post(ajax_url, data_obj, function(data) {
            var d1 = JSON.parse(data);
            location.href = location.href;


            if (d1.status == "error") {

            $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
            } else if (d1.status == "success") {
            $('#inputName').val('')
            $('#inputDescription').val('')

            location.href = location.href;

            }

            })

            })

            })





            share|improve this answer
























            • It still doesn't work :(

              – wei123
              Dec 29 '18 at 9:06











            • @wei123 is .modal-body in html or it's being created programatically ?

              – darklightcode
              Dec 29 '18 at 9:10











            • I took the class modal-body mx-3 from here

              – wei123
              Dec 29 '18 at 9:14













            • Try changing $('.modal-body').on('shown.bs.modal', function() { to $('body').on('shown.bs.modal', '.modal-body', function() { . And check the developers tools if your request is sent

              – darklightcode
              Dec 29 '18 at 9:17













            • When I clicked on the "create new data map" button, developer tools doesn't show the call type for "new_row_entry_vdm" . I also got this error.

              – wei123
              Dec 29 '18 at 9:45













            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53967963%2fmodal-form-not-submitting-user-input-into-database%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            from the comment I see that you have error




            Uncaught ReferenceError: $ is not defined




            it is because missing jQuery, add this line before the </head>



            <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>


            now you can submit the form using Ajax but your script is invalid



            $.post(ajax_url, data_obj, function(data) {
            var d1 = JSON.parse(data);
            location.href = location.href; <-- here


            It will refresh the page no matter if it success or error and here fixed code



            var ajax_url = "<?php echo APPURL;?>/ajax.php";

            $(document).on('click', '.submitBtn', function(event) {

            var datamapname = $('#inputName').val();
            var description = $('#inputDescription').val();

            var data_obj = {
            call_type: 'new_row_entry_vdm',
            datamapname: datamapname,
            description: description
            };

            $.post(ajax_url, data_obj, function(data) {
            var d1 = JSON.parse(data);
            if (d1.status == "success") {
            // its good reload the page
            location.href = location.href;
            }
            else {
            // its error, reset the input?
            $('#inputName').val('')
            $('#inputDescription').val('')
            $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
            }
            })
            })





            share|improve this answer




























              1














              from the comment I see that you have error




              Uncaught ReferenceError: $ is not defined




              it is because missing jQuery, add this line before the </head>



              <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>


              now you can submit the form using Ajax but your script is invalid



              $.post(ajax_url, data_obj, function(data) {
              var d1 = JSON.parse(data);
              location.href = location.href; <-- here


              It will refresh the page no matter if it success or error and here fixed code



              var ajax_url = "<?php echo APPURL;?>/ajax.php";

              $(document).on('click', '.submitBtn', function(event) {

              var datamapname = $('#inputName').val();
              var description = $('#inputDescription').val();

              var data_obj = {
              call_type: 'new_row_entry_vdm',
              datamapname: datamapname,
              description: description
              };

              $.post(ajax_url, data_obj, function(data) {
              var d1 = JSON.parse(data);
              if (d1.status == "success") {
              // its good reload the page
              location.href = location.href;
              }
              else {
              // its error, reset the input?
              $('#inputName').val('')
              $('#inputDescription').val('')
              $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
              }
              })
              })





              share|improve this answer


























                1












                1








                1







                from the comment I see that you have error




                Uncaught ReferenceError: $ is not defined




                it is because missing jQuery, add this line before the </head>



                <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>


                now you can submit the form using Ajax but your script is invalid



                $.post(ajax_url, data_obj, function(data) {
                var d1 = JSON.parse(data);
                location.href = location.href; <-- here


                It will refresh the page no matter if it success or error and here fixed code



                var ajax_url = "<?php echo APPURL;?>/ajax.php";

                $(document).on('click', '.submitBtn', function(event) {

                var datamapname = $('#inputName').val();
                var description = $('#inputDescription').val();

                var data_obj = {
                call_type: 'new_row_entry_vdm',
                datamapname: datamapname,
                description: description
                };

                $.post(ajax_url, data_obj, function(data) {
                var d1 = JSON.parse(data);
                if (d1.status == "success") {
                // its good reload the page
                location.href = location.href;
                }
                else {
                // its error, reset the input?
                $('#inputName').val('')
                $('#inputDescription').val('')
                $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
                }
                })
                })





                share|improve this answer













                from the comment I see that you have error




                Uncaught ReferenceError: $ is not defined




                it is because missing jQuery, add this line before the </head>



                <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>


                now you can submit the form using Ajax but your script is invalid



                $.post(ajax_url, data_obj, function(data) {
                var d1 = JSON.parse(data);
                location.href = location.href; <-- here


                It will refresh the page no matter if it success or error and here fixed code



                var ajax_url = "<?php echo APPURL;?>/ajax.php";

                $(document).on('click', '.submitBtn', function(event) {

                var datamapname = $('#inputName').val();
                var description = $('#inputDescription').val();

                var data_obj = {
                call_type: 'new_row_entry_vdm',
                datamapname: datamapname,
                description: description
                };

                $.post(ajax_url, data_obj, function(data) {
                var d1 = JSON.parse(data);
                if (d1.status == "success") {
                // its good reload the page
                location.href = location.href;
                }
                else {
                // its error, reset the input?
                $('#inputName').val('')
                $('#inputDescription').val('')
                $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
                }
                })
                })






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 29 '18 at 10:30









                ewwinkewwink

                11.6k22238




                11.6k22238

























                    1














                    You need to wrap your modal logic into the shown.bs.modal event and then search for the fields inside your modal when the ajax button is clicked.



                    $('.modal-body').on('shown.bs.modal', function() { // when modal is opened

                    var modal = $(this);
                    var btnSubmit = modal.find('button.submitBtn');

                    var ajax_url = "<?php echo APPURL;?>/ajax.php";

                    btnSubmit.on('click', function(event) {

                    event.preventDefault(); // prevent form submit

                    var datamapname = modal.find('#inputName').val();
                    var description = modal.find('#inputDescription').val();


                    var data_obj = {
                    call_type: 'new_row_entry_vdm',
                    datamapname: datamapname,
                    description: description

                    };


                    $.post(ajax_url, data_obj, function(data) {
                    var d1 = JSON.parse(data);
                    location.href = location.href;


                    if (d1.status == "error") {

                    $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
                    } else if (d1.status == "success") {
                    $('#inputName').val('')
                    $('#inputDescription').val('')

                    location.href = location.href;

                    }

                    })

                    })

                    })





                    share|improve this answer
























                    • It still doesn't work :(

                      – wei123
                      Dec 29 '18 at 9:06











                    • @wei123 is .modal-body in html or it's being created programatically ?

                      – darklightcode
                      Dec 29 '18 at 9:10











                    • I took the class modal-body mx-3 from here

                      – wei123
                      Dec 29 '18 at 9:14













                    • Try changing $('.modal-body').on('shown.bs.modal', function() { to $('body').on('shown.bs.modal', '.modal-body', function() { . And check the developers tools if your request is sent

                      – darklightcode
                      Dec 29 '18 at 9:17













                    • When I clicked on the "create new data map" button, developer tools doesn't show the call type for "new_row_entry_vdm" . I also got this error.

                      – wei123
                      Dec 29 '18 at 9:45


















                    1














                    You need to wrap your modal logic into the shown.bs.modal event and then search for the fields inside your modal when the ajax button is clicked.



                    $('.modal-body').on('shown.bs.modal', function() { // when modal is opened

                    var modal = $(this);
                    var btnSubmit = modal.find('button.submitBtn');

                    var ajax_url = "<?php echo APPURL;?>/ajax.php";

                    btnSubmit.on('click', function(event) {

                    event.preventDefault(); // prevent form submit

                    var datamapname = modal.find('#inputName').val();
                    var description = modal.find('#inputDescription').val();


                    var data_obj = {
                    call_type: 'new_row_entry_vdm',
                    datamapname: datamapname,
                    description: description

                    };


                    $.post(ajax_url, data_obj, function(data) {
                    var d1 = JSON.parse(data);
                    location.href = location.href;


                    if (d1.status == "error") {

                    $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
                    } else if (d1.status == "success") {
                    $('#inputName').val('')
                    $('#inputDescription').val('')

                    location.href = location.href;

                    }

                    })

                    })

                    })





                    share|improve this answer
























                    • It still doesn't work :(

                      – wei123
                      Dec 29 '18 at 9:06











                    • @wei123 is .modal-body in html or it's being created programatically ?

                      – darklightcode
                      Dec 29 '18 at 9:10











                    • I took the class modal-body mx-3 from here

                      – wei123
                      Dec 29 '18 at 9:14













                    • Try changing $('.modal-body').on('shown.bs.modal', function() { to $('body').on('shown.bs.modal', '.modal-body', function() { . And check the developers tools if your request is sent

                      – darklightcode
                      Dec 29 '18 at 9:17













                    • When I clicked on the "create new data map" button, developer tools doesn't show the call type for "new_row_entry_vdm" . I also got this error.

                      – wei123
                      Dec 29 '18 at 9:45
















                    1












                    1








                    1







                    You need to wrap your modal logic into the shown.bs.modal event and then search for the fields inside your modal when the ajax button is clicked.



                    $('.modal-body').on('shown.bs.modal', function() { // when modal is opened

                    var modal = $(this);
                    var btnSubmit = modal.find('button.submitBtn');

                    var ajax_url = "<?php echo APPURL;?>/ajax.php";

                    btnSubmit.on('click', function(event) {

                    event.preventDefault(); // prevent form submit

                    var datamapname = modal.find('#inputName').val();
                    var description = modal.find('#inputDescription').val();


                    var data_obj = {
                    call_type: 'new_row_entry_vdm',
                    datamapname: datamapname,
                    description: description

                    };


                    $.post(ajax_url, data_obj, function(data) {
                    var d1 = JSON.parse(data);
                    location.href = location.href;


                    if (d1.status == "error") {

                    $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
                    } else if (d1.status == "success") {
                    $('#inputName').val('')
                    $('#inputDescription').val('')

                    location.href = location.href;

                    }

                    })

                    })

                    })





                    share|improve this answer













                    You need to wrap your modal logic into the shown.bs.modal event and then search for the fields inside your modal when the ajax button is clicked.



                    $('.modal-body').on('shown.bs.modal', function() { // when modal is opened

                    var modal = $(this);
                    var btnSubmit = modal.find('button.submitBtn');

                    var ajax_url = "<?php echo APPURL;?>/ajax.php";

                    btnSubmit.on('click', function(event) {

                    event.preventDefault(); // prevent form submit

                    var datamapname = modal.find('#inputName').val();
                    var description = modal.find('#inputDescription').val();


                    var data_obj = {
                    call_type: 'new_row_entry_vdm',
                    datamapname: datamapname,
                    description: description

                    };


                    $.post(ajax_url, data_obj, function(data) {
                    var d1 = JSON.parse(data);
                    location.href = location.href;


                    if (d1.status == "error") {

                    $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
                    } else if (d1.status == "success") {
                    $('#inputName').val('')
                    $('#inputDescription').val('')

                    location.href = location.href;

                    }

                    })

                    })

                    })






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 29 '18 at 8:50









                    darklightcodedarklightcode

                    1,25689




                    1,25689













                    • It still doesn't work :(

                      – wei123
                      Dec 29 '18 at 9:06











                    • @wei123 is .modal-body in html or it's being created programatically ?

                      – darklightcode
                      Dec 29 '18 at 9:10











                    • I took the class modal-body mx-3 from here

                      – wei123
                      Dec 29 '18 at 9:14













                    • Try changing $('.modal-body').on('shown.bs.modal', function() { to $('body').on('shown.bs.modal', '.modal-body', function() { . And check the developers tools if your request is sent

                      – darklightcode
                      Dec 29 '18 at 9:17













                    • When I clicked on the "create new data map" button, developer tools doesn't show the call type for "new_row_entry_vdm" . I also got this error.

                      – wei123
                      Dec 29 '18 at 9:45





















                    • It still doesn't work :(

                      – wei123
                      Dec 29 '18 at 9:06











                    • @wei123 is .modal-body in html or it's being created programatically ?

                      – darklightcode
                      Dec 29 '18 at 9:10











                    • I took the class modal-body mx-3 from here

                      – wei123
                      Dec 29 '18 at 9:14













                    • Try changing $('.modal-body').on('shown.bs.modal', function() { to $('body').on('shown.bs.modal', '.modal-body', function() { . And check the developers tools if your request is sent

                      – darklightcode
                      Dec 29 '18 at 9:17













                    • When I clicked on the "create new data map" button, developer tools doesn't show the call type for "new_row_entry_vdm" . I also got this error.

                      – wei123
                      Dec 29 '18 at 9:45



















                    It still doesn't work :(

                    – wei123
                    Dec 29 '18 at 9:06





                    It still doesn't work :(

                    – wei123
                    Dec 29 '18 at 9:06













                    @wei123 is .modal-body in html or it's being created programatically ?

                    – darklightcode
                    Dec 29 '18 at 9:10





                    @wei123 is .modal-body in html or it's being created programatically ?

                    – darklightcode
                    Dec 29 '18 at 9:10













                    I took the class modal-body mx-3 from here

                    – wei123
                    Dec 29 '18 at 9:14







                    I took the class modal-body mx-3 from here

                    – wei123
                    Dec 29 '18 at 9:14















                    Try changing $('.modal-body').on('shown.bs.modal', function() { to $('body').on('shown.bs.modal', '.modal-body', function() { . And check the developers tools if your request is sent

                    – darklightcode
                    Dec 29 '18 at 9:17







                    Try changing $('.modal-body').on('shown.bs.modal', function() { to $('body').on('shown.bs.modal', '.modal-body', function() { . And check the developers tools if your request is sent

                    – darklightcode
                    Dec 29 '18 at 9:17















                    When I clicked on the "create new data map" button, developer tools doesn't show the call type for "new_row_entry_vdm" . I also got this error.

                    – wei123
                    Dec 29 '18 at 9:45







                    When I clicked on the "create new data map" button, developer tools doesn't show the call type for "new_row_entry_vdm" . I also got this error.

                    – wei123
                    Dec 29 '18 at 9:45




















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53967963%2fmodal-form-not-submitting-user-input-into-database%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Mossoró

                    Error while reading .h5 file using the rhdf5 package in R

                    Pushsharp Apns notification error: 'InvalidToken'