database - getting the values of hidden inputs to use them in a php mysql query

admin2025-06-05  0

Im trying to update a value in the database, to do this I pass the values of the input an ID to another hidden inputs and submit the form via jquery, the values are set in the hidden input (tested). But I cant get them to pass to PHP and perform the query, what Im doing wrong?

  <?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><br>
      <?php endforeach ?>
  </form>

  <input type='hidden' name='del_id' id='row_del_id' value=''>
  <input type='hidden' name='up_id' id='row_up_id' value=''>

  <script>
  jQuery('.edit_nov').click(function($) {
      var current_id = jQuery(this).data('rowid');
      var current_value = jQuery('#edit-value-'+current_id).val();
      jQuery('#row_del_id').val(current_id);
      jQuery('#row_up_id').val(current_value);
      jQuery( "#form1" ).submit();
      return false;
  });
  </script>

  <?php 
  if(isset($_POST['del_id']))
  {
    $id = $_POST['del_id'];
    $nobility = $_POST['up_id'];
    global $wpdb;  
    $wpdb->query($wpdb->prepare("UPDATE $wpdb->users SET mg_nobility='$nobility' WHERE ID='$id'"));
  }
  ?>

Im trying to update a value in the database, to do this I pass the values of the input an ID to another hidden inputs and submit the form via jquery, the values are set in the hidden input (tested). But I cant get them to pass to PHP and perform the query, what Im doing wrong?

  <?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><br>
      <?php endforeach ?>
  </form>

  <input type='hidden' name='del_id' id='row_del_id' value=''>
  <input type='hidden' name='up_id' id='row_up_id' value=''>

  <script>
  jQuery('.edit_nov').click(function($) {
      var current_id = jQuery(this).data('rowid');
      var current_value = jQuery('#edit-value-'+current_id).val();
      jQuery('#row_del_id').val(current_id);
      jQuery('#row_up_id').val(current_value);
      jQuery( "#form1" ).submit();
      return false;
  });
  </script>

  <?php 
  if(isset($_POST['del_id']))
  {
    $id = $_POST['del_id'];
    $nobility = $_POST['up_id'];
    global $wpdb;  
    $wpdb->query($wpdb->prepare("UPDATE $wpdb->users SET mg_nobility='$nobility' WHERE ID='$id'"));
  }
  ?>
Share Improve this question asked Dec 6, 2018 at 13:16 Mefistofeles Mefistofeles 51 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Please add method="post" to form like

    <form id="form1" method="post">
          <?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><br>
          <?php endforeach ?>
  <input type='hidden' name='del_id' id='row_del_id' value=''>
  <input type='hidden' name='up_id' id='row_up_id' value=''>
      </form>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749123910a316566.html

最新回复(0)