-1

I am having two fields 'name' and 'username'. I am checking the username availability and this I am able to check. My problem is with the manner in which I display the form. I want to achieve

  1. Showing messages in the same row in which the username is displayed but in the third column.
  2. While showing the messages, it should display the previous entered values.

And I want to achieve this just with html, javascript and php. I don't want to go for any other language or technology

Login.php is building the form.

    <?php 
include 'login2.php';
function form($name, $username)
{
?>
<form name="mylogin" method="post">
<table>
<tr><td>Name</td>
<td><input type="text" id="name" name="name" value="<?php echo $name?>" required></td></tr>
<tr><td>Username</td>
<td><input type="text" id="user" name="user" value="<?php echo $username?>" required></td>
<td id="messgae"></td>//want to display message over here have tried value"<?php echo $message"?>                  
</tr>
<tr><td><input type="submit" name="submit" value="Username Availability Check"></td></tr>
<?php usercheck()?>
</table>
</form>
<?php
}
?>

<?php
form('','')
?>

In Login2.php I am just checking the username and right now I am not inserting the values, so that the insert query is commented and for sending back the entered values by user. The problem I am facing is, the form is getting displayed twice after I click submit button or user availability check button. I know it is happening because I am calling the function twice but how to avoid it?

Code

<?php 
function connect()
{
    mysql_connect('localhost','root','password');
    mysql_select_db('loginprac');
}
function usercheck()
    {
        if(isset($_POST['submit']))
        {
        connect();
        $name=$_POST['name'];
        $user = mysql_real_escape_string($_POST['user']);
        $check_for_username = mysql_query("SELECT user from userpage WHERE user = '$user'");
        $count = mysql_num_rows($check_for_username);
        if($count != 0)
        {

            form($name,$user);
        }
        }
    }
    /*function insert()
{
    connect();
    if(isset($_POST['submit']))
    {
        usercheck();
        $name=$_POST['name'];
        $user=$_POST['user'];
        $sql1= "INSERT INTO userpage (name,user) VALUES ('$name','$user')";
        mysql_query($sql1);
    }

}*/
?>
Sarvap Praharanayuthan
  • 4,212
  • 7
  • 47
  • 72
new2world
  • 43
  • 1
  • 3
  • 6
  • To start you need to define the variable before you can call it. Meaning $message = something. What do you mean by "previous entered values" is this the username and pass? – StenW Jul 31 '13 at 07:27
  • @StenW: yeah i defined it before while i used it in program and previous entered value means which user registered while login that page – new2world Aug 01 '13 at 07:47
  • I would also be careful about using the php mysql extension. It is not very secure and you leave your application open for injector attacks see: http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php. You should use mysqli or PDO they are both safe if you use the correctly. I prefer PDO, see: http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html. – StenW Aug 01 '13 at 12:16

2 Answers2

0

Get the <form> outside the function form(). Pass only the messages and username to the function.

Sarvap Praharanayuthan
  • 4,212
  • 7
  • 47
  • 72
0

If you want to use javascript, on clicking submit button/useravailability check button, call an ajax function which can then go to your php code with the newly entered username(to check the availaility). Check the availabilty and print your message there. Then replace html content(with the ajax response text) in your document where you actually want the message.

here an introduction to such an ajax function

or try jquery ajax

Vipin Kumar KM
  • 356
  • 5
  • 17
  • i dnt want to use ajax or anyother tech as mentiones before.want to achieve it with html,javascript and php. thanks – new2world Aug 01 '13 at 07:48