How to put two buttons inside a form where each one sends the data to different pages?

Asked

Viewed 121 times

1

I have a form , where in the action I send the information to a PHP page , however , I need that when the user click a button send this form data to a page x and when click the other button go to page y .

Follows my code :

<?php include("cabecalho_busca.php")?>
    <br>
        <!-- ##### Breadcrumb Area Start ##### -->
    <div class="mag-breadcrumb py-3">

    </div>
    <!-- ##### Breadcrumb Area End ##### -->
  <div class="container">

        <form method="post" action="conexaobd.php">
         <div class="row align-items-center justify-content-center">
                <div class="row align-items-center justify-content-center">
                    <div class="col-md-2 pt-3">
                        <div class="form-group ">
                            <label for="filial">Filial 01</label>
                            <select class="form-control" name="filial" id="filial">
                                <option value="">Bahia</option>
                                <option value="">Pernambuco</option>
                                <option value="">Rio de Janeiro</option>
                                <option value="">São Paulo</option>
                                <option value="">Vitoria da Conquista</option>
                            </select>
                        </div>
                    </div>
                    <div class="col-md-2 pt-3">
                        <div class="form-group">
                            <label for="filial1">Filial 02</label>
                            <select class="form-control" name="filial1" id="filial1">
                                <option value="">Bahia</option>
                                <option value="">Pernambuco</option>
                                <option value="">Rio de Janeiro</option>
                                <option value="">São Paulo</option>
                                <option value="">Vitoria da Conquista</option>
                            </select>
                        </div>
                    </div>
                    <div class="col-md-2 pt-3">
                        <div class="form-group">
                            <label for="exampleInputEmail1">Data Inicial</label>
                            <input type="date" class="form-control" id="data_inicio" name="data_inicio">
                        </div>
                    </div>
                    <div class="col-md-2 pt-3">
                        <div class="form-group">

                            <label for="exampleInputEmail1">Data Final</label>
                            <input type="date" class="form-control" id="data_final" name="data_final">

                        </div>
                    </div>

                    <div class="col-md-2">
<br>                    
                       <button type="submit" class="btn btn-primary">Consultar</button>

                    </div>
                    <div class="col-md-2">
<br>                    
                       <button type="submit" class="btn btn-primary">Gerar</button>

                    </div>

            </div>
</div>

        </form>
   </div>
    <br>
        <!-- ##### Breadcrumb Area Start ##### -->
    <div class="mag-breadcrumb py-3">

    </div>
    <!-- ##### Breadcrumb Area End ##### -->
    <?php include("rodape.php")?>

2 answers

5


You can use the formaction attribute. Take this example:

<form action="/action_page.php">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit"><br>
  <input type="submit" formaction="/action_page2.php"
  value="Submit as admin">
</form>

Source: w3schools

  • Sincerely unaware such attribute. very useful, especially for those who do not know JS.

0

Did not know the HTML attribute formaction, but I built this example based on your code. Using Javascript’s Jquery library:

<!-- ##### Breadcrumb Area Start ##### -->
<div class="mag-breadcrumb py-3"></div>
<!-- ##### Breadcrumb Area End ##### -->
<div class="container">
    <form method="post" action="conexaobd.php" id="formulario">
        <div class="row align-items-center justify-content-center">
            <div class="row align-items-center justify-content-center">
                <div class="col-md-2 pt-3">
                    <div class="form-group ">
                        <label for="filial">Filial 01</label>
                        <select class="form-control" name="filial" id="filial">
                            <option value="">Bahia</option>
                            <option value="">Pernambuco</option>
                            <option value="">Rio de Janeiro</option>
                            <option value="">São Paulo</option>
                            <option value="">Vitoria da Conquista</option>
                        </select>
                    </div>
                </div>
                <div class="col-md-2 pt-3">
                    <div class="form-group">
                        <label for="filial1">Filial 02</label>
                        <select class="form-control" name="filial1" id="filial1">
                            <option value="">Bahia</option>
                            <option value="">Pernambuco</option>
                            <option value="">Rio de Janeiro</option>
                            <option value="">São Paulo</option>
                            <option value="">Vitoria da Conquista</option>
                        </select>
                    </div>
                </div>
                <div class="col-md-2 pt-3">
                    <div class="form-group">
                        <label for="exampleInputEmail1">Data Inicial</label>
                        <input type="date" class="form-control" id="data_inicio" name="data_inicio">
                    </div>
                </div>
                <div class="col-md-2 pt-3">
                    <div class="form-group">

                        <label for="exampleInputEmail1">Data Final</label>
                        <input type="date" class="form-control" id="data_final" name="data_final">

                    </div>
                </div>

                <div class="col-md-2">
                    <br>                    
                    <button type="submit" class="btn btn-primary" id="btn-consultar">Consultar</button>

                </div>
                <div class="col-md-2">
                    <br>                    
                    <button type="submit" class="btn btn-primary" id="btn-gerar">Gerar</button>
                </div>
            </div>
        </div>
    </form>
</div>
<br>
<!-- ##### Breadcrumb Area Start ##### -->
<div class="mag-breadcrumb py-3"></div>
<!-- ##### Breadcrumb Area End ##### -->
<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
<script type="text/javascript">
    $("#btn-gerar").click(function(evt){
        evt.preventDefault();
        $("#formulario").attr("action", "linkparacadastro");
        $("#formulario").submit();
    });
    $("#btn-consultar").click(function(evt){
        evt.preventDefault();
        $("#formulario").attr("action", "linkparaconsultar");
        $("#formulario").submit();
    });
</script>
  • However with this code , I can’t pull the data with my php . It returns me to nothing .

  • If you’re talking about affiliates, it’s because you need to fill in the attribute value of options, but the date I managed to catch giving a var_dump($_POST);

Browser other questions tagged

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