Reputation: 2269
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();
});
});
});
Upvotes: 0
Views: 24610
Reputation: 1040
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.
Upvotes: 0
Reputation: 6720
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.
Upvotes: 6
Reputation: 1845
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)')
Upvotes: -1
Reputation: 5559
Have you checked the source code of the result page? i would use only one " before and after php tags
Upvotes: 0
Reputation: 21449
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)')
Upvotes: 0
Reputation: 21553
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="<?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)');
Obviously the PHP interpreter will never see this and the PHP code will never be executed as you have already noted in your question.
Upvotes: 0