Submit form to a php script and open a modal as soon as it is submitted

Asked

Viewed 1,855 times

4

I have a form that already validates the fields, but I wanted when clicking the send button to send the data to the file recebe.php and already opened a modal that I created.

Follow the code on my form:

<form class="tab-pane transition scale fade in active " id="meuFormulario" autocomplete="off" method="post" action="painel/recebe.php">              
    <div class="form-group">
        <label class="sr-only">Seu nome completo</label>
        <input type="text" class="form-control" name="nomeCliente" placeholder="Seu nome completo"  required="">
        <span class="error-label"></span>
        <span class="valid-label"></span>
    </div>

    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                <label for="fd_name" class="sr-only">Nome da loja/assistência</label>
                <input type="text" class="form-control" name="nome" id="fd_name" placeholder="Nome da loja/assistência" required="">
                <span class="error-label"></span>
                <span class="valid-label"></span>
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group">
                <label for="si_email" class="sr-only">E-mail comercial</label>
                <input type="email" class="form-control" name="email" id="si_email" placeholder="E-mail comercial"  required="">
                <span class="error-label"></span>
                <span class="valid-label"></span>
            </div>
        </div>
        <div class="space-top-2x clearfix">
            <button type="submit" class="btn btn-success pull-right">
                <i class="flaticon-correct7"></i> Enviar
            </button>
            <a href="#" data-toggle="modal" data-target="#signin-page" data-modal-form="sign-in" class="nav-link">Sign In</a>
        </div>
</form>

Grateful

  • Hello, welcome to Stack Overflow, do our Tuor to proceed in using the community. Ps.: Identando the code to edit the question and make it more presentable I realized that is missing the closing of <div class="row">

  • You can enter the modal code?

1 answer

3


I believe that the expected result can come with AJAX, I did not understand the doubt very well, and it does not have the modal code but I will illustrate.

In giving submit in the form you will send a ajax to receive.php and after collecting the received data will display the modal. This with Jquery:

// ao dubmeter formulário
$('#myForm').submit(function()
{
    // AJAX faz requisição no script, caso nunca usou ajax, saiba
    // que ele aguada um retorno do PHP, seja um echo ou algo assim
    $.ajax
    ({
        url: pagina, // script PHP
        type: 'POST ou GET', // metodo (caso tenha de enviar dados ao script)
        data:  seus-dados, // dados a enviar, caso tenha
        mimeType:"multipart/form-data", // caso for enviar arquivo
        contentType: false,
        cache: false,
        processData:false,
        success: function(data, textStatus, jqXHR)
        {
            // se houve sucesso ao contatar pagina php ou coletar retorno
            // a variavel data é o seu retorno do PHP
            // aqui você irá exibir o MODAL
        },
        error: function(jqXHR, textStatus, errorThrown) 
        {
            // Em caso de erro
        }          
    });
});

PS.: I think you can answer the question with that, but if you want an example, I can make one quickly. I’ll see if I can find an example of this in my other answers.

You can see in this question an example of ajax sending data and executing action after the form is submitted: Convert php code to reply ajax

As requested, here is a simple example, where I have a form with the message field, and when submitting this form I send the data of it to a php script, in this php I just validate the data, seeing if it has more than 3 characters, if it has more than 3 characters the typed message is returned and so it is displayed in the modal, if it has less or 3 characters, then a fault message is returned that makes an error text be displayed on the screen.

The file structure of the example is:

raiz
- index.php
- script.php
- jquery.min.js
- bootstrap/
----- css/...
----- fonts/...
----- js/...

The index php.:

<!doctype html>
<html>
    <head>
        <meta charset='uft-8' />
        <title>Example Modal AJAX</title>
        <link rel='stylesheet' href="bootstrap/css/bootstrap.min.css"/>
    </head>

    <body>
        <form class='form form-inline' id='myForm' method='post'>
            <label>Message:</label>
            <input name='msg' type='text' class='form-control' placeholder='Digit your message' />

            <input class='btn btn-sm btn-primary' type='submit' value='Send' />
        </form>
        <h4 class='text-danger' id='myError'>Failure, check your message</h4>

        <div id='myModal' class="modal fade" tabindex="-1" role="dialog">
            <div class="modal-dialog">
                <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 text-success">Message was received!</h4>
                    </div>

                    <div class="modal-body text-success">
                        <p>Your message: <span id='myMsg'></span></p>
                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-success" data-dismiss="modal">Close</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->

        <script src='jquery.min.js'></script>
        <script src='bootstrap/js/bootstrap.min.js'></script>
        <script>
            $(function()
            {
                $('#myError').hide();
                $('#myForm').submit(function(e)
                {

                    $.ajax
                    ({
                        url: 'script.php', 
                        type: 'POST',
                        data:  new FormData(this),
                        contentType: false,
                        cache: false,
                        processData:false,
                        success: function(data, textStatus, jqXHR)
                        {
                            if(data != "fail")
                            {
                                $('#myMsg').text(data);
                                $('#myModal').modal('show');
                            }
                            else
                            {
                                $('#myError').show('slow');
                            }
                        },
                        error: function(jqXHR, textStatus, errorThrown) 
                        {
                        }          
                    });
                    e.preventDefault();

                });
            });
        </script>
    </body>
</html>

It is a simple page, where I have a form, a text message and a modal. In the script of Jquery, the error message is hidden, and when the form is submitted, send its data to my php script if the return is different from the string fail, i display my modal if I do not display my error text.

In the php script.:

<?php
    if(isset($_POST['msg']) and mb_strlen($_POST['msg']) > 3)
        echo $_POST['msg'];
    else
        echo 'fail';

I do just one validation, to check if the two paths are working.

  • Thanks, if you can send me a little model would be great, I don’t know much about js, so for me it will be better. vlw

  • I’m putting it in the answer

  • @Hudsonvicentim, the answer has been updated with the example, I hope it solves the problem.

  • 1

    @Perfect IVCS, thanks for the response and dedication to the post ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.