Joel
Joel

Reputation: 6173

append PHP code to HTML input placeholder?

I want to append a PHP function that shows the user his/her IP within an inputs' placeholder. I've never seen anything like this been done before so I'd like to just see if it's possible or not.

My thoughts right now is to do something along these lines:

$('input').attr(placeholder,function(){
$('input').append('<?php $variable ?>');
});

then run the PHP code through here some how. If someone can shed some light on this I would be very grateful!

Upvotes: 0

Views: 393

Answers (2)

Sylvain Martin
Sylvain Martin

Reputation: 2375

If you want to do it in PHP you can make a function to retrieve the IP of the user then echo the script in your page:

<?PHP

function getUserIP()
{
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }

    return $ip;
}


echo "<script type='text/javascript'>

    $('input').attr(placeholder,function(){
    $('input').append('".getUserIP()."');
    });
    </script>";
?>

Upvotes: 1

IHead
IHead

Reputation: 23

try this:

$('#inputID').attr("placeholder","<?php echo $variable; ?>");

And if you want to append to all inputs:

$(":input").attr("placeholder","<?php echo $variable; ?>"); should do the work

Upvotes: 2

Related Questions