我是ajax的新手所以我想问一个简单的ajax脚本,使用HTML5 contenteditable =“true”更新内联编辑mysql数据库。
已经在谷歌上搜索,仍然不知道如何解决这个问题。 有人可以帮我提供一个javascript更新数据库,而无需使用javascript提交按钮。
我尽量不使用jQuery,因为它可以在iPad和手机上访问,即使我认为我正在使用twitter bootstrap
这是我的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);
?>
谢谢。
答案 0 :(得分:1)
您可以将event
oninput
附加到contenteditable
。
var el = document.getElementById('element_id')
el.oninput = function(){
// you can grab values of other inputs and send ajax call here
}