Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include php file using Jquery?

I have that div:

<div id="content"></div>

and I want to include inside PHP file using jQuery.

I try with this, but doesn't work:

var about_me = "<?php include('php/about_me.php') ?>"

$('$content').click(function(){
 $('#content').text(about_me);
});

This returns me PHP code like a string?

like image 402
Jordan Avatar asked Mar 11 '12 04:03

Jordan


People also ask

Can we add PHP code in JavaScript?

We can't use "PHP in between JavaScript", because PHP runs on the server and JavaScript - on the client. However we can generate JavaScript code as well as HTML, using all PHP features, including the escaping from HTML one.

What is include () in PHP?

The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.


2 Answers

You could use .load:

$("#content").load('/about_me.php');
like image 122
xdazz Avatar answered Oct 03 '22 09:10

xdazz


Is it a .php file? IF it's a .js or .html file, Apache (or whatever webserver you're using) won't interpret it as a PHP file and won't include the relevant file.

like image 35
StackOverflowed Avatar answered Oct 03 '22 08:10

StackOverflowed