0

I'm trying to use one of my PHP functions within one of my jQuery functions. I understand that it won't execute the code, but I need to echo the function call, so the server can process it. So far I have this:

    .html('<h2>Please <a href=""<?php echo absolute_url("login.php"); ?>"">login</a> or <a href="signup.php">register</a> to vote for this post.</h2>(click on this box to close)')

But it's not working correctly. I heard that I need to enclose the actual php function call within Javascript with quotation marks, which I did (both single and double), but they didn't do the trick. Any ideas?

The whole function for anyone wondering:

      // login or register notification
$(document).ready(function() {
    $('.notice').click(function() {
        $('.error-notification').remove();
        var $err = $('<div>').addClass('error-notification')
        .html('<h2>Please <a href=""<?php echo absolute_url("login.php"); ?>"">login</a> or <a href="signup.php">register</a> to vote for this post.</h2>(click on this box to close)')
        .css('left', $(this).position().left);
        $(this).after($err);
        $err.fadeIn(150);
    });
    $('.error-notification').live('click', function() {
        $(this).fadeOut(150, function() {
            $(this).remove();
        });
    });
});
1
  • did you try this with just text without the absolute_url function Commented May 5, 2011 at 10:00

6 Answers 6

6

You can't -- php commands run during page loading -- your page has already loaded and rendered. You should know php is server side code. This means when your website is requested, your server generates the output file and fills in all the echo values into your output file. The client gets the page with all the variables echo'd to your page -- no php commands are ever given to a client, they're instructions for your server to compile the webpage back to the client.

Why don't you just store absolute_url("login.php") within a javscript variable during your pageload

var loginUrl = "<?php echo absolute_url("login.php"); ?>";

and then

.html('<h2>Please <a href="' + loginUrl + '">Login</a>')....

Edit -- oh wait duh -- you are doing that :P sorry for the tangent then, your js file is linked to your page, it's a stub pointing to the file to be included. This does not register php commands.

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

7 Comments

Value of href is missing quotes. Should be .html('<h2>Please <a href="' + loginUrl + '">Login</a>'). Value of loginUrl should also be quotted and prepared for parsing with javascript, I would write var loginUrl = <?php echo json_encode(absolute_url('login.php')); ?>;.
gettin tired missed the quotes -- why does it need to be converted to json? Is it returning an array? otherwise if it's a simple string there's no reason complicate things by json encoding it
What do you mean with "simple string"? Can it contain new-lines? Quotes? Slashes? Try using string that contains something more than just letters and digits. And what if absolute_url() returns NULL? Will simple echo work? ;) I agree that absolute_url('login.php') in this particular case probably won't contain any "harmful" character, though generic "simple strings" cannot be just echo'ed without any conversions. json_encode() does not just convert arrays to javascript notation, it safely encodes numbers, strings, NULL, arrays/objects etc - even if they are not part of array/object.
hes trying to get his url i dont think he's about to have returned any new lines or crazy characters...
bob -- why dont you put it in a varaible and call the function in the external javascript file passing it in? I know you probably want to consolidate your code in one file, so declare the variables on the php html page, and call the external function passing it in.
|
0

This will not actually do anything as you have noted in your question but try:

$('elem').html('<h2>Please <a href="<?php echo absolute_url("login.php"); ?>">login</a> or <a href="signup.php">register</a> to vote for this post.</h2>(click on this box to close)');

If this is coming from a PHP page then try:

$('elem').html('<h2>Please <a href="&lt;?php echo absolute_url("login.php"); ?&gt;">login</a> or <a href="signup.php">register</a> to vote for this post.</h2>(click on this box to close)');

Obviously the PHP interpreter will never see this and the PHP code will never be executed as you have already noted in your question.

Comments

0

you can't do this in your js file, but you can do this in a <script> tag in HTML file.

.html('<h2>Please <a href="<?php echo absolute_url(\"login.php\"); ?>">login</a> 
or <a href="signup.php">register</a> to vote for this post.
</h2>(click on this box to close)')

1 Comment

It all depends on whether or not you have your .js files processed by php as well. By default this isn't done, but you can configure your server to do this (e.g. by modifying the config, using .htaccess, or appending the .php extension to your .js, like "script.js.php" and including this instead of a plain .js file).
0

Have you checked the source code of the result page? i would use only one " before and after php tags

Comments

0

I think the proper way to use php from Javascript is by creating web services (in php) and call them from your Javascript. If you want to go really wild, you can send the php content to the server using a web service, write it as a temp file, then 'require' the file and run it... this is quite an off thing to do unless there is a very unique use case that cannot be done otherwise. PHP, as an extremely dynamic language allow this kind of usage.

Comments

-1

y u go for php??? i thing its not possible in js,,

best thing is use html its very effective and easy to console..

.html('<h2>Please <a href="login.php">login</a> or <a href="signup.php">register</a> to vote for this post.</h2>(click on this box to close)')

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.