以下代码位于wordpress页面。
<form action="action_page.php" method="post">
Name:<br>
<input type="text" name="name" value="Mickey">
<input type="submit" value="Submit">
</form>
action_page.php
<?php $name=$_POST['name']; echo $name;?>
我想在wordpress页面中显示输出(Mickey),而不是php页面。请帮帮我。
答案 0 :(得分:0)
您可以通过AJAX实现,请参阅codex
将此添加到functions.php
add_action( 'wp_ajax_add_foobar', 'prefix_ajax_add_foobar' );
add_action( 'wp_ajax_nopriv_add_foobar', 'prefix_ajax_add_foobar' );
function prefix_ajax_add_foobar() {
// Handle request then generate response using WP_Ajax_Response
// In a real world scenario, apply all the validation,
// sanitization and black magic to POSTed variables before using them
$name=$_POST['name'];
echo $name;
}
现在,以下内容可以放在<script>
标记内的footer.php或单独的JS文件中。
jQuery.post(
ajaxurl,
{
'action': 'add_foobar',
'data': jQuery("form").sanitize()
},
function(response){
alert('The server responded: ' + response);
}
);
在所有条件相同的情况下,您应该会看到已发布值的警报。