Pregunta

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?

¿Fue útil?

Solución

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);
});

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top