1

我是 ajax 新手,所以我想问一个简单的 ajax 脚本来更新 mysql 数据库,使用 HTML5 contenteditable="true" 进行内联编辑。

已经在谷歌上搜索,但仍然不知道如何弄清楚。有人可以帮我提供一个 javascript 来更新数据库,甚至不用使用 javascript 的提交按钮。

我尽量不使用 jQuery,因为它可以在 iPad 和手机上访问,即使我正在使用 twitter 引导程序

这是我的html5

<!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
        <title>Contact</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <!-- Le styles -->
        <link href="../assets/css/bootstrap.css" rel="stylesheet">
        <link href="../assets/css/bootstrap-responsive.css" rel="stylesheet">
        <link href="../assets/js/google-code-prettify/prettify.css" rel="stylesheet">
    <style>
        body {
            padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
        }
    </style>
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="../assets/js/jquery.js"></script>
<script src="../assets/js/bootstrap.min.js"></script>
<script src="../assets/js/google-code-prettify/prettify.js"></script>
<!-- Fav and touch icons -->
<link rel="shortcut icon" href="../assets/ico/favicon.ico">
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="../assets/ico/apple-touch-icon-144-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="../assets/ico/apple-touch-icon-114-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="../assets/ico/apple-touch-icon-72-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="../assets/ico/apple-touch-icon-57-precomposed.png">
</head>

----
----
<div class="contact">
    <form class="form-horizontal" method="POST" action="updatecontactmaininfo.php">
         <?php
            $sql=mysql_query("select * from contacts where SupplierId='$compid'");
            $dt=mysql_fetch_array($sql);
        ?>
        <table class="table table-bordered table-hover table-striped ">
            <tr>
                <td><i class="icon-user"></i> ID:</td>
                <td><input type="text" name="SupplierId" value="<?=$dt[SupplierId]?>"></td>
                <td><i class="icon-briefcase"></i> Company:</td>
                <td><input type="text" name="CompanyName" value="<?=$dt[CompanyName]?>"></td>
            </tr>
            <tr>
                <td><i class="icon-home"></i> Address:</td>
                <td colspan="3"><input type="text" name="Address1" value="<?=$dt[Address1]?>"></td>
            </tr>
            <tr>
                <td><i class="icon-map-marker"></i> City:</td>
                <td><input type="text" name="City" value="<?=$dt[City]?>"></td>
                <td><i class="icon-map-marker"></i> State:</td>
                <td><input type="text" name="State" value="<?=$dt[State]?>"></td>
            </tr>
            <tr>
                <td align="right" class="tdblue"><i class="icon-flag"></i> Country:</td>
                <td><input type="text" name="Country" value="<?=$dt[Country]?>"></td>
                <td align="right" class="tdblue"><i class="icon-envelope"></i> Zip:</td>
                <td><input type="text" name="State" value="<?=$dt[ZipCode]?>"></td>
            </tr>
            <tr>
                <td><i class="icon-volume-up"></i> Phone:</td>
                <td><input type="text" name="Phone" value="<?=$dt[Phone]?>"></td>
                <td><i class="icon-print"></i> Fax:</td>
                <td><input type="text" name="Fax" value="<?=$dt[Fax]?>"></td>
            </tr>
            <tr>
                <td><i class="icon-globe"></i> Website:</td>
                <td><input type="text" name="website" value="<?=$dt[website]?>"></td>
                <td><i class="icon-adjust"></i> Zone:</td>
                <td><input type="text" name=Zone"" value="<?=$dt[Zone]?>"></td>

                <input type="hidden" name="RecId" value="<?=$dt[RecId]?>">
                <input type="hidden" name="limit" value="<?=$limit?>">
            </tr>
        </table>   
</div>

我的 PHP (updatecontactmaininfo.php) 是:

<?php
    include("../config.php");

    $SupplierId             =   $_POST[SupplierId];
    $CompanyName            =   $_POST[CompanyName];
    $Address1               =   $_POST[Address1];
    $City                   =   $_POST[City];
    $State                  =   $_POST[State];
    $Country                =   $_POST[Country];
    $ZipCode                =   $_POST[ZipCode];
    $Phone                  =   $_POST[Phone];
    $Fax                    =   $_POST[Fax];
    $website                =   $_POST[website];
    $Zone                   =   $_POST[Zone];
    $RecId                  =   $_POST[RecId];
    $limit                  =   $_POST[limit];

    mysql_query("update contacts set SupplierId='$SupplierId', Address1='$Address1', City='$City', State='$State', Country='$Country', ZipCode='$ZipCode', Phone='$Phone', Fax='$Fax', website='$website' where RecId='$RecId'");

    header('Location:contactsform.php?limit='.$limit);

?>

谢谢你。

4

1 回答 1

1

您可以附加event oninputcontenteditable.

var el = document.getElementById('element_id')
el.oninput = function(){
    // you can grab values of other inputs and send ajax call here
}
于 2012-11-22T10:15:08.580 回答