我是ajax的新手。来自wrook博客的基础研究 我使用下面的代码使用ajax插入我的数据库 但是除了“等一下”之外,代码不打印任何东西。
JsFiddle link Ajax代码如下: -
/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
var request_type;
var browser = navigator.appName;
if (browser == "Microsoft Internet Explorer") {
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
request_type = new XMLHttpRequest();
}
return request_type;
}
var http = createObject();
function insertReply() {
if (http.readyState == 4) {
var response = http.responseText;
// else if login is ok show a message: "Site added+ site URL".
document.getElementById('insert_response').innerHTML = 'User added:' + response;
}
}
/* -------------------------- */
/* INSERT */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function insert() {
// Optional: Show a waiting message in the layer with ID login_response
document.getElementById('insert_response').innerHTML = "Just a second...";
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var username = encodeURI(document.getElementById('Username').value);
var name = encodeURI(document.getElementById('Name').value);
var password = encodeURI(document.getElementById('Password').value);
document.getElementById('insert_response').innerHTML = 'h ' + username + ' ' + name;
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', 'insert.php?name=' + name + '&username=' + username + '&password=' + password);
http.onreadystatechange = insertReply;
http.send(null);
}
插入db的php代码是:: -
if(isset($_GET['name']) && isset($_GET['username'])&& isset($_GET['password'])){
$name= $_GET['name'];
$username= $_GET['username'];
$password= $_GET['password'];
$sql = "INSERT INTO `vidyasims`.`user_accounts` (`UserID`, `UserName`, `Name`, `Password`) VALUES (NULL, '. $username. ','. $name. ', '. $password. ');";
$insertSite= mysql_query($sql) or die(mysql_error());
//If is set URL variables and insert is ok, show the site name -->
echo $username;
} else {
echo 'Error! Please fill all fileds!';
欢迎支持。提前谢谢。
注意 - 上面的代码会向db!添加一个新用户。
答案 0 :(得分:1)
您缺少html表单输入的ID,并且无法使用document.getElementById()快速修复其值:
<td><input type="text" id="Name" name='Name' value='' ><br></td>
<td><input type="text" id="Username" name='UserName' value=''></td>
<td><input type="password" id="Password" name='Password'></td>
答案 1 :(得分:0)
为什么你不使用这样的jQuery Ajax,我认为这是你工作的简单方法:
JQuery代码
<script language="javascript">
$(document).ready(function(){
$("#myHtmlButtonID").bind("click",function(){
$('#insert_response').html("Just a second...");
var username = $('#Username').val();
var name = $('#Name').val();
var password = $('#Password').val();
/* add some validation if needed here */
$.ajax({
url: "yourPHPpage.php",
data: {
name: name,
username:username,
password: password
},
type: "get",
success: function(txt){
$('#insert_response').html('User added:' + response);
},
error: function(xhr){
$('#insert_response').html('Error: - '+xhr.status+ ' '+ xhr.statusText);
}
});
});
});
</script>