Can the value of a PHP variable be set as id?

2

My problem is as follows: I assigned an id to my button that contains the value of a PHP query. Here the code:

<input type="text" name="Articulo" id="Articulo"  value="1"/>

<input type="number" name="quanti"  id="quanti"  class="form-control" 
      value="1"/>

<!-- aqui el boton le asigno como id un valor de PHP-->
<!-- este valor lo obtengo de la base de datos -->

<button type="submit" name="add_to_cart" id="<?php echo $row['idArticulo']; ?>" 
      class="btn btn-warning add_to_cart" >Añadir al carrito
</button>

The problem is how to get the value of that id and call it in javascript:

<script type="text/javascript">
    //***
    function validarInput() {
        //no sé cómo obtener ese valor del id y colocarlo
        document.getElementById('aquí el problema').disabled = !document.getElementById("quanti").value.length;
    }

</script>
    
asked by Armando Bolaños 17.08.2017 в 22:04
source

2 answers

2

To the button you can assign an event onclick that calls the function and pass it the reserved word this that refers to the button itself. Once you have recovered the button to which you have clicked, you can obtain your id.

Also, in this way, you will not need to recover the id of the button either, since by means of the reserved word this you can disable the button directly since it refers to the element that you have just clicked.

Example:

function hazClick(boton){
  console.log(boton.id);
  boton.disabled = true;
}
<button id="idBoton" onclick="hazClick(this)">Haz click</button>
    
answered by 17.08.2017 / 22:10
source
0

Try it this way and let me know if you get an alert with the id of the variable that comes from the php

<input type="text" name="variable" id="variable" value="<?php echo $row['idArticulo']; ?>">

<input type="button" name="boton01" id="boton01" value="Sacar el valor del input text">

<script type='text/javascript' src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>

    $(document).ready(function()
    {
        $("#boton01").click(function () 
        {
             alert($("#variable").val());
        });
    });
</script>

Alli should work with the variable that comes from the php if your connection is good, you let me know anything

Greetings

    
answered by 17.08.2017 в 22:28