immunity Posted May 22, 2007 Share Posted May 22, 2007 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 ) 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 == ) 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); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52569-solved-find-if-exists-username-in-mysql-data-via-javascript-so-it-will-be-on-same-page/ Share on other sites More sharing options...
per1os Posted May 22, 2007 Share Posted May 22, 2007 www.google.com Search for AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/52569-solved-find-if-exists-username-in-mysql-data-via-javascript-so-it-will-be-on-same-page/#findComment-259380 Share on other sites More sharing options...
immunity Posted May 22, 2007 Author Share Posted May 22, 2007 www.google.com Search for AJAX. found this http://www.ajax.nl/ hehe joking thx for the tip i will study AJAX a bit in w3schools and the i go ahead to google Quote Link to comment https://forums.phpfreaks.com/topic/52569-solved-find-if-exists-username-in-mysql-data-via-javascript-so-it-will-be-on-same-page/#findComment-259388 Share on other sites More sharing options...
corbin Posted May 22, 2007 Share Posted May 22, 2007 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). Quote Link to comment https://forums.phpfreaks.com/topic/52569-solved-find-if-exists-username-in-mysql-data-via-javascript-so-it-will-be-on-same-page/#findComment-259397 Share on other sites More sharing options...
ReDucTor Posted May 22, 2007 Share Posted May 22, 2007 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); Quote Link to comment https://forums.phpfreaks.com/topic/52569-solved-find-if-exists-username-in-mysql-data-via-javascript-so-it-will-be-on-same-page/#findComment-259411 Share on other sites More sharing options...
immunity Posted May 23, 2007 Author Share Posted May 23, 2007 thank you all Quote Link to comment https://forums.phpfreaks.com/topic/52569-solved-find-if-exists-username-in-mysql-data-via-javascript-so-it-will-be-on-same-page/#findComment-259683 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.