bootstrap editable - 如何将下拉值传递给SQL表?

时间:2016-02-22 16:37:21

标签: php sql ajax twitter-bootstrap x-editable

我目前正在开发一个项目,该项目使用可编辑的bootstrap显示来自SQL表的数据进行实时编辑。

它工作正常 - 更改传输到SQL表。什么工作?:

  1. 显示SQL表的当前值
  2. 提供选择下拉列表
  3. 将更改的值传输到SQL表
  4. - >但是第3部分(传输更改的值)仅适用于自由文本输入(类xedit)。

    我要找的是以下代码:将下拉列表的选定值转移到SQL表

    以下是HTML代码:

    
    
       <?php
        include("connect.php");
        ?>
        <!DOCTYPE html>
        <html>
        <head>
            <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
            <meta charset="utf-8">
            <link href="assets/css/bootstrap.min.css" rel="stylesheet"     type="text/css">
            <link href="assets/css/custom.css" rel="stylesheet" type="text/css">
        </head>
        <body>
    
            <div class="container">    
        		<div style="text-align:center;width:100%;font-size:24px;margin-bottom:20px;color: #2875BB;">EDIT YOUR CHARACTERS</div>
                        <div class="row">
                            <table class= "table table-striped table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <th colspan="1" rowspan="1" style="width: 180px;" tabindex="0">NAME</th>
                                        <th colspan="1" rowspan="1" style="width: 220px;" tabindex="0">ROLE</th>
                                        <th colspan="1" rowspan="1" style="width: 288px;" tabindex="0">SECOND ROLE</th>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php
        						$query = mysql_query("SELECT * FROM characters");
        						$i=0;
        						while($fetch = mysql_fetch_array($query))
        						{
        							if($i%2==0) $class = 'even'; else $class = 'odd';
        							
        							echo'<tr class="'.$class.'">
    
                                        <td class="xedit" id="'.$fetch['id'].'" key="name">'.$fetch['name'].'</td>
        								<td class="xedit" id="'.$fetch['id'].'" key="role">'.$fetch['role'].'</td>
        								<td class="xedit2" id="'.$fetch['id'].'" key="secondrole"><a href="#" class="rolestatus" data-type="select" data-pk="3" data-url="" data-title="Select status">'.$fetch['secondrole'].'	</a></td>
                                        </td>
        					            
                                    </tr>';	
                                }
        						?>
                                </tbody>
                            </table>
                </div>
                </div>              
        		<script src="assets/js/jquery.min.js"></script> 
        		<script src="assets/js/bootstrap.min.js"></script>
                <script src="assets/js/bootstrap-editable.js" type="text/javascript"></script> 
                
                
        <script>
        $(function(){
            $('.rolestatus').editable({    
                source: [
                      {value: 1, text: 'DD'},
                      {value: 2, text: 'HEAL'},
                      {value: 3, text: 'TANK'}
                   ]
            }); 
        });
        </script>
                
                
        <script type="text/javascript">
        jQuery(document).ready(function() {  
                $.fn.editable.defaults.mode = 'popup';
                $('.xedit').editable();		
        		$(document).on('click','.editable-submit',function(){
        		var key = $(this).closest('.editable-container').prev().attr('key');
        var x = $(this).closest('.editable-container').prev().attr('id');
        var y = $('.input-sm').val();
        var z = $(this).closest('.editable-container').prev().text(y);
    
        			$.ajax({
        				url: "process.php?id="+x+"&data="+y+'&key='+key,
        				type: 'GET',
        				success: function(s){
        					if(s == 'status'){
        					$(z).html(y);}
        					if(s == 'error') {
        					alert('Error Processing your Request!');}
        				},
        				error: function(e){
        					alert('Error Processing your Request!!');
        				}
        			});
        		});
        });
        </script>
    
            </div>
        </body>
        </html>
    &#13;
    &#13;
    &#13;

    这是process.php代码

    &#13;
    &#13;
              <?php
              include("connect.php");
              if($_GET['id'] and $_GET['data'])
              {
    	      $id = $_GET['id'];
              $data = $_GET['data'];
              $key = $_GET['key'];
              if(mysql_query("update characters set $key='$data' where id='$id'"))
    	      echo 'success';
              }
              ?>
    &#13;
    &#13;
    &#13;

    那么有人知道我如何将所选的下拉值(class - &gt; xedit2)传输到SQL表吗?

    希望你能帮忙!

1 个答案:

答案 0 :(得分:0)

不确定x-editable如何运作

但是在第一眼看,我认为如果你想更新一些数据,你的ajax的类型应该是“POST”。在我看来,你在“$ key”附近的SQL查询中有语法错误