0

I was wondering how i could achieve using <?php?> in javascript for url's? There's a certain route you have to go, Anyone know?

the normal way for example:

$fetchContent = $('#div').load('website/members #content');

What i'm trying to do:

$fetchContent = $('#grav').load('<?php?> #poppu');

Yep, thats wrong as hell lol, but i'm sure someone knows

I would also like to know how to tie php with javascript, but thats probably a whole new topic

1
  • You could have PHP echo out a url into a dynamically generated page, but you cannot embed PHP code into a javascript block and expect it to execute in the browser. Commented Dec 31, 2010 at 6:09

5 Answers 5

2

You said it right :)

Yep, thats wrong as hell lol, but i'm sure someone knows

Anyway, from your php script, output the url as a javascript code anywhere in the script before the javascript used for ajax call, e.g.

<?php
    echo '<script language="javascript"> var g_ajax_url = "'. $the_url . '";</script>';
?>

and in your javascript, use it this way

$fetchContent = $('#grav').load(g_ajax_url + ' #poppu');

What it simply does is define g_ajax_url as a global variable with the proper php value, and you can use that variable in your js as you use other variables.

To tie php with js directly, try looking into xmlrpc topic.

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

2 Comments

Awsome! Can i use this method for any js function that i might need php in?
Yes, you can. Just remember simply that in php you are writing out javascript code in the browser. so you can output almost anything from php. once that is part of the browser, the remaining client side code can use it in usual manner
1

If javascript is in .php file you can use <?php echo $url ?> and if the file is .js you can't use <?php ?>

Comments

1

It is not clear to me what you are trying to achieve. I assume you are using the jQuery load() function, if yes, you should state so.

You can't load php during javascript execution because the php has already been processes and rendered as HTML and sent back to the client. As PHP is processes on the server it is logical that you cannot run it on the client side.

You could of course send an AJAX request to the server that runs a certain php page and you will be able to use the response as you please.

1 Comment

sounds right, how would i send the request to the server via Ajax?
1

you can't necessarily "tie" them together because they operate in two different spectrums of processing, php being processed on the server, and javascript being processed in the browser.

You can however render javascript within a php file.

if your javascript is included within a <script> tag within your php page your example should work should actually work. The php would render the urls into the script before it is sent to the browser.

if you are wanting to load external javascript files with php inlcuded urls, you will need to set the proper headers and include the php file just as you would a normal .js file.

good article on this topic HERE

5 Comments

well, I have a modal that loads this page holding certain data wrapped in a div. but when i navigate to other pages, it doesnt grab the content, only on the home page
I will need more code to more fully help you. no such thing as too much source code accompanying a question.
ok cool, would both the js and the php page help? or do you just need the js?
also, im reading that document that you sent me the link to, im assuming the headers go at the very top of the php page, correct?
right, changing the header has to happen before anything else outputs.
0

You cannot execute <?php ?> inside JavaScript, but inside PHP you can declare a global variable as:

var x = '<?php echo x;?>';

or, if it's an array, store it as JSON:

var x = <?php json_encode(x); ?>

then access the JavaScript variables inside the external JavaScript.

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.