1

我有一个注册表单,在提交时会分别验证匹配的密码和域名。如果为真,我将尝试通过 ajax 请求检查数据库中是否不存在域名。

<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

<?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'];
?>

任何关于我在这方面出错的地方的帮助将不胜感激。

谢谢!

4

3 回答 3

2

看起来ajax Request.

data: {
    domain:domain
} , < -- Missing comma here
success: function(result) {
于 2012-10-15T01:09:29.237 回答
1

如果那是您的代码的直接副本 - 您在数据之后的 ajax 调用中缺少逗号:{}, <- 就在那里。

另外,从成功语句中删除 if...else ,因为它也没有正确完成(您正在使用 ONE 等号测试一个值,而所做的只是声明您要测试的值)。试试看:success: function(result) { console.log(result); alert(result); }看看你得到了什么。

于 2012-10-15T00:46:27.830 回答
0

由于某些奇怪的原因,jQuery 无法通过缩短的 url 识别文件。

解决方案是输入整个 url -> 不仅是 smtg/smtg.php ,还有http://www.domain.com/smtg/smtg.php

Also, you could try to send the data in json format by adding the following line of code into your ajax call: "dataType: 'json'," and then outputting from a php file like this: "echo json_encode("return value");"

于 2013-06-14T10:14:22.587 回答