0

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

5
  • Use Ajax to get value from server end Commented Oct 23, 2018 at 11:45
  • you mean you want to read the contents of all the textboxes? Or something else? Please explain more clearly the situation and what your goal is Commented Oct 23, 2018 at 11:54
  • Is using jQuery an option? Commented Oct 23, 2018 at 12:01
  • my goal is to display all the values using a button click js Commented Oct 23, 2018 at 12:01
  • @Adder I'll try it Commented Oct 23, 2018 at 12:07

2 Answers 2

1

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);
})
Sign up to request clarification or add additional context in comments.

1 Comment

Welcome.i am happy to help.
0

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>";

1 Comment

It's correct but I'm trying to use javascript for getting the values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.