Pass innerHTML inputs to form and access via PHP

0

I have an HTML form, normal input fields, submit button ... Through a JS function, I add additional input fields to a DIV (with innerHTML).

var div = document.getElementById('produtos-adicionados');
div.innerHTML = '<input type='text' class='form-control' name='item' id='item' required='' />';

When submitting the form, I can not access this new item item input, obviously.

How would I be able to submit the form and get the input value in php? In addition, when sending the form, you can also get the values of the other inputs, which are in the form but are not in innerHTML.

    
asked by anonymous 26.07.2018 / 20:41

1 answer

-1

Please note:

  • Using quotation marks around the html
  • required attribute does not need to assign value
  • Update:

    Using jQuery to show that the added input is part of the form.

    var div = document.getElementById('produtos-adicionados');
    div.innerHTML = "Apelido: <input type='text' class='form-control' name='item' id='item' required />"
    
    $('#myform').submit(function(e) {
      e.preventDefault()
      alert(JSON.stringify(($('#myform').serializeArray())))
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><formid="myform" action="/action_page.php">
      Nome: <input type="text" name="nome" required>
      <div id="produtos-adicionados"> substituir conteúdo</div>
    
      <input type="submit">
    </form>
        
    26.07.2018 / 20:47