我想讓我的註冊表單使用$ .ajax函數,而不是使用正常方式 - 將用戶重定向到另一個php頁面。jQuery Ajax函數沒有完成調用
我調用這個函數:
function register(){
$.ajax({
type: "POST",
url: "user/registerConfirm.php",
data: "regUsername=" + document.getElementById("regUsername").value +
"®Password=" + document.getElementById("regPassword").value +
"&myPassCheck=" + document.getElementById("myPassCheck").value +
"®Email=" + document.getElementById("regEmail").value,
dataType: "json",
success: function(msg){
alert("Success!");
alert(msg);
},
complete: function(msg){
alert("Displaying return value now.." + msg);
},
error: function(request, status, error) {
//alert(request.responseText);
alert("Error, returned: " + request);
alert("Error, returned: " + status);
alert("Error, returned: " + error);
}
});
}
當提交按鈕點擊。然而,「完整」功能從來沒有被調用過,其他功能也沒有。
這是php文件的用途:
<?php
session_start();
ob_start();
require("../widgets/functions.php");
connectToDB();
// Check email
if (!filter_var(clean($_POST['regEmail']), FILTER_VALIDATE_EMAIL))
{
$error = 'Invalid email!';
}
else
{
if (clean($_POST['regPassword']) == clean($_POST['myPassCheck']) && clean($_POST['regPassword']) != NULL
&& clean($_POST['regUsername']) != NULL && clean($_POST['regEmail']) != NULL) // Register can be allowed
{
// Check if their already is a user with the same name..
$sql="SELECT * FROM `users` WHERE username='".clean(trim($_POST['regUsername']))."'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
if($count==1){
// Their was already a user with this name!
$error = 'Registration failed - username already taken..';
}
else if ($count == 0){
// Registration allowed, no user found with the same name
// Encrypt password
$encryptPass = md5($_POST['regPassword']);
$subject = "Confirmation registration";
$message = '
<html>
<head>
<title>Registration at codexplained</title>
</head>
<body>
<p>Hello '.clean($_POST['regUsername']).',</p>
<p>Thank you for registering at our website!
</p>
<p>If you wish, you can now edit your profile, to change your display options and/or to upload your own profile image!<br />
We hope to see you commenting on our articles soon!<br />
If you wish to receive more information - Please contact us, or message any of our moderators.</p>
<hr />
<p>- Current moderators - <br />
Ruud<br />
Willem<br />
Quint
</p>
<hr />
</p>
- Contact details - <br />
Codexplained.tk<br />
[email protected]
</p>
</body>
</html>
';
$from = "[email protected]";
$headers = 'From: Codexplained'."\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$to = clean($_POST['regEmail']);
if (mail($to,$subject,$message,$headers))
{
// Success
}
else
{
// Failed
}
// Insert data
$query = "INSERT INTO `users`
(
`Id` ,`Username` ,`Password` ,`Rank`,`E-mail` ,`PostAmount`, `ProfileImage`, `Ip`, `LastIP`
)
VALUES (NULL , '".clean(trim($_POST['regUsername']))."' , '".$encryptPass."' , 'member', '".clean($_POST['regEmail'])."' , '0', 'none', '".$_SERVER['REMOTE_ADDR']."','".$_SERVER['REMOTE_ADDR']."')
";
mysql_query($query)or die(mysql_error());
$error = 'Registration completed!';
}
}
else
{
if (clean($_POST['regPassword']) != clean($_POST['myPassCheck']))
{
$error = 'Passwords do not match...';
}
else
{
$error = 'Registration failed - not enough data..';
}
}
}
echo $error;
//mysql_close();
//header("location:../index.php?page=register");?>
這個實際工作 - 當按下提交,並同時輸入正確的數據,將在事實上註冊用戶,只是不會給任何反饋 - 這是一個問題。
我希望你能找到我錯過的東西。
在此先感謝。
答案試試這個:'回聲$誤差。 'TEST';'。那麼你一定會回報一些東西。 – 2011-03-01 10:49:16
我剛剛測試過,使用一個單獨的PHP文件 - 這個文件基本上是:echo'test';什麼都沒有發生,所以我可以假設這個bug在jQuery函數中。 – Ruud 2011-03-01 10:53:52
你爲什麼使用'dataType:「json」,'? – 2011-03-01 10:56:39