0

I have tow file first is index.php and the another is user.js I included user.js into index.php file . but the scripts functions not working from external file so if I put this functions directly in index.php it working perfectly but not working when putting them in external file

<script src="js/user.js" type="text/javascript"></script>

This is function which is into users.js.

function deletemessage(id) {

    var parent = $(this).parent().parent();
    $.ajax({
        type: 'get',
        url: 'Save.php?id=delete_msg&msg_id=' + id,

        beforeSend: function() {
            parent.animate({
                'backgroundColor': '#fb6c6c'
            }, 300);
        },
        success: function(data) { //alert(data);
            $('#message_ha').html(data);
            parent.slideUp(300, function() {
                parent.remove();
            });
        }
    });
}
Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
Maj.jad
  • 51
  • 1
  • 1
  • 8
  • Where are you calling your function? Have you included jQuery? Does the console show any errors? You say you have two files (`index.php` and `user.js`), yet your AJAX request is pointing to `Save.php`, what is in that file? – Script47 Mar 29 '18 at 11:39
  • Can you add the error you get? Maybe you just gave an incorrect path? – mrdeadsven Mar 29 '18 at 11:40
  • try this: `src="/js/user.js"` – mcv Mar 29 '18 at 11:43
  • I don't get any error – Maj.jad Mar 29 '18 at 11:43
  • not working so sorry – Maj.jad Mar 29 '18 at 11:44
  • @Maj.jad my comment above should help you find a potential issue, have you tried the above? – Script47 Mar 29 '18 at 11:45
  • @Script47 I included jquery in index.php file , save.php is file which is used to save records into database .all this code working nice when I put code into index.php but not working at all when move script to external file – Maj.jad Mar 29 '18 at 11:47
  • Most probably, you have a wrong path indicated in path. You can check you devtools network section to check if your js file is correctly loaded – Orodan Mar 29 '18 at 11:48
  • what error does your browser console report when you click the button? Inspect the html of button, switch to the necessary tab and look for the error that is being generated from pushing the button. [For example in Chrome.](https://stackoverflow.com/questions/66420/how-do-you-launch-the-javascript-debugger-in-google-chrome) – mcv Mar 29 '18 at 11:53
  • 1
    Uncaught ReferenceError: deletemessage is not defined at HTMLAnchorElement.onclick – Maj.jad Mar 29 '18 at 11:57
  • Could be that it's looking for `Save.php` in the same directory as your `users.js` file. Try adding `/` before `Save.php` – mcv Mar 29 '18 at 11:57
  • no save.php and index.php in same folder but user.js in js folder – Maj.jad Mar 29 '18 at 11:58
  • yes, in your js file you have `url :'Save.php...'` change it to `url:'/Save.php..'` – mcv Mar 29 '18 at 11:59
  • no it not working , I deleted all code into function and just adding alert('hello') but it still not working – Maj.jad Mar 29 '18 at 12:01
  • update the question showing the snippet of html from index.php with the button – mcv Mar 29 '18 at 12:04
  • 1
    I don't know but I changed created new javascript and placed the js code into this new js file , and included it in to index.php and worked fine – Maj.jad Mar 29 '18 at 12:12
  • I have found similar questions to this issue. [Do no use onclick but use the object id with an event listener](https://stackoverflow.com/questions/17378199/uncaught-referenceerror-function-is-not-defined-with-onclick/17378538). Here is it for JS but there is a similar practice for jQuery – mcv Mar 29 '18 at 12:13
  • I was also going to mention that it might be a browser caching issue. – mcv Mar 29 '18 at 12:14

2 Answers2

0

I don't know but I changed created new javascript and placed the js code into this new js file , and included it in to index.php and worked fine

Maj.jad
  • 51
  • 1
  • 1
  • 8
-2

Can you drag User.js file from js folder And drop in Index.php .

kalai
  • 187
  • 11