owebindex owebindex - 2 days ago 5
PHP Question

Using Ajax to determine different results in PHP

HTML CODE

<div class="card text-center pricing-card mb-20">
<ul class="list-group list-group-flush">
<?php
$gbewas = mysqli_query($con,"SELECT * FROM price");
while($okks = mysqli_fetch_array($gbewas))
{ ?>
<li class="list-group-item d-block text-left">
<input type="radio" name="dep" id="btn-login" value="10">
<span style="font-size:16px; margin-left:10px"><?=$okks['plan']?></span>
</li>
<?php } ?>
</ul>
</div>
<!--- SHOW RESULT--->
<input type="text" class="error" id="logerror">


jQuery:

$(document).ready(function() {
$("#btn-login").click(function() {
var price = $("input#btn-login").val();
$('#logerror').input(price);
});
});


I need solution to this code.
First issue, I have 4 rows in price dbTable and it fetch out the 4 rows correctly, but if I click on each/different radio buttons it always show the first input value as the result.

Second Issue, The result is not showing in input format

<input type="text" class="error" id="logerror">


But show in div format

<div class="error" id="logerror"></div>

Answer

Two problems - your ids are all the same, so jQuery will pick the first #btn-login each time. But you're using the same value (10) for each input anyway, so what's the difference?

Assuming your database has a [price] column, let's use that as the value; and let's get the price of just the element that was clicked.

while($okks = mysqli_fetch_array($gbewas))
{    
  ?>
  <li class="list-group-item d-block text-left">
    <input type="radio" name="dep" class="btn-login" value="<?= $okks['price'] ?>">
    <span style="font-size:16px; margin-left:10px"><?=$okks['plan']?></span>
  </li>
  <?php 
}

Note that we're using class instead of id, since id must be unique. We'll do so in our JavaScript, as well:

$(document).ready(function() {
  $(".btn-login").click(function() {
    var price = $(this).val();   // the clicked element
    $('#logerror').val(price);   // val(), not input()
  });
});