使用Jquery中的Ajax进行内联编辑而不是在数据库中更新

时间:2013-10-03 11:34:05

标签: javascript php jquery html ajax

有人可以通过以下代码建议我可能出错吗?我试图使用Ajax和Jquery在html表上创建一些可编辑的字段。编辑工作在前端,但是当我刷新页面时,编辑消失,因为它们没有写入数据库。

以下是我的代码

Edit.php

<table cellpadding="15"> 


<tr class="heading" bgcolor="#ccc">
    <th>region</th>
    <th>country</th>
    <th style="width:285px;">networkname</th>
         <th>mcc</th>
         <th>mnc</th>
         <th>mnp</th>
</tr>

<?php
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'fms';
$dbPassword = 'cxxxxxxxxxx';
$dbDatabase = 'fms';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");



$sql = mysql_query("SELECT * FROM supplierprice");
print $sql;

while($rows=mysql_fetch_array($sql))
{

if($alt == 1)
        {
           echo '<tr class="alt">';
           $alt = 0;
        }
        else
        {
           echo '<tr>';
           $alt = 1;
        }

echo '<td>'.$rows["supp_price_id"].'</td>
        <td class="edit region '.$rows["supp_price_id"].'">'.$rows["region"].'</td>
        <td class="edit country '.$rows["supp_price_id"].'">'.$rows["country"].'</td>
        <td class="edit networkname '.$rows["supp_price_id"].'">'.$rows["networkname"].'</td>
        </tr>';


}

?>

</table>

jquery的

$(document).ready(function(){



        $('td.edit').click(function(){


                                        $('.ajax').html($('.ajax input').val());
                                        $('.ajax').removeClass('ajax');

                                        $(this).addClass('ajax');
                                        $(this).html('<input id="editbox" size="'+$(this).text().length+'" type="text" value="' + $(this).text() + '">');

                                        $('#editbox').focus();

                                  }




                         );

        $('td.edit').keydown(function(event){


                                     arr = $(this).attr('class').split( " " );


                                     if(event.which == 13)
                                     { 

                                        $.ajax({    type: "POST",
                                                    url:"config.php",
                                                    data: "value="+$('.ajax input').val()+"&rowid="+arr[2]+"&field="+arr[1],
                                                    success: function(data){
                                                         $('.ajax').html($('.ajax input').val());
                                                         $('.ajax').removeClass('ajax');
                                                    }});
                                     }

                                  }




                         );


        $('#editbox').live('blur',function(){

                                     $('.ajax').html($('.ajax input').val());
                                     $('.ajax').removeClass('ajax');
                                    });



    });

的config.php

<?php
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'fms';
$dbPassword = 'xxxxxxx';
$dbDatabase = 'fms';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");



    if(isset($_POST['rownum']))  
    {  
        update_data($_POST['field'],$_POST['value'],$_POST['rownum']);  
    }  

    print_r($_POST);

    function get_data()  
    {  
        $query = $dbh->prepare("SELECT * FROM supplierprice");
        $query->execute();
        return $query;
    }  

    function update_data($field, $data, $rownum)  
    {  
        $sql="UPDATE supplierprice SET ".$field." = '".$data."' WHERE supp_price_id = ".$rownum;
        print $sql;

        mysql_query($sql);      
    }   
?>

0 个答案:

没有答案