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


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" 

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

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;

asked by Armando Bolaños 17.08.2017 в 22:04

2 answers


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.


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

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="//"></script>

        $("#boton01").click(function () 

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


answered by 17.08.2017 в 22:28