0

When a Javascript function is called - show_picture() - from sprintf() it happily works (the code is below):

$user_row = sprintf("<a href='javascript:show_picture(%d);'> " .
"<img src='images/missing_user.png' width='15' /></a> ", $user['user_id']);

When a php function is called - show_user_func() - from sprintf() it does not work. The error message is:

The requested URL /show_user_func(168); was not found on this server.

The code is:

sprintf("<a href='show_user_func(%d);'>%s %s</a> ",
            $user['user_id'], $user['first_name'], $user['last_name']);

There probably is a simple explanation for this but it currently escapes me. Does anyone have any ideas?

2
  • 2
    You can't call a PHP function from an HTML link, unless you request another page with that link, and that page runs the PHP function. Commented Jan 11, 2015 at 7:17
  • Thanks for that Alexander. When I implemented a solution given below the function was called without waiting for the link to be clicked. So it appears that you are correct. Commented Jan 11, 2015 at 10:00

3 Answers 3

3

PHP functions are not URLs. You can't just call a function like it's a URL. You could create a page that calls that function, using whatever data you supply in the URL (probably in the query string).

Just to explain why it would be a REALLY BAD IDEATM to let users call PHP functions as URLs, consider this (thankfully non-working) hyperlink:

<a href="array_map('unlink', './');">...</a>

A user could easily add this or just directly visit the URL http://example.com/array_map('unlink', './'). If what you are trying to do was possible, the above would nuke your site.

EDIT: If you are just trying to output the value of show_user_func($user['user_id']) and use it as a link, just do this:

sprintf("<a href='" . show_user_func($user['user_id']) . ";'>%s %s</a> ",
        $user['first_name'], $user['last_name']);

I'm not entirely sure why you'd do that, as you haven't given us much to go on, but it seems like it might be what you're looking for.

EDIT 2: Based on the comments, you want to run a PHP function when the user clicks a link on your page. That is possible, but not the way you're doing it. You need to use AJAX. I'd suggest searching Google for "AJAX" and reading some tutorials to get started.

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

2 Comments

Thanks for that Ed. The above solution half works (if that is possible)! The function is called without waiting for the link to be clicked. It appears what I am trying to do is an impossibility.
PHP is server-side; it will always be called before the user sees the page. That said, you can do what you want with AJAX. I suggest you search for AJAX and read some tutorials to get you started.
0

try using like:

<a href="show_user_func.php">Username</a>//your code resides in show_user_func.php

PHP is a server side language, so it is inaccessible to the HTML like the way you are doing. JavaScript can do this, as it is a client-side scripting language.

see a more similar post:Calling a PHP function within an <a > tag, anchor tag

Comments

0
$user_row = sprintf("<a href='" . show_user_func(%d) . "'>%s %s</a> ",

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.