Change the value of a parameter passed to a function

Asked

Viewed 622 times

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?

  • 1

    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

  • 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.

  • 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

  • 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.

2 answers

1

You can use an Hidden field to save the value of your counter, manipulate with javascript and send to the backend with ajax or post.

<?php 
  $contador = '';
  foreach ($programas as $key => $formu)
  {
      $contador += 1;
  }
?>

<input id="contador" name="contador" type="hidden" value="<?php echo $contador; ?>" />

<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>

Javascript:

function criarCampo() {
     var contador = $('#contador').val();

     //incrementa o contador
     +contador++;

     //TODO: Cria o campo com o contador incrementado "campos_"+contador

     //Atualiza o contador no campo hidden

     $('#contador').val(contador);
}

1

If you need to simply get the id (counter) of each element in the function remover(), can do so:

<br><div class="btn btn-primary"  id="rmv_<?php echo $contador; ?>"   onclick="remover(this)" data-contador="<?php echo $contador; ?>"><span class="fa fa-minus"></span></div>

And in function remover():

function remover() {
   var contador = $(this).data('contador');

   //faça o que tiver que fazer com o contador
}

Browser other questions tagged

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