JQuery $.post & PHP“如果”语句停止工作

发布于 2024-11-07 23:16:42 字数 2047 浏览 0 评论 0原文

这是非常具体的,我还没有找到答案。
我正在编写一个脚本来检查名称是否已使用 JQuery $.POST 输入到 SQL 服务器中。

前端看起来像这样:

register.php

<div id="container" class="container">
    <div id="wrapper" class="wrapper">
        <div id="title" class="title">
            Welcome to Taskar!
        </div>
        <div id="login" class="login">
            <form method="post" action="registermembers.php">
            <div id="exists">
                Register Your Company Below:
            </div>
            <input type="text" id="company" name="company" value="Company Name"/> <br />
            <br />
            <input type="text" id="password" name="password" value="Default Password"/> <br />
            <input type="submit" name="submitcompany" value="Submit" />
            </form>

            <script>
            $(document).ready(function() {
                $('#company').keyup(function() {
                    var $company = $(this).val();
                    var $reg = $('#exists').html();
                    $.post('include/reg_post.php', { company: $company, reg: $reg }, function(company) {
                    $('#exists').html(company);
                    });
                });
            });
            </script>

        </div>
    </div>
</div>

后端代码看起来像这样:

reg_post.php

<?php
include 'include/db.php';

$reg = $_POST['reg'];
$company = $_POST['company'];
$email_hash = $_COOKIE['email_hash'];
$password = $_COOKIE['password'];

$sql = "SELECT * FROM taskar_employee WHERE (company = '$company')";
$result = mysql_query($sql) or die(mysql_error());
    if(mysql_num_rows($result) <= 1){
        echo $reg;
    } else {
        echo "This Company Already Exists!";
    } ?>

现在,我遇到的问题是,当我测试这个时,它一直工作正常,直到我输入公司名称时那是在数据库中。这表明该公司已经注册。当我按退格键或输入另一个字母时,它仍然告诉我该公司仍然存在,尽管它显然不会存在。

看起来 if 语句想要在得到相反的语句后阻止 $.post 功能继续执行?

你们觉得怎么样?

This is pretty specific and I have not found an answer yet.
I am writing a script to check if a name is already input into the SQL server using JQuery $.POST.

The front end looks like this:

register.php

<div id="container" class="container">
    <div id="wrapper" class="wrapper">
        <div id="title" class="title">
            Welcome to Taskar!
        </div>
        <div id="login" class="login">
            <form method="post" action="registermembers.php">
            <div id="exists">
                Register Your Company Below:
            </div>
            <input type="text" id="company" name="company" value="Company Name"/> <br />
            <br />
            <input type="text" id="password" name="password" value="Default Password"/> <br />
            <input type="submit" name="submitcompany" value="Submit" />
            </form>

            <script>
            $(document).ready(function() {
                $('#company').keyup(function() {
                    var $company = $(this).val();
                    var $reg = $('#exists').html();
                    $.post('include/reg_post.php', { company: $company, reg: $reg }, function(company) {
                    $('#exists').html(company);
                    });
                });
            });
            </script>

        </div>
    </div>
</div>

The backend code looks like this:

reg_post.php

<?php
include 'include/db.php';

$reg = $_POST['reg'];
$company = $_POST['company'];
$email_hash = $_COOKIE['email_hash'];
$password = $_COOKIE['password'];

$sql = "SELECT * FROM taskar_employee WHERE (company = '$company')";
$result = mysql_query($sql) or die(mysql_error());
    if(mysql_num_rows($result) <= 1){
        echo $reg;
    } else {
        echo "This Company Already Exists!";
    } ?>

Now, the problem I am having is, when I am testing this, it works fine all the way up to when I type in a company name that is in the database. It shows me that the company is already registered. When I press backspace or type another letter, it still tells me the company still exists, though it obviously would not.

It seems that the if statement wants to stop the $.post feature from continuing after it gets the opposite statement?

What do you guys think?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

箹锭⒈辈孓 2024-11-14 23:16:42

您的问题在于您使用 $reg 变量的方式。

$reg 包含 #exists div 的 HTML 内容,因此它首先是“在下面注册您的公司:”。但是,一旦您找到了确实存在的公司名称,您就可以将该 HTML 替换为“该公司已存在!”。从那时起,$reg 在 JavaScript 和 PHP 方面都始终“该公司已经存在!” (因为您总是在 POST 中传递 $reg )。

如果该 div 的内容根本不传递给 PHP 代码或由 PHP 代码处理,那么事情可能会容易得多。如果您的 PHP 脚本仅在公司存在时返回“1”,如果不存在则返回“0”(或其他一些同样简单的标志),那就更简单了。

然后,在 JavaScript 方面,你可以这样做:

$.post('include/reg_post.php', { company: $company }, function(exists) {
    var message;
    if (exists === '1') {
        message = 'This Company Already Exists!';
    }
    else {
        message = 'Register Your Company Below:';
    }
    $('#exists').html(message);
});

Your problem lies in the way you're using your $reg variable.

$reg contains the HTML contents of the #exists div, so it's "Register your Company Below:" to begin with. But, once you've hit a company name that does exist, you replace that HTML with "This Company Already Exists!". From that point on, $reg, on both the JavaScript and PHP sides, is always "This Company Already Exists!" (since you always pass $reg in your POST).

It would probably be much easier if the contents of that div weren't passed to or handled by the PHP code at all. It'd be simpler if your PHP script just returned "1" if the company existed, and "0" if not (or some other equally simple flags).

Then, on the JavaScript side, you could just do:

$.post('include/reg_post.php', { company: $company }, function(exists) {
    var message;
    if (exists === '1') {
        message = 'This Company Already Exists!';
    }
    else {
        message = 'Register Your Company Below:';
    }
    $('#exists').html(message);
});
独闯女儿国 2024-11-14 23:16:42

您传递的 $('#exists').html(); 会在未找到该公司时返回。问题是,如果找到该公司,您将替换 #exists 中的 html。然后,下次您调用该帖子时,您会将此文本传回,因此在这种情况下:

if(mysql_num_rows($result) <= 1){
    echo $reg;
} else {
    echo "This Company Already Exists!";
} 

您的 $reg 现在设置为 This Company Already Exists! 所以无论发生什么情况您将返回该文本。

You're passing $('#exists').html(); to be returned in the event that the company is not found. The problem is, in the event that the company is found, you replace the html within #exists. Then the next time you call the post you pass this text back along so in this case:

if(mysql_num_rows($result) <= 1){
    echo $reg;
} else {
    echo "This Company Already Exists!";
} 

your $reg is now set to This Company Already Exists! so regardless of what happens you're going to return that text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文