2015-10-21 228 views
0

我想我需要在這裏建議。我不知道我是否以正確的方式做這件事。所以我有從數據庫中提取的這個輸入字段值(選項表)。根據輸入字段更改輸入字段值php

<!-- input to get amount of people --> 
      <tr> 
       <th scope="row"><?php _e('How many people do you want to add?', 'keeping-points'); ?></th> 
       <td> 
        <input type="text" size="15" name="posk_options[ppl_amount]" value="<?php _e($options['ppl_amount'], 'keeping-points'); ?>" /> 
       </td> 
      </tr> 

      <?php dlaugh_ppl_names(); ?> 

用戶類型有多少,他們希望有輸入字段和下面的函數(dlaugh_ppl_names)用戶將創建的形式提交的輸入字段。它通過一個for循環來創建字段的數量。

function dlaugh_ppl_names() { 

    $options = get_option('posk_options'); 
    $name_num = $options['ppl_amount']; 
    for($i=0;$i < $name_num; $i++) { 

     $point = "txt_" . $i; 
     $name_build = $point . "_name"; 
     $point_total = $point . "_point_total"; 
     $name_print = $options[$name_build]; 
     $changed_name = str_replace(' ', '_', $name_print); 
     $point_build = "point_total_" . $changed_name; 

     if ($options[$name_build] == "") { 
      $set_points = $name_num - $i; 
      echo $i; 
      $final_ppl = $name_num - $set_points; 
      $options['ppl_amount'] = $final_ppl; 
      $options[$name_build] = null; 
      if ($options[$name_build] == null) { 
       $options[$point_total] = null; 
      } 
      $options = array_filter($options); 
      update_option('posk_options', $options); 
      break; 
     } 

     if ($options[$name_build] == true) { 

     ?> 
        <!-- Add Person Name --> 
        <tr> 
         <th scope="row"> 
         <?php if ($options[$name_build] == "") { 
          _e('Person ' . $i + 1, 'keeping-points'); ?> 
          <?php } else 
          _e($options[$name_build], 'keeping-points'); 

          ?> 
          </th> 
         <td> 
          <input type="text" size="20" name="posk_options[<?php echo $name_build ?>]" value="<?php _e($options[$name_build], 'keeping-points'); ?>" /> 
          <input type="text" size="10" name="posk_options[<?php echo $point_build; ?>]" value="<?php _e($options[$point_build], 'keeping-points'); ?>" /> 
         </td> 
        </tr> 
     <?php 
     } 
    } 

     for($i=0;$i < $name_num; $i++) { 

     $point = "txt_" . $i; 
     $name_build = $point . "_name"; 
     $point_total = $point . "_point_total"; 
     $name_print = $options[$name_build]; 
     $changed_name = str_replace(' ', '_', $name_print); 
     $point_build = "point_total_" . $changed_name; 

     if ($options[$name_build] == false && $options[$name_build] == null) { 

     ?> 
        <!-- Add Person Name --> 
        <tr> 
         <th scope="row"><?php _e('Person' . ' ' . ($i + 1), 'keeping-points'); ?></th> 
         <td> 
          <input type="text" size="20" name="posk_options[<?php echo $name_build ?>]" value="<?php _e($options[$name_build], 'keeping-points'); ?>" /> 
          <input type="hidden" name="posk_options[<?php echo "point_total_" . $name_print ?>]" value="<?php _e($options[$point_build], 'keeping-points'); ?>" /> 
         </td> 
        </tr> 
     <?php 
     } 

    } 


} 

創建字段後,輸入控件ppl_amount將顯示用戶創建了多少個字段。 (可以說類型15,按下提交,字段被創建,然後15仍然存在)。我希望15在那裏,但在提交時,我希望15根據實際填寫的名稱的多少個輸入字段進行更改。假設他們輸入了15 ...然後只填寫了5,那麼15值就會變成5。代碼我有工作,但我的問題是價值15重新發布作爲人選擇的號碼,但如果我刷新頁面然後字段將按我的預期工作。所以我希望根據dlaugh_ppl_names()函數填充的輸入數量來更改ppl_amount。

希望它不是太混亂。感謝您的任何建議!我想在每個輸入字段被填寫後有一個Ajax調用來刷新頁面,但這真的很煩人。我填充就像有一個簡單的答案,我只是想念。

回答

0

我發現的最佳解決方案是向人物輸入添加一個類,然後計算填充了多少個輸入字段,然後將該值與輸入字段中的問題進行比較。這樣,它會根據填充的輸入數量動態更新該字段值。我將很快發佈一個鏈接到完成的WordPress插件,以防萬一您想要完整的演示。

使用Ajax

jQuery(document).ready(function($){ 
     $('.people').blur(function() { 
     var data = { 
        'action': 'points_action' 
       }; 

       // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php 
       $.post(ajaxurl, data, function(response) { 
       var searchCount = $('.people').filter(function(){ 
        return $(this).val(); 
       }).length; 
       $('#ppl_amount').val(searchCount); 
       }); 
     }); 

});