Add Input with Jquery and how to get the data from this input

Asked

Viewed 392 times

1

I have 1 JS button that creates input. I just can’t get this input data. I can create every input with a different name, but I have no idea how can I get these values from php. I tried to send the var add to make a loop, but she can’t catch the final value.

Here is the JSFIDDLE working the button and the different name.

1 answer

0


Friend, the most correct way to do PHP would be by using the input value as a Array.

I mean. Instead of doing this:

var add = 1;
function add_fields() {
    var d = document.getElementById("content");
    add++;
    d.innerHTML += "<br /><label class='tooltips'>A ser excluído "+add+": <input type='text' name='solicitacao"+add+"'/></label>";
}

You should do this:

var add = 1;
function add_fields() {
    var d = document.getElementById("content");
    add++;
    d.innerHTML += "<br /><label class='tooltips'>A ser excluído "+add+": <input type='text' name='solicitacao[]'/></label>";
}

Simply put: we replace the "solicitacao" + add for "solicitacao[]".

By doing this, PHP will access the value of solicitacao[] through $_POST['solicitacao'], and this will return a Array with all data entered.

Thus, such an operation becomes more dynamic, dispensing with the use of counters to do so.

Remembering that one should declare these inputs within the form (<form>), in order to submit the values.

  • Good, vlw. The code is right. That was just a clipping.

Browser other questions tagged

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