 <p>You're problem is that you are trying to pass data in STRING FORMAT. In your script you pass the $row out of while loop so it doesn't pass anything useful for you. If you have more than one button you can't use ID ATTRIBUTE but you have to use CLASS. Set a data-id attribute so you can pass the value from the database and set an ID to the input so you can take its value also. Try this:</p> <pre><code>&lt;?php if(mysqli_num_rows($result)){ while($row = mysqli_fetch_assoc($result)){ print "&lt;div class='item col-xs`-4 col-lg-4'&gt;". "&lt;div class='row'&gt;". "&lt;div class='col-xs-10'&gt;". "&lt;p class='list-group-item-text'&gt;". "&lt;input type='text' class='form-control' id='input-" . $row["id"] . "'&gt;". "&lt;button data-id='". $row["id"] . "' class='mybutton btn btn-success'&gt;Calculate&lt;/button&gt;". "&lt;/div&gt;". "&lt;/div&gt;". "&lt;/div&gt;"; } } ?&gt; &lt;script type="text/javascript"&gt; $(document).ready(function(){ $(".mybutton").on("click", function(){ var myid = $(this).attr('data-id'); var myinput = $('#input-'+myid).val(); alert("MY ID IS: " + myid); alert("MY INPUT VALUE IS: " + myinput); }); }); &lt;/script&gt; </code></pre>