1

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').append('[<span>'<?php nameFormat('+ item.userid +') ?>'</span>]');

I would appreciate your assistance.

1
  • Why don't you just put your nameFormat() function into updateusers.php and return the formatted value in your json ? Commented May 10, 2011 at 19:41

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

$('#users').append('[<span>'<a href=viewuser.php?u=><font color= #000000></font></a>'</span>]');
1

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 ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.