Results 1 to 4 of 4

Thread: ajax in plugin

  1. #1
    surferbloggy is offline Hello World
    Join Date
    May 2010
    Posts
    45

    Icon5 ajax in plugin

    hi! I've added ajax to wordpress on the viewer-facing side it is a form filled by ajax when is entered email and password it worked, now it doesn't work anymore even if I use the originally code from wp codex:

    Code:
    function myplugin_js_header() // this is a PHP function
    {
      // use JavaScript SACK library for Ajax
      wp_print_scripts( array( 'sack' ));
    
      // Define custom JavaScript function
    ?>
    <script type="text/javascript">
    //<![CDATA[
    
       function myplugin_cast_vote( vote_field,vote2_field, results_div )
    {
           alert("hello");
       var mysack = new sack( 
           "<?php bloginfo( 'wpurl' ); ?>/wp-admin/admin-ajax.php" );    
      
      mysack.execute = 1;
      mysack.method = 'POST';
      mysack.setVar( "action", "my_special_action" );
      mysack.setVar( "vote", vote_field.value );
      mysack.setVar( "results_div_id", results_div );
      mysack.onError = function() { alert('Ajax error in voting' )};
      mysack.runAJAX();
      
      return true;
    
    } // end of JavaScript function myplugin_cast_vote
               
               
               
    //]]>
    </script>
    <?php
    } // end of PHP function myplugin_js_header
        
        
    
    function my_action_callback() {
        // Check request came from valid source here
        // TO DO!
        
        // read submitted information
        
        $vote = $_POST['vote'];
        $results_id = $_POST['results_div_id'];
        
        // Put your vote processing code here
        // In this example, assume 
        //  a) The processing code sets global
        // variable $error to a message if there is an error
        //  b) If there is no error, $results contains
        // the HTML to put into the results DIV on the screen
        // Here you can even access the database by using the
        // global $wpdb
        $error = "";
        $results = "HTML to put into the results DIV on the screen";
        
        if( $error ) {
           die( "alert('$error')" );
        } 
        
        // Compose JavaScript for return
        die( "document.getElementById('$results_id').innerHTML = '$results'" );
    }
    and

    Code:
    add_action('wp_ajax_my_special_action', array(&$this, 'my_action_callback'));
                add_action('wp_ajax_nopriv_my_special_action', array(&$this, 'my_action_callback'));
    it worked beforer after that i write the ajax code for admin side, i include it in the main php file of the plugin do you think

    they are in conflict?? if i put an alert the alert work but ajax doesn't work anymore
    it' called here: onchange="javascript:myplugin_cast_vote(this.form. email,this.form.pass,'emailmsg');"
    could you help me?? thank you

  2. #2
    surferbloggy is offline Hello World
    Join Date
    May 2010
    Posts
    45

    Default

    now it works again, thank you bye!!

  3. #3
    surferbloggy is offline Hello World
    Join Date
    May 2010
    Posts
    45

    Default

    it has happened again ajax only works when i comment these lines:
    Code:
    // Load backend libraries
    		if ( is_admin() ) {	
    			
        		
    			require_once (dirname (__FILE__) . '/admin/admin.php');
    			$this->kdcAdminPanel = new kdcAdminPanel();
    			include_once (dirname (__FILE__) . '/admin/install.php');
    			include_once (dirname (__FILE__) . '/admin/update_status.php');
    			
    			
    		}
    in those files there admin ajax they are in conflict how can i solve it??could you help me??

  4. #4
    surferbloggy is offline Hello World
    Join Date
    May 2010
    Posts
    45

    Default

    ok, now it works, i didn't realize the php function names were the same now it's ok thank you bye

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •