My plugin shows 2 input fields and a button wherever you put the placeholder in WP. After clicking the button it calls a js function which should start a php function using AJAX but somehow i get the error message: "reference error myAjax is not defined"
wsn-plugin.php
function wpb_new_company(){
echo '<input type="text" class="form-control" id="companyName" placeholder="Firmenname">';
echo '<input type="text" class="form-control" id="companyYear" placeholder="Jahr">';
echo '<button onclick="myAjax();" id="btnNewCompany" type="submit" class="btn btn-primary">Erstellen</button>';
}
script.js (which handles all the events)
function myAjax() {
alert("myAjax gestartet");
$.ajax({
type: "POST",
url: 'localhost/wp/wp-content/plugins/wsn-plugin/wsn-plugin.php',
data:{action:'call_this'},
success:function(html) {
alert(html);
}
});
alert("myAjax ausgeführt");
}
and again wsn-plugin.php which should then run some function
if($_POST['action'] == 'call_this') {
echo "i reached it";
}
Changed
function wpb_adding_scripts() {
wp_register_script('wsn_script', plugins_url('script.js', __FILE__), array('jquery'),'1.1', true);
wp_enqueue_script('wsn_script');
}
add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );
and js script:
function myAjax() {
alert("myAjax gestartet");
$.ajax({
type: "POST",
url: '/wp/wp-content/plugins/wsn-plugin/wsn-plugin.php',
data:{action:'call_this'},
success:function(html) {
alert(html);
}
});
alert("myAjax ausgeführt");
}
no chrome shows the error message:
localhost says
fatal error uncaught error call to undefined function add_action() in wsn-plugin.php:16