• Resolved mortenamdk

    (@mortenamdk)


    Hi

    I have a problem with Ajax for update my data in database.
    I get the right data in my ajax call, but nothing happening.

    Her is my code for call my update php function:

    global $wpdb;
      $table_name = $wpdb->prefix . 'topbanner';
    
        $outPut = $wpdb->get_results(
      "
      SELECT *
      FROM $table_name
      "
    );
    
      foreach ( $outPut as $(Hidden)_top_banner ) { ?>
    ?>
    
    <script>
    (function($) {
     $(document).on('click', '#submit_update<?php echo $(Hidden)_top_banner->id; ?>', function () {
       var (Hidden)_top_banner_name = tinymce.get('(Hidden)_top_banner_name<?php echo $(Hidden)_top_banner->id; ?>').getContent();
       var (Hidden)_top_banner_name_color = $('#(Hidden)_top_banner_name_color<?php echo $(Hidden)_top_banner->id; ?>').val();
       var (Hidden)_top_banner_name_top_text_line = $('#(Hidden)_top_banner_name_top_text_line<?php echo $(Hidden)_top_banner->id; ?>').val();
       var (Hidden)_top_banner_name_top_height = $('#(Hidden)_top_banner_name_top_height<?php echo $(Hidden)_top_banner->id; ?>').val();
       var (Hidden)_top_banner_name_top_start_date = $('#(Hidden)_top_banner_name_top_start_date<?php echo $(Hidden)_top_banner->id; ?>').val();
       var (Hidden)_top_banner_name_top_stop_date = $('#(Hidden)_top_banner_name_top_stop_date<?php echo $(Hidden)_top_banner->id; ?>').val();
       var (Hidden)_top_banner_name_media = $('#(Hidden)_top_banner_name_media<?php echo $(Hidden)_top_banner->id; ?>').val();
       var (Hidden)_image_attachment_id = $('#(Hidden)_image_attachment_id<?php echo $(Hidden)_top_banner->id; ?>').val();
    
           var data = {
           (Hidden)_top_banner_name : (Hidden)_top_banner_name,
           (Hidden)_top_banner_name_color : (Hidden)_top_banner_name_color,
           (Hidden)_top_banner_name_top_text_line : (Hidden)_top_banner_name_top_text_line,
           (Hidden)_top_banner_name_top_height : (Hidden)_top_banner_name_top_height,
           (Hidden)_top_banner_name_top_start_date : (Hidden)_top_banner_name_top_start_date,
           (Hidden)_top_banner_name_top_stop_date : (Hidden)_top_banner_name_top_stop_date,
           (Hidden)_top_banner_name_media : (Hidden)_top_banner_name_media,
           (Hidden)_image_attachment_id : (Hidden)_image_attachment_id
           };
    $.ajax({
        type: 'post',
        url: '<?php echo plugin_dir_url( __FILE__ ) . 'ajax_update.php'; ?>',
        data: data,
        success : function( response ){
            $('body').fadeTo(1000, '0.4');
        },
        complete : function( response ) {
            location.reload()
          console.log(data); },
          error:function(response){
    alert("Fejl");
                }
    
    });
    
          });
    })(jQuery);
    </script>
    <?php
    }
    

    My php function to $_POST update:

    
    define( 'WP_USE_THEMES', false );
    
    require_once( '../../../wp-load.php' );
    global $wpdb;
      $table_name = $wpdb->prefix . 'topbanner';
    
        $outPut = $wpdb->get_results(
      "
      SELECT *
      FROM $table_name
      "
    );
      foreach ( $outPut as $(Hidden)_top_banner ) {
    
        $(Hidden)_top_banner_name                  = wp_kses_post($_POST['(Hidden)_top_banner_name'.$(Hidden)_top_banner->id'']);
        $(Hidden)_top_banner_name_color            = sanitize_hex_color($_POST['(Hidden)_top_banner_name_color'.$(Hidden)_top_banner->id'']);
        $(Hidden)_top_banner_name_top_text_line    = sanitize_text_field($_POST['(Hidden)_top_banner_name_top_text_line'.$(Hidden)_top_banner->id'']);
        $(Hidden)_top_banner_name_top_height       = sanitize_text_field($_POST['(Hidden)_top_banner_name_top_height'.$(Hidden)_top_banner->id'']);
        $(Hidden)_top_banner_name_top_start_date   = sanitize_text_field($_POST['(Hidden)_top_banner_name_top_start_date'.$(Hidden)_top_banner->id'']);
        $(Hidden)_top_banner_name_top_stop_date    = sanitize_text_field($_POST['(Hidden)_top_banner_name_top_stop_date'.$(Hidden)_top_banner->id'');
        $(Hidden)_top_banner_name_media            = sanitize_text_field($_POST['(Hidden)_top_banner_name_media'.$(Hidden)_top_banner->id'']);
        $(Hidden)_image_attachment_id              = sanitize_text_field($_POST['(Hidden)_image_attachment_id'.$(Hidden)_top_banner->id'']);
    
    $wpdb->update(
      $table_name,
      array(
            '(Hidden)_top_banner_name'                   => $(Hidden)_top_banner_name,
            '(Hidden)_top_banner_name_color'             => $(Hidden)_top_banner_name_color,
            '(Hidden)_top_banner_name_top_text_line'     => $(Hidden)_top_banner_name_top_text_line,
            '(Hidden)_top_banner_name_top_height'        => $(Hidden)_top_banner_name_top_height,
            '(Hidden)_top_banner_name_top_start_date'    => $(Hidden)_top_banner_name_top_start_date,
            '(Hidden)_top_banner_name_top_stop_date'     => $(Hidden)_top_banner_name_top_stop_date,
            '(Hidden)_top_banner_name_media'             => $(Hidden)_top_banner_name_media,
            '(Hidden)_image_attachment_id'               => $(Hidden)_image_attachment_id,
      ),
      array( 'id' => $(Hidden)_top_banner->id ),
      array(
        '%s',
        '%s',
            '%s',
            '%s',
            '%s',
            '%s',
            '%s',
            '%d',
      ),
      array( '%d' )
    );
    }

    I have done the right thin with my ajax_insert.php there does it work, just not when i try to update.

    Best Regards
    Morten

    The page I need help with: [log in to see the link]

Viewing 6 replies - 16 through 21 (of 21 total)
  • mortenamdk

    (@mortenamdk)

    Hi

    I did it, it works now.
    Pick up some example wordpress ajax code: https://wptheming.com/2013/07/simple-ajax-example/

    Maby are there oder there need the same.

    Best Regards
    Morten

    mortenamdk

    (@mortenamdk)

    But i have a problem with ajax Update, my ajax response as it should and show the new values i request. But it dont get to the Database

    function ajax_(hidden)_update_top_banner() {
        global $wpdb;
          $table_name = $wpdb->prefix . "topbanner";
          $outPut = $wpdb->get_results(
        "
        SELECT *
        FROM $table_name
        "
        );
    
        foreach ( $outPut as $(hidden)_top_banner ) {
          if ( isset($_REQUEST['submit_update'.$(hidden)_top_banner->id])) {
    
          $(hidden)_top_banner_name                  = wp_kses_post($_POST['(hidden)_top_banner_name'.$(hidden)_top_banner->id]);
          $(hidden)_top_banner_name_color            = sanitize_hex_color($_POST['(hidden)_top_banner_name_color'.$(hidden)_top_banner->id]);
          $(hidden)_top_banner_name_top_text_line    = sanitize_text_field($_POST['(hidden)_top_banner_name_top_text_line'.$(hidden)_top_banner->id]);
          $(hidden)_top_banner_name_top_height       = sanitize_text_field($_POST['(hidden)_top_banner_name_top_height'.$(hidden)_top_banner->id]);
          $(hidden)_top_banner_name_top_start_date   = sanitize_text_field($_POST['(hidden)_top_banner_name_top_start_date'.$(hidden)_top_banner->id]);
          $(hidden)_top_banner_name_top_stop_date    = sanitize_text_field($_POST['(hidden)_top_banner_name_top_stop_date'.$(hidden)_top_banner->id]);
          $(hidden)_top_banner_name_media            = sanitize_text_field($_POST['(hidden)_top_banner_name_media'.$(hidden)_top_banner->id]);
          $(hidden)_image_attachment_id              = sanitize_text_field($_POST['(hidden)_image_attachment_id'.$(hidden)_top_banner->id]);
    
        $wpdb->update(
        $table_name,
        array(
          '(hidden)_top_banner_name'                   => $(hidden)_top_banner_name,
          '(hidden)_top_banner_name_color'             => $(hidden)_top_banner_name_color,
              '(hidden)_top_banner_name_top_text_line'     => $(hidden)_top_banner_name_top_text_line,
              '(hidden)_top_banner_name_top_height'        => $(hidden)_top_banner_name_top_height,
              '(hidden)_top_banner_name_top_start_date'    => $(hidden)_top_banner_name_top_start_date,
              '(hidden)_top_banner_name_top_stop_date'     => $(hidden)_top_banner_name_top_stop_date,
              '(hidden)_top_banner_name_media'             => $(hidden)_top_banner_name_media,
              '(hidden)_image_attachment_id'               => $(hidden)_image_attachment_id,
        ),
        array( 'id' => $(hidden)_top_banner->id ),
        array(
          '%s',
          '%s',
              '%s',
              '%s',
              '%s',
              '%s',
              '%s',
              '%d',
        ),
        array( '%d' )
        );
          // The $_REQUEST contains all the data sent via ajax
        }
          }
        print_r($_REQUEST);
          // Always die in functions echoing ajax content
         die();
    }
    
      add_action( 'wp_ajax_ajax_(hidden)_update_top_banner', 'ajax_(hidden)_update_top_banner' );

    Best regards
    Morten

    Moderator bcworkz

    (@bcworkz)

    Take that INSERT query string that was output by directly calling the function and try executing it in phpMyAdmin’s SQL tab after you’ve navigated to your WP DB. It should give you a better idea about what it doesn’t like about the query. It looks OK to me, but phpMyAdmin is much more discerning.

    Isn’t it odd that the name field is empty? Or is it just because of the way you had to execute the function to see any output? I think the output to console failed because of quotes in the query string. The string probably needs to be serialized or converted to JSON before outputting to the browser.

    mortenamdk

    (@mortenamdk)

    Hi

    I have found the bug, it is my id i not can get.
    Here is the problem:

      function ajax_(hidden)_update_top_banner() {
        global $wpdb;
          $table_name = $wpdb->prefix . "topbanner";
    
          if ( isset($_REQUEST)) {
    
          $(hidden)_top_banner_name                  = wp_kses_post($_REQUEST['(hidden)_top_banner_name']);
          $(hidden)_top_banner_name_color            = sanitize_hex_color($_REQUEST['(hidden)_top_banner_name_color']);
          $(hidden)_top_banner_name_top_text_line    = sanitize_text_field($_REQUEST['(hidden)_top_banner_name_top_text_line']);
          $(hidden)_top_banner_name_top_height       = sanitize_text_field($_REQUEST['(hidden)_top_banner_name_top_height']);
          $(hidden)_top_banner_name_top_start_date   = sanitize_text_field($_REQUEST['(hidden)_top_banner_name_top_start_date']);
          $(hidden)_top_banner_name_top_stop_date    = sanitize_text_field($_REQUEST['(hidden)_top_banner_name_top_stop_date']);
          $(hidden)_top_banner_name_media            = sanitize_text_field($_REQUEST['(hidden)_top_banner_name_media']);
          $(hidden)_image_attachment_id              = sanitize_text_field($_REQUEST['(hidden)_image_attachment_id']);
        
        $wpdb->update(
        $table_name,
        array(
          '(hidden)_top_banner_name'                   => $(hidden)_top_banner_name,
          '(hidden)_top_banner_name_color'             => $(hidden)_top_banner_name_color,
              '(hidden)_top_banner_name_top_text_line'     => $(hidden)_top_banner_name_top_text_line,
              '(hidden)_top_banner_name_top_height'        => $(hidden)_top_banner_name_top_height,
              '(hidden)_top_banner_name_top_start_date'    => $(hidden)_top_banner_name_top_start_date,
              '(hidden)_top_banner_name_top_stop_date'     => $(hidden)_top_banner_name_top_stop_date,
              '(hidden)_top_banner_name_media'             => $(hidden)_top_banner_name_media,
              '(hidden)_image_attachment_id'               => $(hidden)_image_attachment_id,
        ),
    
        array( 'id' => $outPut ),
        array(
          '%s',
          '%s',
              '%s',
              '%s',
              '%s',
              '%s',
              '%s',
              '%d',
        ),
        array( '%d' )
        );
        }
    
          // The $_REQUEST contains all the data sent via ajax
        echo $wpdb->last_query;
          // Always die in functions echoing ajax content
         die();
    }
    
      add_action( 'wp_ajax_ajax_(hidden)_update_top_banner', 'ajax_(hidden)_update_top_banner' );

    Can i somehow do this:
    $_REQUEST[‘id’] = $id;
    $outPut = $wpdb->get_results( $wpdb->prepare( “SELECT * FROM $table_name WHERE id = %d”, $id ) );

    Have a hidden input with id.
    I hoped i cut use it to get id from call from my form.

    Best regards
    Morten

    Hi
    It all works now.
    Insert, update and delete in ajax.
    Now its time secure the plugin.
    Have done database ok.
    So is it my insert update and delete I need to work with.

    If you more good method I will be happy.

    And thanks for all you help and time.

    Best regards
    Morten

    Thanks for your help 🙂
    I close this question for now 🙂

    Best Regards
    Morten

Viewing 6 replies - 16 through 21 (of 21 total)
  • You must be logged in to reply to this topic.