使用admin_url()的表单提交设置为操作

时间:2015-02-25 19:42:01

标签: forms wordpress

我将以下表格作为Wordpress插件的一部分:

<form method="post" action="<?php echo admin_url(); ?>">
    Area Name: <input type="text" name="new_area_name" placeholder="<?php esc_html_e('New Area Name', 'libhours'); ?>" required><br>
    URL (optional): <input type="text" name="new_area_url" placeholder="<?php esc_html_e('http://emich.edu/library/newplace', 'libhours'); ?>"><br>
    Note (optional): <textarea rows="3" name="new_area_note"></textarea><br>
    <input type="hidden" name="action" value="add_area">
    <input type="submit" value="Add Area">
</form>

处理此新记录的逻辑位于libhours / includes / _process.php

function cp_libhours_add_area() {
global $wpdb, $table_prefix;

$new_area = sanitize_text_field( $_POST['new_area_name'] );
$new_url = sanitize_text_field( $_POST['new_area_url'] );
$new_note = sanitize_text_field( $_POST['new_area_note'] );

if($wpdb->insert($table_prefix . library_areas, array(
    'location' => $new_area,
    'displayOrder' => 0,
    'url' => $new_url,
    'note' => $new_note
    ))){
    $_SESSION['usermsg'] = '<p class="user-success">The new library area was added.</p>';
} else {
    $_SESSION['usermsg'] = '<p class="user-warning">There was an error adding the new library area. Please try again.</p>'; 
}
header("Location: " . $_SERVER['HTTP_REFERER']);
exit;

}

if($_POST['action'] == 'add_area') cp_libhours_add_area();

我不明白的是:在提交此表单时,查询仍在执行cp_libhours_add_area,就好像我告诉表单转到include / _process.php。

1 个答案:

答案 0 :(得分:0)

表单提交事件上可能存在某种javascript侦听器,它会阻止默认操作并将其重定向到其他URL。

在Chrome开发者工具中,如果您检查表单,然后查看由当前所选元素过滤的事件侦听器,您可以找到它发生的位置并适当地编辑javascript。