我如何从php的while循环中使用的html标签中获取单个文本值来显示数据库中的数据?

时间:2022-11-27 18:32:21
<script type="text/javascript">
$(document).ready(function() {
    $(document).on('change','.uptprice',function(){
        var quantity = $(this).text();
        var vege = $('.itemText').text();
        $.ajax({ type: 'POST',
                 url: "price.php",
                 data: "{'quant':'"+quantity+"', 'vegtle':'"+vege+"'}",
                 success: function(result) {
                    $('.price').text(result);                   
                 },
                 error: function(error) {
                    console.log(error);
                 }
        });
    });
});

this is the js code used to fetch a text from p tag.

这是用于从p标签中获取文本的js代码。

var quantity = $(this).text();
var vege = $('.itemText').text();

but it is not fetching a single value, it is looping through all the value it has in the database. I checked using:

但它没有获取单个值,而是循环遍历数据库中的所有值。我查了一下:

console.log(quantity);
console.log(vege)

html code:

HTML代码:

    <div class="container">
        <div class="row">
            <div class="col-lg-3" >
                <?php
                if($vegetable<>$row["vegetable"])
                {
                ?>
                <p class="itemText"><?php echo $row["vegetable"]; ?></p>
            </div> 
            <div class="col-lg-3" >
                <p class="price"><?php echo $row["price"]; ?></p>
            </div> 
            <div class="col-lg-3" >
                <select class="uptprice">
                    <?php
                        $sql1 = "SELECT quantity FROM vegetables WHERE vegetable = '".$row["vegetable"]."'";
                        $result1 = $conn->query($sql1);
                        if ($result1->num_rows > 0) {
                            while($row1 = $result1->fetch_assoc()) {
                    ?>
                    <option ><?php echo $row1["quantity"]; } } ?></option>
                </select>
            </div>
            <div class="col-lg-3">
                <button class="add">ADD</button>
            </div>
            <div class="cart"></div>
        </div>
    </div>

here dropdown is also populated by database. my aim is, whenever user selects a option from dropdown, code should fetch that changed value and pass through the ajax request so that i can process that selected option in php to get desired result. But instead it is looping through the while loop i have used in html code to display data from database.

这里的下拉列表也由数据库填充。我的目标是,每当用户从下拉列表中选择一个选项时,代码应该获取该更改的值并通过ajax请求,以便我可以在php中处理所选的选项以获得所需的结果。但它反过来循环我在html代码中使用的while循环来显示数据库中的数据。

1 个解决方案

#1


-1  

Hola debes realizar el siguiente cambio en tu código Html:

Hola debes realizar el siguiente cambio entucódigoHtml:

            <select name="select_ejemplo" id="select_ejemplo">
                <?php
                    $sql1 = "SELECT quantity FROM vegetables WHERE vegetable = '".$row["vegetable"]."'";
                    $result1 = $conn->query($sql1);
                    if ($result1->num_rows > 0) {
                        while($row1 = $result1->fetch_assoc()) {
                ?>
                <option id="uptprice" value="<?php echo $row1['quantity'] ; ?>"><?php echo $row1["quantity"]; } } ?></option>
            </select>

cambios en el price.php:

cambios en el price.php:

la variable que se envía por el metodo post es vegetle

la variable queseenvíaporel metodo post es vegetle

$vegle = $_POST["vegtle"]; (malo)

$ vegle = $ _POST [“vegtle”]; (马洛)

debe cambiar a

debe cambiar a

$vegle = $_POST["vegetle"];(bien)

$ vegle = $ _POST [“vegetle”];(bien)

cambios en el jquery:

cambios en el jquery:

<script type="text/javascript"> $(document).ready(function() { $("select[name=select_ejemplo]").change(function(){ var quantity = $('select[name=select_ejemplo]').val(); var vege = $('.itemText').text(); $.ajax({ type: 'POST', url: "price.php", dataType:"text", data:{ quant: quantity,vegetle: vege}, success: function(result) { $('.price').text(result);
}, error: function(error) { console.log(error); } }); }); });

#1


-1  

Hola debes realizar el siguiente cambio en tu código Html:

Hola debes realizar el siguiente cambio entucódigoHtml:

            <select name="select_ejemplo" id="select_ejemplo">
                <?php
                    $sql1 = "SELECT quantity FROM vegetables WHERE vegetable = '".$row["vegetable"]."'";
                    $result1 = $conn->query($sql1);
                    if ($result1->num_rows > 0) {
                        while($row1 = $result1->fetch_assoc()) {
                ?>
                <option id="uptprice" value="<?php echo $row1['quantity'] ; ?>"><?php echo $row1["quantity"]; } } ?></option>
            </select>

cambios en el price.php:

cambios en el price.php:

la variable que se envía por el metodo post es vegetle

la variable queseenvíaporel metodo post es vegetle

$vegle = $_POST["vegtle"]; (malo)

$ vegle = $ _POST [“vegtle”]; (马洛)

debe cambiar a

debe cambiar a

$vegle = $_POST["vegetle"];(bien)

$ vegle = $ _POST [“vegetle”];(bien)

cambios en el jquery:

cambios en el jquery:

<script type="text/javascript"> $(document).ready(function() { $("select[name=select_ejemplo]").change(function(){ var quantity = $('select[name=select_ejemplo]').val(); var vege = $('.itemText').text(); $.ajax({ type: 'POST', url: "price.php", dataType:"text", data:{ quant: quantity,vegetle: vege}, success: function(result) { $('.price').text(result);
}, error: function(error) { console.log(error); } }); }); });