Wordpress + jquery (inside functions.php) not working

i was done adding this script into \mythemes\functions.php:

// FGI CODE started

function my_scripts_method() {
// register your script location, dependencies and version
   wp_register_script('custom_script',
   get_template_directory_uri() . '/js/javascript_.js',
   array('jquery'),
   '1.0' );
 // enqueue the script
  wp_enqueue_script('custom_script');
  }
add_action('wp_enqueue_scripts', 'my_scripts_method');

wp_enqueue_script('jquery');
// FGI CODE ended

and also my jQuery script which is located under \mythemes\js\javascript_.js :

(function($) {
$(document).ready(function() {

 $("#pilih_bulan").change(function() {

 alert('a');

 });

})
})(jQuery);

actually my purpose is making a combobox with an ID of : #pilih_bulan always trigger some ajax function each time user change the selection (dropdown items).

And the above code (using alert code) is working fine. Unfortunately, when I changed into this below code, it doesn't work at all. Any ideas?

(function($) {
$(document).ready(function() {

 $("#pilih_bulan").change(function() {

 $.ajax({
  method: "POST",
  url: "./another_processor.php",
  data: { name: "John", location: "Boston" }
})
  .done(function( msg ) {
    alert( "Data Saved: " + msg );
  });

 });

})
})(jQuery);

Answers


Use the below script and why you used wp_enqueue_script('jquery'); please check your source code jquery must be already added

<?php

function my_scripts_method() {
    wp_enqueue_script(
        'custom-script',
        get_stylesheet_directory_uri() . '/js/custom_script.js',
        array( 'jquery' )
    );
}

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

?>

Need Your Help

extract JSON variables with php for email use

php json

I'm having difficulty extracting JSON values (obtained by using a URL) into email variables. The following code works (I have removed identifiable and usable data).

Data in dataSource is not refreshed when source is changed

javascript jquery ajax kendo-ui

I have dataSource in KendoUI that is connected with on click event. I mean - after click ID of the element is passed to function, which - based on the id - chooses proper url for dataSource.