0

This is my code example:

<select name="codethings"id="products">
<option value="0">--choose--</option>
<?php 
   include 'config.php';
   $sql="select * from thing where status='S01'";
   $call=mysqli_query($connect,$sql);
while($data=mysqli_fetch_array($call)){ ?>
<option value="<?php echo $data['codethings']; ?>" data-price="<?php echo ['price']; 
? >" ><?php echo $data['codethings'];?>-<?php echo $data['namet'];?>(<?php echo $data['price']; ?>)</option>
    <?php } ?>
</select>
<br>

Price:

<input type="text" name="price" id="priceInput" value="" disabled="disabled"><br>

Script

<script>
$(function () {
    $('#products').change(function () {
        $('#priceInput').val($('#products option:selected').attr('data-price'));
    });
});
</script>

The code i reference (Automatically show price when we select the ID in PHP)

It is not work after i select. How i want to make it work to make the price display automatically after i select.

  • I don't see anything very wrong. do some debugging? does the html that's generated actually have values in data-price? put console.log() calls in your onchange function to see if it is actually called, see if it is finding the selected option, see if it is finding #priceInput – ysth Sep 16 '20 at 16:51
  • 1
    Note: The [object-oriented interface to `mysqli`](https://www.php.net/manual/en/mysqli.quickstart.connections.php) is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface where missing a single `i` can cause trouble. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era and should not be used in new code. Additionally the procedural interface has less rigorous error checking and reporting, frustrating debugging efforts. – tadman Sep 16 '20 at 17:01
  • It is because php is my first programming language, so i am hard to understand. I learn php myself. – I am newbie Sep 16 '20 at 17:11
  • Are you sure you're loading jQuery? `` – Bossman Sep 16 '20 at 17:27

1 Answers1

0

$(function () {
        $('#products').change(function () {
            $('#priceInput').val($('#products option:selected').attr('data-price'));
        });
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="productID" id="products">
        <option value="" >-Select-</option>
        <option value="0" data-price="100">First product</option>
        <option value="1" data-price="200">Second product</option>
    </select><br/>
    Price :<input type="text" name="Price" value="" id="priceInput" disabled="disabled"/></br>
Mit Patel
  • 1
  • 1
  • try to use this code , and in select fetch data from database . – Mit Patel Sep 25 '20 at 06:13
  • Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. – DCCoder Sep 25 '20 at 06:49