简体   繁体   English

如何使用 php、jquery 和 ajax 在数据库中插入值

[英]How can i insert the value in database using php, jquery and ajax


I am struggling very hard to get this to work and I don't know what I'm doing wrong.我正在努力让它发挥作用,我不知道我做错了什么。 I am new into this so be gentle, I have the index that let the user pick item and show the total price.我是新手,所以要温柔,我有让用户选择商品并显示总价的索引。 I want to insert the totalprice into my database with jQuery and AJAX.我想用 jQuery 和 AJAX 将总价格插入我的数据库。 but when I tried to submit it to it say Undefined index: total但是当我尝试将它提交给它时说 Undefined index: total


index.php index.php

    <thead>
        <tr>
            <th>Component</th>
            <th>Item Name</th>
            <th>Price </th>
        </tr>
    </thead>

    <tbody>
        <tr>
            <td>CPU</td>
            <td>
                <?php
                //Retrieving CPU table
                $query = $conn->query("SELECT * FROM cpu");
                echo '<select name="cpu" class="cpu"  onChange = $("#cpuprice").val($(this).find("option:selected").attr("cpuprice"))>';
                echo '<option></option>';
                while ($obj = mysqli_fetch_assoc($query)) {
                    echo '<option cpuprice = ' . $obj['price'] . ' cpuname=' . $obj['cpuname'] . ' >' . $obj['cpuname'] . '</option> /n';
                }
                echo '</select>';
                ?>
            </td>
            <td>
                <output id="cpuprice" disabled value="">
            </td>
        </tr>
    </tbody>

    <tbody>
        <tr>
            <td>GPU</td>
            <td>
                <?php
                //Retrieving GPU table
                $query = $conn->query("SELECT * FROM gpu");
                echo '<select name="gpu" class ="gpu"  onChange = $("#gpuprice").val($(this).find("option:selected").attr("gpuprice"))>';
                echo '<option></option>';
                while ($obj = mysqli_fetch_assoc($query)) {
                    echo '<option  gpuprice = "' . $obj['price'] . '" gpuname = "' . $obj['gpuname'] . '">' . $obj['gpuname'] . '</option>';
                }
                echo '</select>';
                ?>
            </td>
            <td>
                <output class="form-control prc" id="gpuprice" disabled value="">
            </td>
        </tr>
    </tbody>

    <tbody>
        <tr>
            <td>
            </td>
            <td>

            </td>
            <td>
                <span class="totalprice" name="total">Total: </span>
                <script>
                    $('select').change(function() {
                        //get value from cpu slect box check if attr there else take value 0
                        var cpu_price = $(".cpu").find('option:selected').attr('cpuprice') ? $(".cpu").find('option:selected').attr('cpuprice') : 0
                        $('#cpuprice').val(cpu_price)
                        
                        //get value from gpu slect box check if attr there else take value 0
                        var gpu_price = $(".gpu").find('option:selected').attr('gpuprice') ? $(".gpu").find('option:selected').attr('gpuprice') : 0
                        $('#gpuprice').val(gpu_price)
                        var totalp
                        var total = parseInt(cpu_price) + parseInt(gpu_price);
                        $('.totalprice').text('₱' + total);
                    })
                </script>
            </td>
        </tr>
    </tbody>
    <script>
        $("#totalp").on("submit", function(event) {
            event.preventDefault();
            $.ajax({
                type: "GET",
                url: "insert.php",
                data: {
                    'total':total
                },
                dataType: "json",
                success: function(data) {

                },
            });
        });
    </script>
</table>

<input class="submit" type="submit" />

insert.php插入.php

    $total= $_GET['total'];

echo $total;
$conn = mysqli_connect("localhost", "root", "", "pcpart");
// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sqtr = "INSERT INTO trycombuild(totalprice) VALUE ('$total')";
mysqli_query($conn,$qstr);
mysqli_close($conn);
echo "successful";

you can set variable total in tab ' input type ="hidden" id="total " ' after in jquery insert to ajax, you can get value total in tab input and send ajax.在 jquery 插入 ajax 后,您可以在 tab ' input type ="hidden" id="total " ' 中设置变量总计,您可以在 tab 输入中获取 value total 并发送 Z2705A83A5A0659EDAZ343458。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM