1

I try since two days a check from the username befor a new user can registration. I have read all what i have found but i can't find the problem.

When the user check is the name free, i not become a answer from php/msql. Can you please check my code?

Table code:

<form id="form" method="get" action="">

    <legend>Formular mit Validierung</legend>
    <tr>
        <td><label id="geschlecht">Geschlecht:* </label></td>
        <td><select name="anrede" value=""/> 
        <option> Frau </option> 
        <option> Mann </option> 
        <option> Paar </option>
        </select>
      </td>
    </tr>
    <tr>
        <td><label for="vorname">Vorname* </label></td>
        <td><input type="text" id="vorname" name="vorname" />
     </td>
    </tr>
    <p>
        <label for="nachname">Nachname* (mindestens 2 Zeichen)</label>
        <input type="text" id="nachname" name="nachname" />
    </p>
    <p>
        <label for="email">E-Mail*</label>
        <input id="email" name="email" />
    </p>

    <p>
        <label for="geburtsDatum">Geburtsdatum:*</label>
        <input type="text" name="geburtsDatum"  id="geburtsDatum" value=""/>
    </p>
    <p>
        <label for="username">Benutzername:*</label>
        <input type="text" name="username"  id="benutzername" value=""/>
    </p>       
    <p>
        <label for="message">Mitteilung*</label>
        <textarea id="message" name="message"></textarea>
    </p
    ><p>
        <input class="submit" type="submit" value="Submit"/>
    </p>
</fieldset>

js code:

$(document).ready(function() {
// hier die Methode .validate()
$("#form").validate({
    submitHandler: function() {
    },
    rules: {
        vorname: {
        required: true,
        minlength: 3
    },
        nachname: {
        required: true,
        minlength: 3
    },
    email: {
        required: true,
        email: true 
    },
    geburtsDatum: {
        required: true,
        date: true  
    },

    username: {
            required: true,
            minlength: 3,
            remote: "check.php"
        },

    },
    messages: {
    username: {
            required: "Bitte Tragen Sie ihren Benutzername ein",
            minlength: "Der Benutzername ist zu kurz",
            remote: "Somenoe have already chosen nick like this."
        },
        dateformat: "Choose your preferred dateformat",
        terms: " "
    }
});

var newsletter = $("#newsletter");
var isChecked = newsletter.is(":checked");
var themen = $("#newsletter_themen");
themen[isChecked ? "show" : "hide"]();
//var themaInputs = themen.find("input")
// .attr("disabled", !isChecked);
newsletter.click(function() {
    themen[this.checked ? "fadeIn" : "fadeOut"](1000);
    //themaInputs.attr("disabled", !this.checked);
});

and the php code:

$username = trim(strtolower($_REQUEST['value']));
$sql =" SELECT  login FROM benutzername WHERE  login =  '".$username."' LIMIT 1";
$result = $connector->query($sql);
$num = mysql_num_rows($result);
if ($num == 1){
    echo "true";

I hope you can help me.

Best regards Thomas

Robin Maben
  • 22,194
  • 16
  • 64
  • 99
Thomas
  • 11
  • 1
  • 1
    It won't help you find a solution, but you should stop using `mysql_*` functions. They're being deprecated. Instead use [PDO](http://php.net/manual/en/book.pdo.php) (supported as of PHP 5.1) or [mysqli](http://php.net/manual/en/book.mysqli.php) (supported as of PHP 4.1). If you're not sure which one to use, [read this SO article](http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons). – Matt Aug 06 '12 at 20:51
  • [SSCCE](http://robzu.com/sscce-short-self-contained-correct-compilable-example) – Robert Aug 06 '12 at 20:52
  • @Matt Yes it does, do you see 'remote: check.php' ? – Ohgodwhy Aug 06 '12 at 20:53
  • It's possible that `$num = mysql_num_rows($result);` doesn't return what you think it does. – Matt Aug 06 '12 at 20:56

3 Answers3

0

From what I can see there are two possibilities:

A) $num = mysql_num_rows($result); doesn't set $num to what you think it does.

B) $num != 1: Instead of

if ($num == 1) { ... }

The following will work just as well since you're testing for existence of the record, not existence of only one record

if ($num > 0) { ... }
Matt
  • 6,993
  • 4
  • 29
  • 50
0

The issue here is, if your code here is exactly the same as in your dev, $_REQUEST['value'] is wrong.

It will always be undefined because remote sends the name of the validation element not some arbitrary placeholder.

You need to use $_REQUEST['username'] moreover, later on in the code, you redeclare the validation on this object.

Also........

You do echo "true". Which is wrong for two reasons. First, we need to return a json_encoded array for this plugin to accept the response. 2nd, using "true" will return "\true\", when we want true. This is a BOOLEAN we are sending, not a string.

$username = trim(strtolower($_REQUEST['username']));
$sql = sprintf("SELECT login FROM benutzername WHERE login = '%s' LIMIT 1", $username);
$result = $connector->query($sql);
$num = mysql_num_rows($result);
if ($num == 1){
    $valid = true;
}else{
    $valid = false;
}
return json_encode($valid);

This should do.

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
  • Hi, thank you but i becomme alwaiys the message that "Somenoe have already chosen nick like this" If the username is taken or not. – Thomas Aug 07 '12 at 07:11
0
$username = trim(strtolower($_REQUEST['value'])); SHOULD BE
$username = trim(strtolower($_REQUEST['username']));

And you should stop using mysql functions and switch to using PDO or MySQLi.

Eric
  • 1
  • But it is frustrating because I have new books, this is measured in consenting nor Sun. There are books of 2011! I have inside me in this topic. Thank you. – Thomas Aug 07 '12 at 07:16