最后,在获得 Thaikolja的帮助之后
,在这里,我将以简单的步骤分享问题的完整示例。此答案也被视为完整的基本Ajax插件示例。我现在个人建议将每个文件分开存放,以实现更好,干净和舒适的编码。
主插件文件:
<?php// Calling All PHP File To Loadinclude('my_functions.php');function wp_testingPlugin_admin() { echo ' <form id="searchForm"> <input name="WhatToSearch" type="text" /> <input type="submit" value="Search"/> <input type="reset" value="Reset"/> <div id="showReturnData"></div> </form> ';}//Adding "WP Testing Plugin" Menu To WordPress -> Toolsfunction wp_testingPlugin() { // add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function); Menu Under Tools add_management_page("WP Testing Plugin By Hassan", "WP Testing Plugin", 'activate_plugins', "WP-Testing-Plugin", "wp_testingPlugin_admin");}add_action('admin_menu', 'wp_testingPlugin');?>
my_functions.php
<?phpadd_action( 'admin_enqueue_scripts', 'my_enqueue' );function my_enqueue() { wp_enqueue_script( 'ajax-script', plugin_dir_url( __FILE__ ).'my_javascript.js', array('jquery') ); wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );}// Run Search Functionadd_action('wp_ajax_nopriv_SearchFunction', 'ajaxSearchFunction'); // For Web Visitorsadd_action('wp_ajax_SearchFunction', 'ajaxSearchFunction'); // For Admin Userfunction ajaxSearchFunction(){ if($_POST['WhatToSearch'] == ""){ $WhatToSearch = "Nothing"; } else { $WhatToSearch = $_POST['WhatToSearch']; } echo "<div >SUCCESS: Function Is Working Perfectly And Getting Data ".$WhatToSearch.".</div>";}?>
my_javascript.js
jQuery(document).ready(function() {jQuery('#searchForm').on("submit",function(e) {var incomingData = jQuery('#searchForm').serializeArray();incomingData.push({name: 'action', value: 'SearchFunction'});alert(JSON.stringify(incomingData));jQuery.ajax({type: 'post',url: my_ajax_object.ajax_url,data: incomingData,success: function(response) {jQuery('#showReturnData').html(response);},error: function(response) {jQuery('#showReturnData').html('<div">Error</div>');},});return false; // For Not To Reload Page});});
感谢您阅读本文。如果您喜欢这个基本示例,请投票答疑解惑,以帮助他人。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)