Ajinkya Rathod
Ajinkya Rathod

Reputation: 37

How to include php file using jquery

I already visited this link,

How to include php file using Jquery?

but when I m trying it in my code, its not working. What could be the issue?

index file

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

<script type="text/javascript">
var about_me = "<?php include('del.php') ?>"
$('$content').click(function(){
    $('#content').load(about_me);
});
</script>

del.php

<?php
echo "hi again";
?>

Upvotes: 0

Views: 1589

Answers (2)

Jay Blanchard
Jay Blanchard

Reputation: 34426

First, make sure you include the jQuery library.

Second, don't use include as it isn't necessary. You will just load the file via jQuery:

<script type="text/javascript">
$(document).ready(function(){
    var about_me = "del.php";
    $('#content').click(function(){
        $('#content').load(about_me);
    });
});
</script>

NOTE: You have a typo in your selector $content should be #content to make it clickable. I have also included a document ready function in the event your script is at the top of the page, instead of the bottom.

Upvotes: 2

rollingthedice
rollingthedice

Reputation: 1125

.load() accepts url (type string) as first parameter, not a php code.

Try to change it from: var about_me = "<?php include('del.php') ?>"

To: var about_me = "/del.php"

EDIT: You have a typo in your event listener's selector, should be $('#content').click() instead of $('$content').click()

Upvotes: 0

Related Questions