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



我需要更新数据库中的"display_name",为此,我使用wordpress函数和foreach显示所有用户,并在每个用户旁边添加一个文本框(用于更改名称(和一个按钮(用于执行操作(,我的问题是,如果我循环用户,我如何获得正确的输入ID和我正在单击的按钮?

<?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> 

我目前陷入了困境,我得到了id,但第一个文本框的值

<?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>

您必须有唯一的id值,否则您将只得到第一个,请尝试以下操作:

<?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>

最新更新