我正在尝试建立一个简单的电子邮件注册,我遇到了这个教程,它似乎正是我想要做的(http://net.tutsplus.com/tutorials/javascript-ajax/building-a-sleek-ajax-signup-form/)。我没有太多的编程知识,所以这是我启动和运行某些东西的最佳选择。我遵循了教程,但不幸的是,我遇到了一些问题。
我的问题是,当我试图提交电子邮件地址时,我在jquery.js的565行得到了Uncaught SyntaxError: Unexpected token <
。当我在开发工具中展开错误时,它显示:
jQuery.extend.parseJSON jquery.js:565
$.ajax.success common.js:36
jQuery.Callbacks.fire jquery.js:1046
jQuery.Callbacks.self.fireWith jquery.js:1164
done jquery.js:7399
jQuery.ajaxTransport.send.callback jquery.js:8180
正如我所说,我是一个新手,所以我非常感谢任何帮助。我已经研究了一段时间,但没有发现任何与我相同的问题。有些是类似的,但我无法用我遇到的任何解决方案来解决这个问题。
这是表单代码:
<form id="newsletter-signup" action="?action=signup" method="post">
<fieldset>
<label for="signup-email">Sign up for email offers, news & events:</label>
<input type="text" name="signup-email" id="signup-email" />
<input type="submit" id="signup-button" value="Sign Me Up!" />
<p id="signup-response"></p>
</fieldset>
</form>
这是注册JS:
/* SIGNUP */
$('#newsletter-signup').submit(function(){
//check the form is not currently submitting
if($(this).data('formstatus') !== 'submitting'){
//setup variables
var form = $(this),
formData = form.serialize(),
formUrl = form.attr('action'),
formMethod = form.attr('method'),
responseMsg = $('#signup-response');
//add status data to form
form.data('formstatus','submitting');
//show response message - waiting
responseMsg.hide()
.addClass('response-waiting')
.text('Please Wait...')
.fadeIn(200);
//send data to server for validation
$.ajax({
url: formUrl,
type: formMethod,
data: formData,
success:function(data){
//setup variables
var responseData = jQuery.parseJSON(data),
klass = '';
//response conditional
switch(responseData.status){
case 'error':
klass = 'response-error';
break;
case 'success':
klass = 'response-success';
break;
}
//show reponse message
responseMsg.fadeOut(200,function(){
$(this).removeClass('response-waiting')
.addClass(klass)
.text(responseData.message)
.fadeIn(200,function(){
//set timeout to hide response message
setTimeout(function(){
responseMsg.fadeOut(200,function(){
$(this).removeClass(klass);
form.data('formstatus','idle');
});
},3000)
});
});
}
});
}
//prevent form from submitting
return false;
});
这就是PHP:
<?php
//email signup ajax call
if($_GET['action'] == 'signup'){
mysql_connect('host','user','password');
mysql_select_db('table');
//sanitize data
$email = mysql_real_escape_string($_POST['signup-email']);
//validate email address - check if input was empty
if(empty($email)){
$status = "error";
$message = "You did not enter an email address!";
}
else if(!preg_match('/^[^'W][a-zA-Z0-9_]+('.[a-zA-Z0-9_]+)*'@[a-zA-Z0-9_]+('.[a-zA-Z0-9_]+)*'.[a-zA-Z]{2,4}$/', $email)){ //validate email address - check if is a valid email address
$status = "error";
$message = "You have entered an invalid email address!";
}
else {
$existingSignup = mysql_query("SELECT * FROM signups WHERE signup_email_address='$email'");
if(mysql_num_rows($existingSignup) < 1){
$date = date('Y-m-d');
$time = date('H:i:s');
$insertSignup = mysql_query("INSERT INTO signups (signup_email_address, signup_date, signup_time) VALUES ('$email','$date','$time')");
if($insertSignup){ //if insert is successful
$status = "success";
$message = "You have been signed up!";
}
else { //if insert fails
$status = "error";
$message = "Ooops, Theres been a technical error!";
}
}
else { //if already signed up
$status = "error";
$message = "This email address has already been registered!";
}
}
//return json response
$data = array(
'status' => $status,
'message' => $message
);
echo json_encode($data);
exit;
}
?>
谢谢!
更新:Shad-我在"success:function(data){"之后插入了该代码,这是正确的吗?完成后,当尝试提交电子邮件地址时,我在控制台中得到了这个,指向新添加的代码行:
Failed:
SyntaxError
arguments: Array[1]
get message: function getter() { [native code] }
get stack: function getter() { [native code] }
set message: function setter() { [native code] }
set stack: function setter() { [native code] }
type: "unexpected_token"
__proto__: Error
<br />
<b>Warning</b>: mysql_num_rows(): supplied argument is not a valid MySQL result resource in <b>/homepages/37/d403623864/htdocs/_php/launch_notify.php</b> on line <b>22</b><br />
{"status":"error","message":"Ooops, Theres been a technical error!"}
出现该错误的开发工具屏幕截图。如果您需要查看任何扩展的行或其他内容,请告诉我:https://i.stack.imgur.com/IwnBr.png
更新#2:使用satoshi提供的代码,我认为我在解决这个问题上取得了一些进展,但我仍然没有解决它。我认为我将其缩小为MySQL连接问题。我试过这个代码:
<?php
mysql_connect("[DB]","[USER]","[PASS]")
or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("signups")
or die(mysql_error());
echo "Connected to Database";
?>
我得到的回应是:
Connected to MySQL
Access denied for user '[USER]'@'%' to database 'signups'
我试过很多东西,但都想不通。我的主机是1&1,然后我使用PHPMyAdmin通过那里创建了表。我试过不同的桌子,都有相同的问题。以下是PHPMyAdmin中显示该表的屏幕截图:https://i.stack.imgur.com/Oe0Fm.png
再次感谢大家到目前为止的帮助。我很感激。
您的PHP文件正在警告您,因为$existingSignup
不是有效的资源。这是因为您的SQL查询无效。出于这个原因,因为PHP正在输出一些意外的东西,所以页面不会返回有效的JSON响应。
在调用mysql_num_rows(...)
之前,请验证您的mysql_query(...)
调用是否返回了有效的资源,如下所示:
$existingSignup = mysql_query("SELECT * FROM signups WHERE signup_email_address='$email'");
if($existingSignup !== FALSE)
{
if(mysql_num_rows($existingSignup) < 1){
// ...
}
else { //if already signed up
$status = "error";
$message = "This email address has already been registered!";
}
}
else {
$status = "error";
$message = mysql_error();
}
编辑:请注意,查询在语法上是正确的,我想你面临的问题是因为你没有正确设置DB表。