PHP - send data from a form to a modal bootstrap with php oop

1

I hope you can help me. it does not show any error and it does not respond when you click on the Save button. I tried the PHP insertion code on another page without booting and it works I wonder why it is not working in modal dialog.

Annex code to see if someone can help me solve it.

This is in the "category.php" file where I load the list and I have the bootstrap modal form.

    <?php
       $user = new User($db); //Objecto

        if(isset($_POST['submit']))
        {
            $name = $_POST['name'];
            $link = $_POST['link'];

            if($user->create($name,$link)) //INSERTAR
            {
                header("Location: category.php?inserted");
            }
            else
            {
                header("Location: category.php?failure");
            }
        }

        if(isset($_GET['inserted']))
        {
            echo "Exito";
        }
        else if(isset($_GET['failure']))
        {
            echo "Error";
        }

        echo "<div class='page-wrapper'>";

            echo "<div class='page-content'>";

                echo "<div class='container-fluid-md'>";
                    echo "<div class='row'>";

                            echo "<button type='button' class='btn btn-success' data-toggle='modal' data-target='#myAdd'>Añadir</button>";

                        ?>
        <!-- Modal -->
        <div class="modal fade" id="myAdd" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
              </div>
              <div class="modal-body">
                <?php
                echo "<form id='formADD' method='POST' action='#'>";
                echo "<div class='form-group'>";
                                echo "<label for='nm'>Nombre</label>";
                                echo "<input type='text' id="name" class='form-control' id='nm' name='name'>";
                            echo "</div>";

                            echo "<div class='form-group'>";
                                echo "<label for='nm'>Url</label>";
                                echo "<input type='text' id="url" class='form-control' id='nm' name='link'>";
                            echo "</div>";


                            echo "</form>";
                ?>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
                <button type="button" id="button"class="btn btn-primary" name="submit">Añadir</button>
              </div>
            </div>
          </div>
        </div>

                        <?php

                    echo "</div>";

                echo "</div>";

            echo "</div>";

<script type="text/javascript">
        $(document).ready(function(){

            $("#button").click(function(){

            var pathname = window.location.pathname;
            var name = $("#name").val();
            var url = $("#url").val();
            var parentID = $("#parent").val();
            // Returns successful data submission message when the entered information is stored in database.
            var data = 'name1='+ name + '&url1='+ url;

            if(name=='' || url==''|| parentID=='')
            {
                alert("Please Fill All Fields");
            }
            else
            {
                // AJAX Code To Submit Form.
                $.ajax({
                type: "POST",
                url: 'nav-menus.php',
                data: data,
                cache: false,
                success: function(result) {
                    alert(result);
                },
                 error: function(result) {
                    alert("Local error callback.", result);
                  }
                });
            }
            return false;
            });
        });
      </script>
    
asked by Diego Sagredo 21.03.2017 в 21:35
source

1 answer

1

first of all you have to take care that you are assigning 2 times the id to the <input> that you are adding and it is important that if in the echo you are using double quotes, use single quotes for the content of the echo.

Once verified this, you have to modify your 2 <input> to something like the following:

echo "<input type='text' id='name' class='form-control' name='name'>";

echo "<input type='text' id='url' class='form-control' name='link'>";

Regarding the sending of your form through AJAX, you have to assign the value of url as that of your current file, which in this case is category.php , plus you have to send the parameters with the name that you think them receive in your PHP that in this case the names are name and link so your AJAX code should look something like the following:

$.ajax({
   type: "POST",
   url: 'category.php',
   data: { name: name, link: url },
   cache: false,
   success: function(result) {
          alert(result);
   },
   error: function(result) {
          alert("Local error callback.", result);
   }
});
    
answered by 22.03.2017 в 06:53