wip ajax in menu with fetch
This commit is contained in:
27
plugins/wp_model_plugin/php/menu/menu.php
Normal file
27
plugins/wp_model_plugin/php/menu/menu.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
function wp_model_plugin_content() {
|
||||
echo "
|
||||
<input type='text' id='mytext'>
|
||||
<button id='mybutton'>send</button>
|
||||
|
||||
<script>
|
||||
const inputElement = document.getElementById('mytext');
|
||||
const sendButton = document.getElementById('mybutton');
|
||||
|
||||
sendButton.addEventListener('click', () => {
|
||||
const inputValue = inputElement.value;
|
||||
const myurl = php_data.ajax_url;
|
||||
console.log(myurl);
|
||||
|
||||
fetch(url, {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
body: JSON.stringify({ data: inputValue })
|
||||
})
|
||||
});
|
||||
</script>
|
||||
";
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -93,4 +93,11 @@ function plugin_menu() {
|
||||
}
|
||||
add_action('admin_menu', 'plugin_menu');
|
||||
|
||||
add_action( 'wp_ajax_nopriv_get_data', 'my_ajax_handler' );
|
||||
add_action( 'wp_ajax_get_data', 'my_ajax_handler' );
|
||||
|
||||
function my_ajax_handler() {
|
||||
wp_send_json_success( 'It works' );
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user