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', '');
    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 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="
                        var username = 'Enter Username';

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


but this doesnt work

Member Avatar

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

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


<meta charset="utf-8">
<title>Untitled Document</title>


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


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, learning, and sharing knowledge.