mercredi 25 février 2015

Form Submission with admin_url() Set as Action

I have the following form as part of a Wordpress plugin:



<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://ift.tt/1LEc3YS', '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>


The logic to process this new record is located in 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();


What I don't understand is: on submission of this form, the query is still executing cp_libhours_add_area as if I told the form to go to includes/_process.php.


Aucun commentaire:

Enregistrer un commentaire