Submit form with buttons outside it

Asked

Viewed 302 times

-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 button clicked, the variable $pasta takes proper value correctly.

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

In addition it has two buttons that will, in a given 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 the modal the variable $pasta takes on the value usuario/ of condition else PHP, that is, no isset

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

  • A possibility would be instead of button in the modal, to <label for="id_do_botao> - When you click on a label for, the button is triggered (it is a kind of "remote control"). Ai would be the case to style the label to look like button (I’m presenting as quick solution, obviously someone can post something more elaborate with JS and real button)

  • As I recall, PHP will only be able to do this comparison on type buttons submit. That is, by clicking on gravar2 he won’t be able to recover.

  • @Everson the clicked <button> also sends its value. (Just what was clicked, practically like Submit).

  • 1

    For those who only know how to vote negative will a publication of dvd pra vcs https://chat.stackexchange.com/rooms/75640/votos-negativos-sem-explicacao Rethink and try to be more helpful!

  • It’s crossed my mind now to just put a form in the modal and end of conversation!

3 answers

3

Tip: both demos use the excellent debug tool "httpbin" to show the results

With JS:

A simple solution is to link 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>
<form method="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>


Label:

This one doesn’t 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>
<form method="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>

  • I liked your reply

  • but when you click an ai button on the run it appears until the user’s ip

  • @Leocaracciolo the page that appears is the destination of the form, just for you to see what is being sent. It is not the answer code that shows this, but the page that is receiving the form. In your case, the action= will be for your original code. It’s the same logic that the DVD site colleague posted after me and was accepted (as already predicted), only much simpler and objective.

  • On the negative, it is a disservice to other users. For my reputation, it doesn’t change anything, but one day the little club of exchanged votes will fall (or tire, because as the birth captain would say, "they will never be")

  • @DVD For starters, I didn’t think the negative was yours. Second time you’ve been accusing me, coincidentally after I took negative. Weird right.

  • I know, the action points to https://httpbin.org/post, is that I did not know this link and I was surprised.

  • And people continue with the -1 spree with two functional demos, rest assured.

Show 2 more comments

1

Values of Submit buttons are not being sent $jQuery.(). Then you can pass these values by post with Hidden input, and not by 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();
});

In validating:

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

-1


Instead of making a submit, you can make a click on the form button. You will have the same effect of directly clicking on the respective button.

You can simplify by placing the two selectors in the same function by taking the value button clicked, which is the same as the value 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'>       |
                                             ↑___________|
  • 1

    Ball show, no work, was just copy and paste. Perfect!!!! With 3 lines only, hehehehe.

  • 1

    'Cause I didn’t think to do it like that before you answered? :)

  • sometimes it turns even :D

  • hahaha, I wouldn’t get that script ever, missing some basics!!!

  • Mt locco. You’re the same guy..

  • I wanted to understand the negative for this answer and also for @Bacco. And also for my question. I think there is a lot of mané on this site. After all we are here working for free for the gringo site helping a lot of people.

  • 1

    It’s the meta gang, if someone there says that farting in the code is performatic, sure many ups. This is the land of brasucas!!

Show 2 more comments

Browser other questions tagged

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