Title: Update custom sql table width ajax
Last modified: March 4, 2017

---

# Update custom sql table width ajax

 *  [qatrik](https://wordpress.org/support/users/qatrik/)
 * (@qatrik)
 * [9 years, 3 months ago](https://wordpress.org/support/topic/update-custom-sql-table-width-ajax/)
 * Hi,
 * I’m trying to insert, update and delete rows in my custom sql table width ajax.
   I have followed different tutorials but i can’t get it to work. When I’m trying
   to update a row I only sent on to [http://www.mysite.se/?action=updateTable](http://www.mysite.se/?action=updateTable),
   and the database table is not updated. I’m also want to save a whole HTML element.
   Can someone help me and see whats wrong?
 * HTML/Javascript
 *     ```
       <script type="text/javascript">
       jQuery('#tableForm').submit(ajaxSubmit);
   
       function ajaxSubmit(){
             var content = jQuery(".table-1").html();
   
             jQuery.ajax({
             type:"POST",
             url: "/wp-admin/admin-ajax.php",
             data: {content : content},
             success:function(data){
                jQuery("#feedback").html(data);
             }
             });
   
             return false;
   
   
             }
       </script>
   
       <form type="post" action="" id="tableForm">
       <table>
         <thead>
           <tr>
             <th>Heading 1</th>
             <th>Heading 2</th>
           </tr>
         </thead>
         <tbody id="table-1" class="table-1"><tr>
             <td><textarea>Content</textarea></td>
             <td><textarea>Content 2</textarea></td>
           </tr>
         </tbody>
       </table>
       <input type="hidden" name="action" value="updateTable"/>
       <input type="submit"/>
       </form>
       <br/><br/>
       <div id="feedback"></div>
       ```
   
 * function.php
 *     ```
       <?php
   
       function updateTable(){
   
          global $wpdb;
   
          $content = $_POST['content'];
          $id = 1;
   
          if($wpdb->update(
          'table',
          array('content'=>$content),
          array('id'=>$id))===FALSE){
   
          echo "Error";
   
          }
          else {
   
          echo "Success!";
   
          }
          die();
          }
   
       add_action('wp_ajax_updateTable', 'updateTable');
       add_action('wp_ajax_nopriv_updateTable', 'updateTable');
   
       ?>
       ```
   

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 3 months ago](https://wordpress.org/support/topic/update-custom-sql-table-width-ajax/#post-8875318)
 * The main problem is your .ajax data does not have the “action” value. The value
   in the request URL does not get passed to the AJAX handler. Some other things
   to verify:
 * Is your HTML in index.html or whatever the default file is for your site? Is 
   the jQuery library properly loaded? Have you checked your browser console for
   JS errors? Checked your PHP error log? While your .ajax url is fine for your 
   particular request and a root WP install, you should always define the full URL,
   http, domain, everything. Relative requests in WP will eventually fail somewhere
   because you cannot always know what folder your WP code thinks it’s calling from.
 * You actually have a table in the DB called ‘table’ with the referenced row and
   columns I hope.
 * Following different tutorials can be risky since there’s different ways to do
   things, and they do not always mix well. I understand that there is probably 
   no single tutorial that makes total sense and you need multiple references to
   figure it all out, but at least try to consistently use code from one source.
   If you are going to work from one source, at least consider the [WP Plugin Handbook](https://developer.wordpress.org/plugins/javascript/ajax/).
 *  Thread Starter [qatrik](https://wordpress.org/support/users/qatrik/)
 * (@qatrik)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/update-custom-sql-table-width-ajax/#post-8904844)
 * Thank you for your answare! After more searching, I now understand that I had
   to put a little more code in function.php to be possible to use AJAX on the front-
   end in wordpress. I also updated my AJAX script so now I think that AJAX works
   but the database is still not updating.
 * In my AJAX script I have a value “action: ‘updateTable’ ‘for data. If I understand
   correctly “updateTable” should be the php function that I creat in function.php,
   or is it wrong? Or is there something wrong in my code?
 *     ```
       <table>
         <thead>
           <tr>
             <th>Heading 1</th>
             <th>Heading 2</th>
           </tr>
         </thead>
         <tbody id="table-1" class="table-1"><tr>
             <td><textarea>Content</textarea></td>
             <td><textarea>Content 2</textarea></td>
           </tr>
         </tbody>
       </table>
       <a class="btn" id="update">Update</a>
       ```
   
 * function.php
 *     ```
       function my_enqueue() {
   
           wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/script.js', array('jquery') );
   
           wp_localize_script( 'ajax-script', 'my_ajax_object',
                   array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
       }
       add_action( 'wp_enqueue_scripts', 'my_enqueue' );
   
       //Uppdatera databasen
       function updateTable() {
   
       	global $wpdb;
   
       	$content = $_POST['content'];
       	$id = 1;
   
       	$wpdb->update(
       	'table',
       	array(
       		'content' => content
       		),
       	array(
       		'id' => $id
       		)
       	);
   
       	wp_die();
       }
   
       add_action('wp_ajax_updateTable', 'updateTable');
       add_action('wp_ajax_nopriv_updateTable', 'updateTable');
       ```
   
 * script.js
 *     ```
       $( '#update' ).click( function() {
   
       var content = $("#table-1").html();
   
       $.ajax({
          url: my_ajax_object.ajaxurl,
          type: 'POST',
          data: {
             action: 'updateTable',
             content: content
             },
          success: function( response ){
                console.log("success");
          },
          error: function ( error ) {
             console.log( error );
          }
       });
   
       });
       ```
   
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/update-custom-sql-table-width-ajax/#post-8905981)
 * The action value defines what wp_ajax_?? and wp_ajax_nopriv_?? action hooks you
   add to, which you’ve done correctly. You can call the function anything you want,
   as long as it’s not used elsewhere.
 * There’s a slight problem with how you’ve setup your $wpdb->update() call. You
   are missing the $ for content. `'content' => $content`
 * AFAICT that should get things working. If not, try adding something like `error_log('
   My AJAX callback executed!');` to your AJAX handler. Click the Update link, then
   check your error log. If your message is there, at least you know the AJAX part
   is working and there is some minor error with the DB update part. Go over all
   the parameters, you missed something minor, as it appears OK to me. Things like
   is there really a row with id = 1 to update?
 *  Thread Starter [qatrik](https://wordpress.org/support/users/qatrik/)
 * (@qatrik)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/update-custom-sql-table-width-ajax/#post-8917086)
 * I switched to `content => $ content` and I have also added `error_log` on top
   of my Ajax handler `updateTable ()` but it still doesn’t work and my error log
   file is empty. This means that there is still something wrong with my Ajax?
 * If I write `success: function(data) { console.log( data) }` I get my entire site
   and not the ‘content’. Is this right?
 * My ajax function is inside js/script.js. Ajax handler and php are inside function.
   php and my update button and the HTML table I want to save is in a custom_template.
   php. Does this mean that I have to do something different?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/update-custom-sql-table-width-ajax/#post-8919163)
 * You cannot have a space after the $. `content => $content`
 * If you placed the entire `error_log('My AJAX callback executed!');` and not just`
   error_log`, then your AJAX request is not even reaching the server.
 * Check the HTML source in your browser and confirm the path to script.js is correct.
   If you click the link, the file content should display. Also confirm the the 
   jquery file is loaded before script.js.
 * Try `console.log(my_ajax_object.ajaxurl);` to confirm that URL is correct.
 * While it shouldn’t be a problem, camelCase form is unusual in PHP. Try changing
   all instances of updateTable to update_table in your PHP code, plus the action:
   value in your jQuery.ajax() call.
 * Your AJAX handler is not returning any data by design, so logging it on success
   should just show up as null or empty. The fact you are getting a bunch of data
   makes me think there is another updateTable callback in your system. Changing
   to update_table will fix that.
 * And you do have a table called ‘table’ and it has at least id and content columns,
   right? And one of the records there has a 1 in the id column, correct?
 * Be sure your PHP code is in function**s**.php, not function.php. Note the ‘s’.
   Just a typo I suspect, but please verify.
 * That’s all I can think of on things that could be wrong. Hopefully one of these
   solves it for you.
 *  Thread Starter [qatrik](https://wordpress.org/support/users/qatrik/)
 * (@qatrik)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/update-custom-sql-table-width-ajax/#post-8966906)
 * Now it works. I had written my_ajax_object.ajaxurl but it should be my_ajax_object.
   ajax_url. Thanks so much for all your help!
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/update-custom-sql-table-width-ajax/#post-8967874)
 * You’re welcome! ajax_url, of course! I should have seen that 🙁 Oh well, I’m 
   glad you finally noticed the glitch.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Update custom sql table width ajax’ is closed to new replies.

## Tags

 * [ajax](https://wordpress.org/support/topic-tag/ajax/)
 * [sql](https://wordpress.org/support/topic-tag/sql/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 8 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [9 years, 2 months ago](https://wordpress.org/support/topic/update-custom-sql-table-width-ajax/#post-8967874)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
