I’m developing a WordPress plugin. There are multiple form elements, and for some reason, each time I click the submit button on one form, it tells another form to submit. This is inconvenient, because when I click the button to save new information, the other form deletes the first row from the database.
Javascript:
//This is the Javascript that controls the remove all form.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('tr.assoc_row').show();
$('#settings-removed-msg').hide();
$('#formdeleteassoc').submit(function(e){
e.preventDefault(); //Works to prevent normal submission of the form.
$.ajax ({
type: 'POST',
url: '',
data: {remove_all: ''
},
success: function() {
$('#settings-removed-msg').fadeIn('fast'); //Working now
$('tr.assoc_row').fadeOut('fast'); //Working now
}
});
});
});
</script>
HTML
<!-- This the the HTML and PHP that renders the options page in WordPress. -->
<div class="wrap">
<?php screen_icon('plugins'); ?>
<h2>Tag to Category Associator</h2>
<div id="settings-removed-msg" class="updated"><p>Associations were successfully removed.</p></div>
<form action="<?php echo $_SERVER['PHP_SELF'].'?page=tag2cat-associator'; ?>" method="post">
<?php
settings_fields('cb_t2c_options');
do_settings_sections('tag2cat-associator');
?>
<input name="Submit" type="submit" value="Save Changes" />
</form></div>
PHP
//These are the form elements.
//Show the buttons to remove all associations and remove a single association.
echo '<table>';
echo '<tr>';
echo '<td></strong>Remove existing associations</strong></td>';
echo "<td><form action='" . $_SERVER['PHP_SELF'] . "?page=tag2cat-associator' method='post'>";
echo "<select name='remove_single' id='removeSingle' class='remove-single' >";
foreach ($cb_t2c_show_associations as $tags) {
echo "<option value = '".$tags->assoc_ID."'>".$tags->assoc_ID."</option>";
}
echo '</select>';
echo " <input type = 'submit' name='submit-remove' value='Remove'></input>";
echo '</form></td></tr>';
echo '<tr>';
echo '<td>Remove all (Will delete existing associations)</td>';
echo "<td><form action='" . $_SERVER['PHP_SELF'] . "?page=tag2cat-associator' id='formdeleteassoc' method='post'>";
echo "<input name='remove_all' id='removeAll' class='remove-all' type='submit' value='Remove All'></input></form></td></tr>";
echo '</table>';
//The if's alter the database if the right $_POST information occurs.
if ( isset( $_REQUEST['remove_all'] ) ){
$wpdb->query("DELETE FROM ".$prefix."cb_tags2cats");
}
if ( isset( $_REQUEST['remove_single'] ) ) {
$remove_assoc = $_REQUEST['remove_single'];
$wpdb->query("DELETE FROM ".$prefix."cb_tags2cats WHERE assoc_ID = " . $remove_assoc);
}
The problem was due to having 2 settings_fields as registered settings in WordPress. The basic structure was:
What I wanted to accomplish with WordPress was to have a section outside the form, which would show the user the currently selected options in a simple HTML table. Therefore, I deleted the second ‘add_settings_field’ function, took the contents of its callback function and put it at the end of the function that draws the options page. E.g.,
This does not yet take care of the SQL injection vulnerability, but it does explain why an unintended value was getting passed to the form.