Help with HTML form

Asked

Viewed 44 times

0

Sorry about the title of the post, but I’m not thinking of something to specify what I want (feel free to edit). It’s as follows:

I have an html form and a button:

<form id="formflor" method="post">

  <input type="text" class="form-control" id="codigo" name="codigo" placeholder="código de barras">
  <input type="text" class="form-control" id="nome" name="nome" placeholder="Rosa">
  <textarea class="form-control" id="informacoes" name="informacoes" rows="3" placeholder="descreva aqui..."></textarea>
  <input type="file" id="imagem" name="imagem">
  <button type="button" class="btn btn-primary" onclick="editaFlor(1, 'jasmim');">Enviar dados</button>
</form>

when clicked calls a function jquery:

function editaFlor(id, descricao){
  if (confirm("Confirma a alteração de " + descricao + "?"))
  {
    var myForm = document.getElementById('formflor');
    var form = new FormData(myForm);

    $.ajax({
      type: "POST",
      url: "functions/editarFlor.php",
      data: form,
      cache: false,
      contentType: false,
      processData: false,
      success: function(data) {
        if (data == 'ok'){
          alert(descricao + ' editado com sucesso!');
          listaFlor();
        }
        else{
          alert(data);
        }
      }
    });
  }
}

That one form works normally, and I need the following: include the parameter id on the form form and send it along with the form data.

Is there any way to do that?

  • Have you tried form.append('id', document.getElementById('inputID').value); ?

2 answers

2


You can add your id at the FormData

function editaFlor(id, descricao){
  if (confirm("Confirma a alteração de " + descricao + "?"))
  {
    var myForm = document.getElementById('formflor');
    var form = new FormData(myForm);
    form.append('id', id);
    //...resto do código

1

You can put another input into your form with the ID and hide this field. something like

<input type="hidden" name="id" value="2">

No value you play the ID you need.

  • unfortunately, I cannot change the form, it has to be on the button, because I will have several buttons for each item

  • 1

    Where is this ID information? You render the page with it or retrieve it with javascript?

  • 1

    You can then pass the id on querystring url: "functions/editarFlor.php?id=<seu_id>".

  • The page is created with a PHP script that already generates the Buttons with their id

  • 1

    @Leandroangelo gave a possible solution, pass the id through the url and recover this value. Just be careful to validate this value before using.

  • 1

    @Italorodrigo forgets, is a bad idea, only in the last alternative

Show 1 more comment

Browser other questions tagged

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