Jump to content

[SOLVED] Find if exists username in Mysql Data via Javascript (so it will be on same page


immunity

Recommended Posts

hello i tried to check if there is the username (that user type in register form) already in Database (so it would be pop up alert window)

i got 1 php file (register.php)

1 external script (clock it works :D)

i try with php to echo script  that will make see if a $_POST[txtfield] == $row['username']

 

but as i think now it isnt possible right ?

I mean i have to post the form so i will have values in my txtfields and do that == :/

 

anyone could advise me how i have to do it ? (Somehow with Javascript send mysql query and get data and then do the ==  ( i dont know the word in english for the  == :P)

my code is :

<head>
<script src="clock.js"></script>
<?php
$script = "<script type=\"text/javascript\"> function FindUsername(){ alter(\"Υπάρχει ήδη αυτό το username!!!\"+'\\n'+\"Παρακαλώ επιλέχτε κάτι άλλο!!\")}</script>";
	if (($_POST[textfield9]&&$_POST[textfield10]&&$_POST[textfield4]&&$_POST[textfield5]&&$_POST[textfield6]&&$_POST[textfield7]&&$_POST[textfield8]&&$_POST[textfield11]&&$_POST[textfield3]) != NULL)
	{
		if( $_POST[textfield4] == $_POST[textfield5])
		{
			$con = mysql_connect("localhost","****","******");
			if (!$con)
		    {
	  			die('Could not connect: ' . mysql_error());
	  		}
			mysql_select_db("labdb2_19", $con);
	  		$sql = "SELECT * FROM user WHERE login = '$_POST[textfield]'";
	  		$result = mysql_query($sql);
			while ($row = mysql_fetch_assoc($result)) 
			{
				if ($_POST[textfield3] == $row['account'])
				{
					$script = "<script type=\"text/javascript\"> function FindUsername(){ if(document.form2.onsubmit()) { document.form2.submit(); }}</script>";
				}
			}
			echo $script;
			mysql_free_result($result);
			mysql_close($con);
		}
	}

?>

 

Link to comment
Share on other sites

I think you'll be able to figure it out from w3schools (they're tutorial helped me a lot ;p) and google, but just to outline things (mainly because I'm bored):

 

You'll want to have the main page which shows the form and what not.

 

When the form is submitted intercept the data using an onsubmit function (<form onsubmit="return YourFunction()">).  You'll want YourFunction to always return false so that the form doesn't submit (or if the user's browser is ajax capable let it submit and process it using PHP ;p).

 

Make an ajax request to a page (called page2 from now on) with YourFunction.

 

On your page you would want to do the following:

Make sure the input username is valid (alpha numeric or how ever user names are allowed).

Check if it exists in your DB.

If it exists, echo something like '1', and if it doesn't echo something like '0'.

 

On the form page, read the output from Page2 with YourFunction (or any sub function ;p), and then analyze it.

Use the 1 or 0 to dynamically change content on your page alerting whether or not the username exists ;p (or make YourFunction return true so that it submits).

Link to comment
Share on other sites

The code for the Page to check:

function checkUser(username) {
    document.write("<script type=\"text/javascript\" src=\"checkuser.php?username=" & username &"\"></script>");
}

 

The code for checkuser.php

<?php
mysql_connect('localhost') or die(mysql_error());
mysql_select_db('labdb2_19');
$q = mysql_query('SELECT COUNT(*) FROM user WHERE login = \''.filter_input(INPUT_GET,'username',FILTER_SANITIZE_MAGIC_QUOTES).'\'');
if(mysql_result($q,0,0))
print 'alert("Username already exists");';
?>

 

I reckon this method is far better then the XML method of ajax

 

  $sql = "SELECT * FROM user WHERE login = '$_POST[textfield]'";

  $result = mysql_query($sql);

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.