从PHP中的函数获取值并在div或列中显示值,使用以前的值重新加载页面

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);
    }

}*/
?>

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

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