开发者

Using a php function in javascript

开发者 https://www.devze.com 2023-03-05 02:00 出处:网络
I need to use a php function inside this script which converts the ID into a required format. function onlineStatus() {

I need to use a php function inside this script which converts the ID into a required format.

function onlineStatus() {  
    $.getJSON('updateusers.php', function(data) {  
        $('#users').html('');
        $.each(data, function(i, item) {  
            $('#users').append('[<span>'+ item.userid +'</span>]');
        });
    });
    setTimeout("onlineStatus()",30000);
}

I have tried the following in various ways but I either get nothing or it fails with a missing ) after the argument list.

$('#users').a开发者_如何学Pythonppend('[<span>'<?php nameFormat('+ item.userid +') ?>'</span>]');

I would appreciate your assistance.


PHP functions can only be executed on the server-side. You should probably translate the nameFormat function into javascript so you can use it on the client-side.


If you are trying to run this command on the server side before sending the script to the user, you have forgotten to echo the statement. Either type

$('#users').append('[<span>'<?php echo nameFormat('+ item.userid +'); ?>'</span>]');

or

$('#users').append('[<span>'<?=nameFormat('+ item.userid +')?>'</span>]');

... unless ofcourse the function nameFormat allready does an echo.


It's one side connection. You can use javascript in php, but you can't use php in javascript. For example:

You can assign variable in php, and than use it in JS.

var a = <?php echo $a ?>; 

The same thing you can do with functions ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消