harley pinkman
harley pinkman

Reputation: 3

Get the values of while loop using button with js

How can I get the values in the while loop using button with js?

<?php
    while ($row = mysqli_fetch_array($query))
    {
      echo '<input type="text" name="sample" value="'.$row['sample'].'">';

    } 
?>

Thanks

Upvotes: 0

Views: 63

Answers (2)

Bhanu Sengar
Bhanu Sengar

Reputation: 382

You can get the value of textbox when you click on a button.

<input type="text" name="sample[]" value="abc" class="valueInput">
<input type="text" name="sample[]" value="xyz" class="valueInput">
<input type="text" name="sample[]" value="pqr" class="valueInput">
<input type="button" class="getValue" value="Get Value">

Note: I have set the static input box you can make it dynamic but make sure please add the class as valueInput.

Jquery Code.

$(document).on('click','.getValue',function(){
var valArr = [];
$(".valueInput").each(function(){
    valArr.push($(this).val());
});
console.log(valArr);
})

Upvotes: 1

Adder
Adder

Reputation: 5868

The name of the input field should indicate an array with [].

<form action="action.php" method="POST">
<?php
    while ($row = mysqli_fetch_array($query)){
       echo '<input type="text" name="sample[]" value="'.$row['sample'].'">';
    } 
?><input type="submit">
</form>

action.php: (Processes the data on submit)

<?php
      echo "<pre>";
      print_r($_POST['sample']);
      echo "</pre>";

Upvotes: 0

Related Questions