• hi i have some error for my first wordpress plugin

    i make the plugin admin site juqery and ajax to update database
    but it not work. can somebody give some advice.

    below is the code

    index.php

    // Hook for adding admin menus
    add_action('admin_menu', 'dinar_currency_pages',5);
    
    // action function for above hook
    function dinar_currency_pages() {
        // Add a new top-level menu
    	//add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
        add_menu_page('Dinar Currency', 'Dinar Currency', 'manage_options', 'currency', 'currency_admin' );
    }
    
    // Currency Page
    function currency_admin() {
    
    	global $wpdb;
    	$table_name = $wpdb->prefix . "dinar";
    
    	$plugin_url=WP_PLUGIN_URL;
    
    	global $current_user;
    
    	//get_currentuserinfo();
    	$user = $current_user->user_login;
    
    	$datetime = gmdate("Y-m-d H:i:s ");
    
    	$query = mysql_query("SELECT id, currency, disc, thumb FROM wp_dinar");
    
    	echo "
    	<form method='post' action='update.php'>
    	<div id='container'>
    	<div id='main'>
    	";
    
    	echo "<ul id='photos'> \n";
    
    	while( $row = mysql_fetch_assoc($query)){
    
    		$currency = $row['currency'];
    		$thumb = $row['thumb'];
    		$id = $row['id'];
    
    		echo "<li><img src='images/$thumb' id='$id' width='160' height='120'/>\n";
    		echo "<h4>$currency</h4> \n";
    		echo "<input type='text' name='currency' value='$currency' /></li> \n \n";
    		}	
    
    		echo "</ul>";
    
    		echo "
    
    			<div id='response' class='hidden' />
    	</div><!-- end main-->
    
    </div><!-- end container-->
    </form>
    
    		";
    
    }
    
    add_action( 'admin_print_styles', 'admin_enqueue_styles' );
    
    function admin_enqueue_styles() {
    
    	wp_enqueue_style( 'admin', ex_plugin_url( 'css/style.css' ),
    		array(), EXCHANGE_VERSION, 'all');
    
    	wp_enqueue_script( 'myscript', ex_plugin_url( '/script.js' ), EXCHANGE_VERSION);
    	wp_deregister_script( 'jquery' );
        wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js','v1.51');
        wp_enqueue_script( 'jquery' );
    }

    script.js

    $(function() {
        $('form').attr('action', '');
    
        $('h4').click(function() {
    
            $(this).slideUp().next('input').slideDown();
        });
    
        $('ul#photos input').change(function() {
            var id = $(this).parent('li').find('img').attr('id');
            var thisParam = $(this);
            var currency = $(this).val();
    
            $.ajax({
                type: 'post',
                url: 'update.php',
                data: 'currency=' + currency + '&id=' + id,
    
                success: function(response) {
                    $('input').slideUp();
                    $(thisParam).prev('h4').text(currency).slideDown();
    
                    $('#response').fadeIn('1000').empty().append(response).prepend('<span id="x">X</span>');
    
                    $('span#x').click(function() {
                        $('div#response').fadeOut('slow');
                    });
    
                }
    
            });
    
        });
    });

    update.php

    global $wpdb;
    $table_name = $wpdb->prefix . "dinar";
    
    $currency = $_POST['currency'];
    $id = $_POST['id'];
    
    //$query = mysql_query("SELECT id, title, src FROM photos");
    $update_query = mysql_query("UPDATE wp_dinar SET currency = '$currency' WHERE id='$id'");
    
    //$result = $mysqli->query($update_query) or die(mysqli_error($mysqli));
    
    //if ($result) {
    	echo "Success! <br />";
    	echo "The title of this photo has been changed to:  <strong>$title</strong>";
    //}
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

The topic ‘admin site ajax plugin’ is closed to new replies.