dousu1900 2018-12-06 02:08
浏览 41

从循环内的用户列表更新单个值

I need to update the "display_name" in the database, for that I display all of the users using a wordpress function and a foreach, and add next to each user a textbox(to change the name) and a button (to perform the action), my question is how can I get the correct ID for the input and button that I am clicking, if Im looping the users?

<?php global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM $wpdb->users" );
?>
<form>
<?php
foreach ($results as $key ) {
  echo "$key->ID . $key->display_name <input type='text' name=''> <button>Submit</button>
 }
?>
</form> 

Im currently trapped in this, i get the id but the value of the first textbox

<?php  
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM $wpdb->users" );
?><form id="form1"><?php
foreach ($results as $key ) {
  echo "$key->ID . $key->display_name . $key->mg_nobility <input type='text' 
id='edit_value' class='$key->ID' value=''> <button row_id='$key- 
>ID'class='edit_nov'>Submit</button>";
}
  ?>

</form>
<input type='hidden'  id='row_del_id' value=''>
<input type='hidden'  id='row_up_id' value=''>
<script>
jQuery('.edit_nov').click(function()
{
    var current_id = jQuery(this).attr('row_id');
    var current_value =jQuery('#edit_value').val();
    jQuery('#row_del_id').val(current_id);
    jQuery('#row_up_id').val(current_value);


return false;
});
</script>
  • 写回答

1条回答 默认 最新

  • doushi7314 2018-12-06 05:17
    关注

    You must have unique id values or you will only get the first one, try this:

    <?php  
    global $wpdb;
    $results = $wpdb->get_results( "SELECT * FROM $wpdb->users" )
    ?>
    
    <form id="form1">
        <?php foreach($results as $key): ?>
            <?php echo $key->ID.$key->display_name.$key->mg_nobility ?>
            <!-- Append the id to the end of this -->
            <input type='text' id='edit-value-<?php echo $key->ID ?>' value='' /> 
            <!-- Use the data attr instead -->
            <button data-rowid='<?php echo $key->ID ?>' class='edit_nov'>Submit</button>
        <?php endforeach ?>
    </form>
    <!-- I don't understand the function of these since they are outside the form -->
    <input type='hidden'  id='row_del_id' value=''>
    <input type='hidden'  id='row_up_id' value=''>
    
    <script>
    jQuery(function($) {
        $(this).on('click', '.edit_nov', function() {
            // I would use the native method of .data()
            var current_id = $(this).data('rowid');
            // Append id and get value
            var current_value = $('#edit-value-'+current_id).val();
    
            $('#row_del_id').val(current_id);
            $('#row_up_id').val(current_value);
        });
    });
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等