저는 사용자가 다른 PHP 페이지로 리다이렉트하는 일반적인 방법 대신에 .ajax 함수를 사용하여 등록 양식을 만들고 싶습니다.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);
}
});
}
하면 제출 버튼을 클릭 :
나는이 함수를 호출합니다. 그러나 'complete'함수는 호출되지 않으며 다른 함수도 호출되지 않습니다.
이 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");?>
이 실제로 작동 - 제출 누를 때, 그것은 실제로 사용자를 등록 할 올바른 데이터를 입력하는 동안, 단지 피드백을 제공하지 않습니다입니다 - 이는 문제입니다.
내가 놓친 것을 찾을 수 있기를 바랍니다.
미리 감사드립니다.
의 대답을 참조
또는 텍스트 같은 다른 데이터 유형을 사용 : 귀하의 경우에 PHP 스크립트의 마지막 줄을 변경합니다. '테스트'; 그럼 분명히 뭔가를 돌려 줄거야. –
방금 분리 된 PHP 파일을 사용하여 테스트했습니다.이 파일은 기본적으로 다음과 같습니다. echo 'test'; 아무 일도 없었으므로 버그가 jQuery 함수에 있다고 가정 할 수 있습니다. – Ruud
왜'dataType : "json",'?을 사용하고 있습니까? –