0

I have a registration form wherein one of the field is username which needs to be unique. So, I am checking it first in my database before insert proceeds. If username exists, I put the below line. My problem is after clicking OK, it refreshes the page and clears all the inputted data of the user. I know I can use return false but how do I put this on my echo?

else if(mysql_num_rows($result)>0)
{
    echo '<script>alert("Username is not available")</script>';
} 
else 
{ 
    insert goes here
    <form id="appform"  action="register.php" method="post" onsubmit="return validateform('appform');">
    <input type="text" class="textarea1" name="stdname" size="30" onkeyup="isallnospace(this)" /> .
}
Manjunath Ballur
  • 6,287
  • 3
  • 37
  • 48
안젤라
  • 43
  • 1
  • 7
  • you've updated the code, where is the isallnospace(this) function ? and you're mixing php and html in one lump – Billy Dec 31 '15 at 10:42
  • @Billy function isallnospace(ele) { var r=/\W$/i; if(r.test(ele.value)) { alert("This Field allows Only Alpha Numeric characters."); ele.value=""; ele.focus(); } } – 안젤라 Dec 31 '15 at 10:48
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Dec 31 '15 at 10:51
  • What you're asking requires that you use AJAX. There is not enough code in your question for anyone to answer any more fully. – Jay Blanchard Dec 31 '15 at 10:54

2 Answers2

0

You are validating in the worst way possible. If you want to validate using both client side and server side script, please use ajax.

Uses of Ajax:

  • Update a web page without reloading the page
  • Request data from a server - after the page has loaded
  • Receive data from a server - after the page has loaded
  • Send data to a server - in the background

For more details, visit AJAX Tutorial

Manikiran
  • 2,618
  • 1
  • 23
  • 39
0

best way is to check with ajax before submit, but if you want you can add the $_POST values to the form when it returns to it like so

<input type="text" name="username" value="<?php if(isset($_POST['username'])){echo $_POST['username'];}?>" />
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Billy
  • 2,448
  • 1
  • 10
  • 13