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'"));
  }
  ?>
New contributor
Mefistofeles is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
up vote 0 down vote accepted

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>
  • And also put <input type='hidden' name='del_id' id='row_del_id' value=''> <input type='hidden' name='up_id' id='row_up_id' value=''> this into the form. – Pratik Patel Dec 6 at 13:28

Your Answer

Mefistofeles is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.