Jump to content

Check If Username Is Taken?


han003

Recommended Posts

Hi!

 

I've made a simple registration form, and I'm currently using javascript to validate it.

The thing is, I want to be able to check if username is available before they hit the submit button, and I don't know how to integrate PHP into form validation and stuff..

 

:confused:

Link to comment
Share on other sites

Well if you want you can use PHP

 

 

 

<?php

 

$f_usr = username; // where the value of the text input of the username and password

$f_pswd = password;

$con=mysql_connect("localhost","root","");

 

if(! $con){

die('Connection Failed'.mysql_error());

}

 

mysql_select_db("YOUR_DATABASE",$con);

$result = mysql_query("SELECT * FROM ROW_IN_YOUR_DATABASE WHERE username= '" .$f_usr. "' AND password= '" .$f_pswd. "' ");

$numrows=mysql_num_rows($result);

 

if ($numrows == 0){ //If there's a row that contains this username and passowrd

ENTER_WHAT_YOU_WANT_TO_DO_IN_THIS_CASE;}

else{

ENTER_WHAT_YOU_WANT_TO_DO_IF_NOT;}

}

 

 

?>

Link to comment
Share on other sites

Hi!

 

I've made a simple registration form, and I'm currently using javascript to validate it.

The thing is, I want to be able to check if username is available before they hit the submit button, and I don't know how to integrate PHP into form validation and stuff..

 

:confused:

 

1) Be aware that the user can disable Javascript, which means none of your validations will run. It is OK to validate using Javascript for the purpose of giving the user a quicker negative response however you absolutely must validate (again) on the server using PHP.

 

2) PHP runs on the server. It is done and has gone away before the browser ever gets the page to display. So, in fact, you cannot "integrate PHP into form validation" (on the client side). In order to "integrate" them, the browser makes a call to the server which will run a PHP script and send a result. This is usually handled using AJAX (Asynchoronous Javascript and XML).

Link to comment
Share on other sites

1) Be aware that the user can disable Javascript, which means none of your validations will run. It is OK to validate using Javascript for the purpose of giving the user a quicker negative response however you absolutely must validate (again) on the server using PHP.

 

I'll add a clarification to DavidAM's spot-on statements. Even when you are using AJAX, the validation for something such as verifying if a username is taken still takes place "on the server", and it is important to revalidate when the form is posted. The username could have been taken between the time the validation was done using AJAX and the time the user ultimately submitted the form.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.