Form Validation with Submit

2

In My project I have three forms being one independent of the other. The form1, form2 e form3 . The form1 contains mandatory fields to be filled, and that data will be necessary to generate the information of the other two forms . So follow my problem, how can I check to see if form1 was filled in at the time I submit it for form2 ou form3 ? I was able to handle this with PHP , by the time it arrives on the page PHP I make a isset on POST to see if form1 data came too, but I need this work done on the side of the client.

<form class="form-horizontal" id="FormInfObg" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
    <input type="text" class="form-control" name="nome" placeholder='<?php if(isset($_POST['nome'])){$_SESSION['nome']=$_POST["nome"];echo $_SESSION['nome'];}else{ echo "Nome Completo";}?>'>
    <input type="text" class="form-control" name="cargo" placeholder='<?php if(isset($_POST['cargo'])){$_SESSION['cargo']=$_POST["cargo"];echo $_SESSION['cargo'];}else{ echo "Cargo";}?>'>
    <button class="btn btn-default" name="btnSalvar" value="btnSalvar">Salvar <span class="fa fa-floppy-o"></span></button>
</form>

 <form class="form-horizontal" id="Form2" action="proc.php" method="POST">
    <input type="text" class="form-control" name="filial">
    <button class="btn btn-default" type="submit" value="btnSubmitForm2">Enviar</button>
</form>

<form class="form-horizontal" id="Form3" action="proc.php" method="POST">
    <input type="text" class="form-control" name="nSerial">
    <button class="btn btn-default" type="submit" value="btnSubmitForm3">Enviar</button>
</form>
    
asked by anonymous 09.11.2016 / 14:05

2 answers

0

Well if you have 2 forms and they depend on form1, I suggest you put form1's data in hidden fields in 2 forms ...

I imagine the stream to be this ...

1st user fills in form1

2nd user clicks (on some button, to save in the server or to give continuity)

3rd depending on the data that was entered the user is redirected to form2 or form3

4 ° form1 data is set to <input type="hidden" name="dado_do_form1"/> via javascript in the form that was redirected

5th user fills in the remaining form2 or form3 data and sends to server

6th server checks incoming data

If you have 2 forms and submit on 1 of them, the data from the other form will not be sent.

    
09.11.2016 / 14:24
0

You can validate in the submit event of the form, and if the user did not fill in the values of the first, the second did not will be submitted.

$("#form2").submit(function(event) {
  event.preventDefault();      

  if (!validarForm1())
    alert("Primeiro preencha o primeiro formulário");
  else {
    // post por ajax do segundo formulário
  }

});

$("#form3").submit(function(event) {
 // faca a mesma coisa 
});

function validarForm1(){
    var form1Campo1 = $("#form1Campo1").val(),
        form1Campo2 = $("#form1Campo2").val();

    if (!form1Campo1 || !form1Campo2)
        return false;
    else
        return true;
}

JSFiddle : link

    
09.11.2016 / 14:26