2
I have a dynamic form that I add several fields and create together a div that passes by button that is assigned an event onClick
for passing a function that receives a parameter. This parameter is a sequential number that I create through a foreach
with a array of BD. But when I click on add one more field, this function parameter remains the same, not incrementing. I wish I could increment the value of the parameter, but I don’t know how to do this via Jquey.
<?php
$contador = '';
foreach ($programas as $key => $formu)
{
$contador += 1;
?>
<div id="campos_<?php echo $contador; ?>" name="campos[]" class="campos col-md-12" >
<br><div class="btn btn-primary" id="rmv_<?php echo $contador; ?>" onclick="remover('campos_<?php echo $contador; ?>')"><span class="fa fa-minus"></span></div>
</div>
notice that I want to increment this:
onclick="remover('campos_<?php echo $contador; ?>')"
I can increase the others id counters through jquery like this:
campos.children().find("#rmv_"+(contador)).attr('id','rmv_'+(comentario+1));
but I have no idea how to change this parameter value.
Someone who’s been there Can help me?
The ideal in your case is to work two separate ways, since php runs on the back (server) and jquery on the front (client). Then you should make a proposal to only at the time of update vc send all fields with your counters or each time you add the field you make an ajax call to the server to return you this incremented field
– Tafarel Chicotti
Yes, doing an ajax is a good thing, because the $counter is only incremented when it goes through the foreach. But is there no way to hold the event value via jquery? Because if there were I could replace it with a counter made via jquery. It seems to be simpler, but of course be have like.
– Estácio Di Fabio
Do you want to hold the php counter in jquery? there are 2 ways... since q vc mixes php and html/javascript on the same page, you can make a variable in the javascript that stores the value, or write in a Hidden field that value and read it in javascript
– Tafarel Chicotti
I wanted to actually check the last counter value that in this case could be the value of the last field, I don’t know, by checking this via jquery and adding +1, then I could take this value and play to the event parameter. My worst problem is to play the value for the event parameter.
– Estácio Di Fabio