0

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.

Edited by hwoarang69

3
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by jstfsklh211
0

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]"

Edited by hwoarang69

0

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

Edited by hwoarang69

1

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>
Votes + Comments
thanks!!!!!!!!
0

@diafol

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

thanks :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.