Home > Blockchain >  Using variables in PHP input radio name tag and get value on JQUERY or JAVASCRIPT
Using variables in PHP input radio name tag and get value on JQUERY or JAVASCRIPT

Time:11-03

I have a PHP file that creates radio buttons based on pets ids. The var $idperro is a number that changes dynamically to identify the set of radio buttons independently. I am trying to put the value inside the p tag.

Heres the PHP

<?php

echo '
    Jack Baño: <input type="radio" name="'.$idperro.'/servicio" value="'.$idperro.'/baño/'.$baño.'">
    Jack Corte: <input type="radio" name="'.$idperro.'/servicio" value="'.$idperro.'/corte/'.$corte.'">

    Pepe Baño: <input type="radio" name="'.$idperro.'/servicio" value="'.$idperro.'/baño/'.$baño.'">
    Pepe Corte: <input type="radio" name="'.$idperro.'/servicio" value="'.$idperro.'/corte/'.$corte.'">
    
    <p id="precio"></p>
';
?>

Heres the HTML output for that:

Jack baño: <input type="radio" name="8/servicio" value="8/baño/260.00">
Jack Corte: <input type="radio" name="8/servicio" value="8/corte/300.00">
Pepe baño: <input type="radio" name="17/servicio" value="17/baño/260.00">
Pepe Corte: <input type="radio" name="17/servicio" value="17/corte/300.00">
<p id="precio"></p>

I am trying to get the value of the selected radio button, but I cant set the name in Jquery in this fashion. (consider the var $idperro the dynamic var below. I know thats not the correct format for jquery and ive tried many things)

$("input[name=$idperro/servicio]").filter(":checked").val();

Is there any other way on which i can set a javascript function or jquery to get it.

CodePudding user response:

You could try something like this.

Add a class yourClassName for easy targeting with jQuery:

Jack baño: <input class="yourClassName" type="radio" name="8/servicio" value="8/baño/260.00">
Jack Corte: <input class="yourClassName" type="radio" name="8/servicio" value="8/corte/300.00">
Pepe baño: <input class="yourClassName" type="radio" name="17/servicio" value="17/baño/260.00">
Pepe Corte: <input class="yourClassName" type="radio" name="17/servicio" value="17/corte/300.00">
<p id="precio"></p>

Then you could try:

$("input.yourClassName:checked").val();
  • Related