2012-02-20 4 views
0

私はWordpressプラグインを開発しています。複数のフォーム要素があり、何らかの理由で、あるフォームの[送信]ボタンをクリックするたびに、別のフォームが送信されるように指示します。これは、ボタンをクリックして新しい情報を保存すると、もう1つのフォームがデータベースから最初の行を削除するため、不便です。

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 "&nbsp;<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); 
     } 
+0

問題はなんですか? – SLaks

+1

あなたはSQLインジェクションの脆弱性があります。 – SLaks

+0

質問をより明確に指定できますか? – gideon

答えて

0

問題は2 settings_を有することによるものでしたフィールドをWordpressの登録設定として使用します。基本的な構造は:

function cb_t2c_admin_init() { 
register_setting('cb_t2c_options', 'cb_t2c_options' /*'cv_t2c_validate_options'*/); 

    //Define sections and settings 
    add_settings_section(
     //This defined the settings section. 
     ); 

    add_settings_field(
     //This defined the first setting, with a call to a function. 
     ); 

    add_settings_field(
     //This defined the unwanted setting. 
    ); 

} 

私はWordpressので達成したいどのようなユーザーに簡単なHTMLテーブルで現在選択されているオプションを表示思われる、フォームの外側部分を持っていることでした。したがって、私は2番目の 'add_settings_field'関数を削除し、そのコールバック関数の内容を取り出し、オプションページを描画する関数の最後に置きました。例えば、

//Draw the options page 
function cb_t2c_plugin_options_page() { 
    $cb_t2c_remove_all_url = plugin_dir_url(_FILE_) . 'remove_all.php'; 
?> 

    <div class="wrap"> 
    <?php screen_icon('plugins'); ?> 
    <h2>Title</h2> 
    <div id="new-assoc-msg" class="updated"><p>Data was successfully added.</p></div> 
    <form id="formsavesettings" name="save_settings" action="<?php echo $_SERVER['PHP_SELF'].'?page=the_options_page'; ?>" method="post"> 

    <?php 
    settings_fields('cb_t2c_options'); 
    do_settings_sections('section_name'); 
    ?> 

    <input name="Submit" type="submit" value="Save Changes" /> 
    </form></div> 
    <?php 

// Here is where I added the PHP code to show my table full of already selected values. 
// This is accomplished with 1 settings section and 1 settings field. 
// By previously putting the code in a settings field, I was telling Wordpress to pass along the values to the form. 
}; 

は、これはまだ、SQLインジェクションの脆弱性の世話をしない、それが意図しない値がフォームに渡さなった理由を説明しません。