Reputation: 201
I want to watermark a text box with a variable.
I've got it working by doing:
placeholder="<?php echo $user;?>">
This puts the correct information in the text box as a watermark. However if I don't enter any information in that text box (because I want the data to remain the same) it clears the data stored in the $user
because I have not updated the record.
Upvotes: 3
Views: 1234
Reputation: 4435
While not particularly a PHP solution (for which I would recommend Ben's answer), if you need to have the value, then you can just use CSS to have the values display as placeholders and then use :focus
to make it look like a regular input tag.
You could also use a data-*
attribute and then use Javascript/jQuery on submit to set the values to the data attribute you designate.
input[type="text"] {
color: darkgrey;
padding: 5px;
border: 1px solid grey;
}
input[type="text"]:focus {
color: black;
background-color: white;
}
<input type="text" value="Placeholder value" />
Upvotes: 2
Reputation: 384
This doesn't set a value for the input but it's reliable even when the user has intentionally submitted an empty input:
if (strlen($_POST['user']) == 0) {
$_POST['user'] = $user;
}
You can also make a function out of it:
function nullCheck($index, $default = 'UNKNOWN') {
if (strlen($_POST[$index]) == 0) {
$_POST[$index] = $default;
}
}
Then call it:
nullCheck('user', $user);
But I'm not sure if you put the post variables(in the function) as: {$_POST[$index]}
, because it might consider the index variable as a separate variable.
NB: This code is untested
Upvotes: 1