I would like to place two actions for the same form

Asked

Viewed 1,034 times

1

<form method="post" id="formulario_contato" onsubmit="validaForm(); return false; " class="form" action="?go=enviar">
                    <table id="login_table">
                    <div id="all">
                    <div id="login-box">
                    <p class="nome">
                    <input type="text" name="nome" id="nome" class="form-control" placeholder="Nome do paciente" />
                    </p>

                    <p class="idade">
                    <input type="text" name="idade" id="idade" class="form-control" placeholder="Idade do paciente" />
                    </p>

                    <p class="cpf">
                    <input type="text" name="cpf" id="cpf" class="form-control" placeholder="CPF do paciente" />
                    </p>

                    <!-- Botao Enviar -->
                    <p>
                    <center><input type="submit" value="Enviar" class="btn btn-info" href="sala_medico.php"/></center>
                    </p>
                    </div>
                    </div>
                    </table>
                    </form>

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

</body>
</html>

<?php
if(@$_GET['go']=='enviar'){
}

I’m registering people who can choose to answer a questionnaire or not. This way, I need two "register" buttons for this same form, one for each action. Should it be impossible to do so, any other solution is welcome.

  • When you speak 'two actions' you refer to the attribute action form? You wanted two buttons submit and if you click on a send to a action and if you click the other send to the other action?

  • Yes that’s right. I put on the page the script you sent and the form. But in the if to call the action I put what ?

  • It’s the way it is, you’ll just change the fields in the script addressAction1 and addressAction2 to specify the addresses of actions that you want for every occasion, ie the action in case the button clicked is 1 (inside the if) and the action in case the button clicked is 2 (inside the else).

  • I say this if here: <?php if(@$_GET['go']='send'){ } How I place to run a php function depending on the button you press. I didn’t understand how I run the action using php

  • That one if of php you will no longer need it, you will only need the script anyway, the script will already take care of detecting the clicked button and direct the data to the respective action.

  • Yes, but I wanted to use php inside the actions and not javascript. I’ve done the functions of the actions in php and it’s quite something :/

  • Then you should have made explicit in the question you wanted only in PHP and removed the javascript and jQuery tags, because, the way you put it, it is as if it served both in PHP, as in javascript/jquery.

Show 2 more comments

1 answer

1

If what you want is to change the attribute action of form according to the button clicked, follows example using jquery:

$("#btnenviar1, #btnenviar2").click(function(){
  //Recebe o id do botão clicado
  var id = $(this).attr('id');
  //Verifica qual foi o botão clicado através do id do mesmo e seta o action correspondente
  if (id == 'btnenviar1'){
      $('#formulario_contato').attr('action', 'endereçoAction1');
  }
  else {
      $('#formulario_contato').attr('action', 'endereçoAction2');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" id="formulario_contato" class="form" action="">
    <table id="login_table">
    <div id="all">
    <div id="login-box">
    <p class="nome">
    <input type="text" name="nome" id="nome" class="form-control" placeholder="Nome do paciente" />
    </p>

    <p class="idade">
    <input type="text" name="idade" id="idade" class="form-control" placeholder="Idade do paciente" />
    </p>

    <p class="cpf">
    <input type="text" name="cpf" id="cpf" class="form-control" placeholder="CPF do paciente" />
    </p>

    <!-- Botao Enviar -->
    <p>
    <center><input type="submit" id="btnenviar1" value="Cadastrar1" class="btn btn-info" href="sala_medico.php"/>
    <input type="submit" id="btnenviar2" value="Cadastrar2" class="btn btn-info" href="sala_medico.php"/></center>
    </p>
    </div>
    </div>
    </table>
</form>

Browser other questions tagged

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