Submit form with buttons outside of it

-2

I have the following form

<form method='post' name='checkbox_form' id='checkbox_form' onsubmit='return validate()' action=''>
..............
..............
<input name='salvar' type='submit' value='Salvar'>
<input name='gravar' type='submit' value='Gravar'>
</form>

and in PHP, depending on the clicked button, the $pasta variable takes its due value correctly.

    if (isset($_POST["gravar"])||isset($_POST["gravar2"])){
        $pasta="favoritas/";
    }else{
        $pasta="usuario/";
    }

In addition there are two buttons that will, in a certain situation, appear within a modal

<div style=\"display:none\">
    <div id=\"ajax2\" class=\"ajax2\">
    <input name='salvar2' type='button' id='salvar' value='Salvar'>
    <input name='gravar2' type='button' id='gravar' value='Gravar'>
    </div>
</div>

To submit the form with the modal buttons I used the following script:

$("#salvar").click(function() {
    $("#checkbox_form").submit();
});
$("#gravar").click(function() {
    $("#checkbox_form").submit();
});

The problem: Whatever the button clicked on modal, the $pasta variable takes the value usuario/ of the else condition of PHP, ie, isset p>

The question : How can I do the conditional execution in PHP correctly by clicking the modal buttons?

    
asked by anonymous 06.04.2018 / 22:00

3 answers

0

Instead of making a submit , you can do a click on the form button. It will have the same effect as clicking the button directly.

You can simplify by putting the two selectors on the same function, taking the value of the clicked button, which is the same as the value of the button:

$("#salvar, #gravar").click(function() {
    $("#checkbox_form").find("input[value='"+ $(this).val() +"']").click();
});
Botões da modal:
                                                         ________
                                                         ↓      |
<input name='salvar2' type='button' id='salvar' value='Salvar'> |
<input name='gravar2' type='button' id='gravar' value='Gravar'> |
                                                         ↑      |
Botões do form:                              ____________|______|
                                             ↓           |
<input name='salvar' type='submit' value='Salvar'>       |
<input name='gravar' type='submit' value='Gravar'>       |
                                             ↑___________|
    
06.04.2018 / 22:29
2

The values of the submit buttons are not being sent $ jQuery.submit () . Then you can pass these values per post with hidden input , not the name button.

Ex:

<div style=\"display:none\">
    <div id=\"ajax2\" class=\"ajax2\">
    <input name='salvar2' type='button' id='salvar' value='Salvar'>
    <input name='gravar2' type='button' id='gravar' value='Gravar'>

    <input name='acao' type='hidden' id='acao' value=''>
    </div>
</div>

And in JavaScript:

$("#salvar").click(function() {
    $('#acao').val('salvar');
    $("#checkbox_form").submit();
});
$("#gravar").click(function() {
    $('#acao').val('gravar');
    $("#checkbox_form").submit();
});

Validation:

if (isset($_POST["acao"]) {
    if ($_POST["acao"] == 'gravar'){
        $pasta="favoritas/";
    }else{
        $pasta="usuario/";
    }
}
    
06.04.2018 / 22:26
2

Tip: Both demos use the excellent debugging tool " link " to show results

With JS:

A simple solution is to bind the action to click() and not to submit:

$("#tres").click(function() {
  $("#um").click();
  return false;
});

$("#quatro").click(function() {
  $("#dois").click();
  return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><formmethod="post" action="https://httpbin.org/post">
  <input id="um"   name='acao' type='submit' value='submit um'>
  <input id="dois" name='acao' type='submit' value='submit dois'>
</form>

<button id='tres'  >Remoto do Um</button>
<button id='quatro'>Remoto do Dois</button>


With Label:

This does not even need JS (but a CSS goes well to make it look like a button):

.button {
display:block;
background:#ccc;
margin:4px;
width:150px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><formmethod="post" action="https://httpbin.org/post">
  <input id="um"   name='acao' type='submit' value='submit um'>
  <input id="dois" name='acao' type='submit' value='submit dois'>
</form>

<label class="button" for='um'  >Remoto do Um  </label>
<label class="button" for='dois'>Remoto do Dois</label>
    
06.04.2018 / 22:25