我尝试使用ajax发送数据。这是我的ajax代码。但我无法将数据发送到数据库。任何人都可以提供帮助吗?
$('#submit').click(function(){
//get data to the variables
var username = $("#username").val();
var password = $("#password").val();
var email = $("#email").val();
var firstname = $("#firstname").val();
var lasttname = $("#lastname").val();
var age = $("#age").val();
var gender = $("#gender").val();
var country = $("#country").val();
var dataString = 'username='+username + '&password='+ password + '&email='+ email + '&firstname='+firstname + '&age='+ age + '&gender='+gender + '&country='+ country;
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "register-exec.php",
data: dataString,
success: function() {
$('#sendform').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>" + dataString)
.append("<p>We will be in touch soon.</p>")
.hide()
.fadeIn(1500, function() {
$('#message').append("<img id='checkmark' src='images/check.png' />");
});
}
});
alert( "Data Saved: ");
});
});
我已经编写了数据库连接的代码,这是正确的。我检查了很多东西并在谷歌搜索,但我无法弄清楚是什么问题。 我的register-exec.php代码是..
<?php
//Start session
session_start();
//Include database connection details
require_once('config.php');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//get the POST values
$username = $_POST['username'];
$password = $_POST['password'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$country = $_POST['country'];
//Create INSERT query
$qry = "INSERT INTO step-signin(username, password, firstname, lastname, email, age, gender, country) VALUES('$username','".md5($_POST['$password'])."','$firstname','$lastname','$email','$age','$gender','$country')";
$result = @mysql_query($qry);
//Check whether the query was successful or not
if($result) {
header("location: register-success.php");
exit();
}else {
die("Query failed");
}
?>