1

I want to include a php file later(dynamically), rather than at the top. All this file does is get some contents from server and stores it in a javascript variable that i later use in jquery.

basically when you click the link, i get the info, then display it to save resources because there are many links that may not be used.

Should i just do $("body").load("phpfile.php"); ? this should work but I am trying to find a more proper way because it has nothing to do with html tag like body.

asked May 10, 2011 at 18:04
2
  • So all you want to do is retrieve information from a php file and put into a javascript variable? Commented May 10, 2011 at 18:07
  • Yeah, is it not worth it? I guess i can just get the variable anyways and display it on click? Commented May 10, 2011 at 18:08

4 Answers 4

1

You are approaching the problem in an odd way. Not to say it wouldn't work, just that you would have a hard time getting to. I would recommend using jquery's ajax with json.

Something like:

<div id="output"></div>

$(function ()
{
 //$.json('urltoRequestfrom?variable1=value1');
 $.post('/echo/json/',
 {json: '{"name":"test"}'},
 function (data)
 {
 $('#output').html(data["name"]); //first json object
 },'json');
});
seth
7019 silver badges17 bronze badges
answered May 10, 2011 at 18:17
Sign up to request clarification or add additional context in comments.

Comments

1

Why would you want to do this?

jQuery does indeed have a load function where you can fetch a page fragment, which is an ajax call, just use AJAX to fetch the data dynamically whenever you want, javascript can be configured to handle the data fetched however and whenever you want.

Also include a better description of your objective, as what you have described is very unclear.

Thanks and good luck,

h

answered May 10, 2011 at 18:10

Comments

0

Always expect the worst from your visitors. If it where possible to include a php file with javascript, it would be a huge risk.

PHP is a server side language, and is not present in the browser. Javascript is a clientside language, and does not know anything about the PHP, only about the outputted HTML.

Use an AJAX call instead, check this page for more info: http://api.jquery.com/jQuery.ajax/

answered May 10, 2011 at 18:53

Comments

0

Simply, you can't. Php is ran server-side. But you could use AJAX or Jquery AJAX

answered May 10, 2011 at 20:10

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.