here is what iam trying to do.
there is a textfield. which has a watermark in it(js code). so it will say "Enter Username". so user will enter username in it but if somethig go wrong than it should save the value in textfield(php code).

<input type="text" name="username" id="login_username" class="field" value="<?php if(isset($_POST['username'])){echo htmlentities($_POST['username']);}?>" />

the poblem is that its interfering this code below. what this code does is create watermark into field.

 var username = 'Enter Username';

$('#login_username').css('color', 'gray');
$('#login_username').attr('value', username).focusin(function()
{   
    $(this).css('color', 'black');
    if($(this).val() == username)
    {
        $(this).attr('value', '');
    }
}).focusout(function()
{
    if($(this).val() == '')
    {
        $(this).css('color', 'gray');
        $(this).attr('value', username);
    }
});

right now what it does is js code over rides php code. and it only display watermark. but if i remove js code that php code work fine.

is there a way to combine those two scripts?

look at hotmail.com login page. that is what iam trying to do. there is a watermar in textfield but if you enter some thing in it, than it save the value.

Recommended Answers

All 7 Replies

you need to change the js to only fire when empty($_POST['username'])

i tried putting js in if statment:

if($('#login_username') == "")
{
var username = 'Enter Username';
...
}

but this doesnt work. php code seem to be working but it never come in js if statment.
i think the reason is value of testfield will be never empty bc of:

value="<?php if(isset($_POST['username'])){echo htmlentities($_POST['username']);}?>"

if i print the value of textfiel it is "[object Object]"

you need to do it in php

<?php if(empty($_POST['username'])) { ?>
    js here
<?php } ?>

do yo means like this?

<input type="text"... value="
 <?php 
     if(empty($_POST['username']))
                    {?>
                        <script>
                        var username = 'Enter Username';
                           ...
                        </script>

                    <?php }
                    else
                    {
                        echo htmlentities($_POST['username']);
                    }

                    ?>"
                    />

but this doesnt work

Member Avatar for diafol

Aren't you just looking for placeholder? Something like this?

<?php
    session_start();
    $username = (isset($_SESSION['user'])) ? $_SESSION['user'] : '';

?>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>

<form>
    <input placeholder="Place username here" value="<?php echo $username;?>" />
</form>
</body>
</html>
commented: thanks!!!!!!!! +2

@diafol

dude you just blew my mind!!!
i cant belive i didnt knew about placeholder. This just made my live sooo mush easier!

thanks :)

just fyi placeholder is an html5 attribute

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.