使用 php mysql jquery ajax 更新选择框的值

2024-01-21

我在改变方面面临重大问题status in <select>通过 jquery-ajax 标记到数据库。

注意:我在 stackoverflow 中搜索了这个问题。但这些答案与我的问题并不接近。这是下面的链接link1 https://stackoverflow.com/questions/34915126/how-to-update-select-boxes-with-php-mysql-jquery-ajax, link2 https://stackoverflow.com/questions/16753613/dynamically-updating-select-boxes-with-php-mysql-aja, link3 https://stackoverflow.com/questions/25894033/changing-the-value-of-a-select-box-with-jquery

When i click on first row select box the data is sent through ajax and the status column in table get updated and also it is updated in mysql database. But when is select 2nd, 3rd row it doesn't change not update the status column in html page nor in database here is the output image. output image

先感谢您..

Here is the details of my code database table name: ajaxselect Table image from database

HTML 文件:index.php

 <?php
    include('processing.php');
    $newobj = new processing();
?>
<html>
    <head>
        <title>Jquery Ajax select <tag> with PHP Mysql</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    </head>
    <body>
        <table border="1">
            <tr>
                <th>Id</th>
                <th>Product name</th>
                <th>Status</th>
                <th>Action</th>
            </tr>
            <?php echo $newobj->display();?>
        </table>
        <script>
            $(document).ready(function(){
                $("#selectstatus").change(function(){
                    var statusname = $("#selectstatus").val();                  
                    var getid = $("#getid").val();                  
                    $.ajax({
                        type:'POST',
                        url:'ajaxreceiver.php',
                        data:{statusname:statusname,getid
                        :getid},
                        success:function(result){
                            $("#display").html(result);
                        }
                    });
                });
            });
        </script>
    </body>
</html>

Ajax 文件:ajaxreceiver.php

 <?php
    include('processing.php');
    $newobj = new processing();

    if(isset($_POST['statusname'],$_POST['getid'])){
        $statusid = $_POST['statusname'];
        $id = $_POST['getid'];

        $newobj->getdata($statusid,$id);
    }
?>

PHP 类文件:processing.php

    <?php
    class processing{
        private $link;

        function __construct(){
            $this->link= new mysqli('localhost','root','','example');
            if(mysqli_connect_errno()){
                die ("connection failed".mysqli_connect_errno());
            }
        }

        function display(){
            $sql = $this->link->stmt_init();
            $id=1;
            if($sql->prepare("SELECT id,productname,status FROM ajaxselect")){
                $sql->bind_result($id,$productname,$status);
                if($sql->execute()){
                    while($sql->fetch()){   
            ?>
                        <tr>
                            <td><input type="hidden" id="getid" value="<?php echo $id;?>"><?php echo $id;?></td>
                            <td><?php echo $productname;?></td>
                            <td><p id="display"><?php echo $status;?></p></td>
                            <td>
                                <select id="selectstatus">
                                    <option>Pending</option>
                                    <option>Delivered</option>
                                    <option>Cancelled</option>
                                    <option>Amount Paid</option>    
                                </select>
                            </td>
                        </tr>
            <?php   
                    }
                }
            }
        }

        function getdata($statusid,$id){
            $sql = $this->link->stmt_init();
            if($sql->prepare("UPDATE ajaxselect SET status=? WHERE id=?")){
                $sql->bind_param('si',$statusid,$id);
                if($sql->execute()){
                    echo $statusid;
                }
                else
                {
                    echo "Update Failed";
                }
            }
        }
    }
?>

所有选择框都有相同的 id,所以$("#selectstatus").val()将始终返回相同的值。您应该获得更改元素的值。 JavaScript:this.value或 jQuery$(this).val()示例(请注意selectstatus是一个类,所以你应该添加一个新类): 超文本标记语言

<tr>
    <td><?php echo $productname;?></td>
    <td><p id="display"><?php echo $status;?></p></td>
     <td>
         <select status-id="<?php echo $id;?>" class="selectstatus" id="selectstatus">
            <option value="Pending">Pending</option>
            <option value="Delivered">Delivered</option>
            <option value="Cancelled">Cancelled</option>
            <option value="Amount paid">Amount Paid</option>    
         </select>
     </td>
   </tr>

JS

$(".selectstatus").change(function(){
     var statusname = $(this).val();                  
     var getid = $(this).attr("status-id");                  
                $.ajax({
                    type:'POST',
                    url:'ajaxreceiver.php',
                    data:{statusname:statusname,getid
                    :getid},
                    success:function(result){
                        $("#display").html(result);
                    }
                });
            });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 php mysql jquery ajax 更新选择框的值 的相关文章

随机推荐