我有一个注册表格,在提交,验证密码和域名,分别匹配。如果为真,我将尝试通过ajax请求检查DB中的域名是否不存在。
<div class="grid-6">
<p>
<label for="First Name">First Name:</label>
<input type="text" name="first_name" placeholder="James" required value="">
<label for="Last Name" name="lastname">Last Name:</label>
<input type="text" name="last_name" placeholder="Brown" required value="">
<label for="email">Email:</label>
<input type="email" name="email" placeholder="email@email.com" required value="">
<label for="Preferred Password">Preferred Password:</label>
<input id="og_password" type="password" name="password" required value="">
<label for="Confirm Password">Confirm Password</label>
<input id="confirm_password" type="password" name="password_confirm" required value="">
</p>
</div><!-- /grid-6-->
<div class="grid-6">
<p>
<label for="Domain Name">Domain Name <span class="italic red">(lowercase letters and numbers only - no spaces):</span></label>
<input id="domain_name_a" type="text" name="domain_name_a" placeholder="mystudioname" required value="">
<label for="Domain Name">Confirm Domain Name:</label>
<input id="domain_name_b" type="text" name="domain_name_b" placeholder="mystudioname" required value="">
</p>
</div>JS
unction passwordMatch() {
var pass1 = $('#og_password').val();
var pass2 = $('#confirm_password').val();
var domain1 = $('#domain_name_a').val();
var domain2 = $('#domain_name_b').val();
var error = true;
if(pass1 != pass2){
alert("Your passwords do not match!");
return false; // cancel form submission if passwords don't match
}
if(domain1 != domain2){
alert("Your domain names do not match!");
return false;
}
//no errors check DB for domain exits
checkDomain(domain1);
}
function checkDomain(domain) {
alert(domain);//testing only
$.ajax({
type:"POST",
url: "/actions/domain.php",
data: {
domain:domain
}
success: function(result) {
if(result = false) {
alert(result);
} else {
alert(result);
}
}
});
}事情通过警报(域)运行良好,这将返回正确的值。问题在于domain.php文件中的某个地方,返回,或者只是简单地不正确地使用.ajax。下面是php
<?php
require_once("../includes/connection.php");
$domainName = $_POST['domain'];
$sql = "SELECT domain_name
FROM user
WHERE domain_name = '{$domainName}'
";
$run = mysqli_query($mysqli, $sql);
$result = mysqli_fetch_assoc($run);
echo $result['domain_name'];
?>对我在这件事上哪里出了差错的任何帮助都将不胜感激。
谢谢!
发布于 2012-10-15 00:46:27
如果这是代码的直接副本,那么在数据之后的ajax调用中缺少一个逗号:{},<-就在那里。
另外,从success语句中删除if...else,因为它也没有正确完成(您正在使用一个等号来测试一个值,所做的只是声明要测试的值)。试试看:success: function(result) { console.log(result); alert(result); },看看你得到了什么。
发布于 2012-10-15 01:09:29
看起来您的在ajax Request中的数据和成功函数之间缺少一个逗号。
data: {
domain:domain
} , < -- Missing comma here
success: function(result) {发布于 2013-06-14 10:14:22
由于一些奇怪的原因,jQuery没有通过缩短的url来识别该文件。
解决方案是键入整个url ->,不仅仅是smtg/smtg.php,而是http://www.domain.com/smtg/smtg.php。
此外,还可以尝试在ajax调用中添加以下代码行,以json格式发送数据:"dataType:'json',“,然后从以下php文件输出:"echo json_encode(”返回值“);”
https://stackoverflow.com/questions/12887963
复制相似问题