这是围绕Ajax处理的混乱的一般“原因”和“如何”问题。我终于开始在某处实现我的第一个完整的Ajax发布表单了。我在不同的网站上以不同的方向进行各种各样的演示,我挣扎了这么长时间。最大的问题是我从php处理程序得到响应的唯一方法,就是将php放在插件的基本文件中,而不是单独的文件中。
我的简单测试适用于一个文件中的所有php,另一个文件中的表单以及单独的js文件。我似乎无法从主安装插件文件中获取php处理程序。为什么以及如何将处理程序移出此基本文件。我玩过,但找不到能够做到这一点的明确答案。
任何建议或帮助都非常感谢。谢谢。 - 我现在添加了我用作测试的代码,试着看看出了什么问题。这在the_action_function()在插件的基本文件中时有效,但在加载到另一个文件位置时则不起作用。如何将其移动到另一个文件?
function pob_noted_testplugin(){
wp_enqueue_script( 'my-ajax-handle', plugin_dir_url( __FILE__ ) . '/includes/1ext/js/pob_noted_form_script.js', array( 'jquery' ) );
wp_localize_script( 'my-ajax-handle', 'the_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_ajax_the_ajax_hook', 'the_action_function' );
add_action( 'wp_ajax_nopriv_the_ajax_hook', 'the_action_function' );
function the_action_function(){
$name = $_POST['name'];
echo"Note added:" . " " . $name;
wp_die();
}
.js脚本
function submit_me(){
jQuery(document).ready(function($) {
jQuery.post(the_ajax_script.ajaxurl,
jQuery("#theForm").serialize(),
function(response_from_the_action_function){
jQuery("#response_area").html(response_from_the_action_function);
}
);
$("#theForm")[0].reset();
});
}
表格
function hello_world_ajax_frontend(){
$the_form = '
<form id="theForm">
<input id="name" name="name" value = "" type="text" />
<input name="action" type="hidden" value="the_ajax_hook" />
<input id="submit_button" value = "Click This" type="button" onClick="submit_me();" />
</form>
<div id="response_area">
Response
</div>';
return $the_form;
}
add_shortcode("hw_ajax_frontend", "hello_world_ajax_frontend");
?>