0

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";
?>
3
  • @MartinStoyanov Tried it. Not Working :( Commented Jan 10, 2019 at 18:57
  • As javascript runs on the client, and php should have to be ran on the server, it simply will not work this way. What I do in such a situation is to include the del.php in a container that only becomes visible when the click happens. Commented Jan 10, 2019 at 18:57
  • I tried this line $('$content').ready(function(){ instead of $('$content').click(function(){ Still not working Commented Jan 10, 2019 at 19:04

2 Answers 2

2

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.

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

6 Comments

Not Working :-(
By "not working" what do you mean? Are there any errors in the browser's console?
@AjinkyaRathod - "Not working" isn't that helpful. What happens? Check the console for errors and/or the network tab in your browsers developer tools to check if it makes a request at all. Btw, I would recommend putting you js in $(function () { ...your js goes here... }); to make sure that the DOM have finished loading when it's executed. Read more in the manual
@AjinkyaRathod is the script at the top or the bottom of your page? If it is at the top you will need to include it in a document ready handler.
Jay Blanchard. I made such a silly mistake. Wasted hours for this :-( I forget to include the jquery libraries.Thank you very much :-)
|
0

.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()

2 Comments

Nope. Not Woking in either cases
Updated, you had a typo.

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.