AJAX function not working

时间:2012-11-24 作者:user23769

网站上的一个人写了一个函数,但我不知道为什么它会起作用。此函数显示随机报价。这些函数实现了通过AJAX(链接新报价)下载新报价的功能,但它根本不起作用。帮助请理解原因。我自己也无法理解这一点。

对不起,我的英语不好。

PHP

/wp-content/themes/%your_theme%/js/ajax-load-quote.php

 <?php
 /* uncomment the below, if you want to use native WP functions in this file */
// require_once(\'../../../../wp-load.php\');

 $array = file( $_POST[\'file_path\'] ); // file path in $_POST, as from the js
 $r = rand( 0, count($array) - 1 );

 return \'<p>\' . $array[$r] . \'</p>\';
 ?>
jQuery

/wp-content/themes/%your_theme%/js/ajax-load-quote.js

function ajaxQuote() {
    var theQuote = jQuery.ajax({
        type: \'POST\',
        url: ajaxParams.themeURI+\'js/ajax-load-quote.php\',
        /* supplying the file path to the ajax loaded php as a $_POST variable */
        data: { file_path: ajaxParams.filePath },
        beforeSend: function() {
            ajaxLoadingScreen(true,\'#randomquotes\');
        },
        success: function(data) {
            jQuery(\'#randomquotes\').find(\'p\').remove();
            jQuery(\'#randomquotes\').prepend(data);
        },
        complete: function() {
            ajaxLoadingScreen(false,\'#randomquotes\');
        }
    });
    return theQuote;
}
/* Loading screen to be displayed during the process, optional */
function ajaxLoadingScreen(switchOn,element) {
    /* show loading screen */
    if (switchOn) {
        jQuery(\'\'+element).css({
            \'position\': \'relative\'
        });
        var appendHTML = \'<div class="ajax-loading-screen appended">
            <img src="\'+ajaxParams.themeURI+\'images/ajax-loader.gif"
                alt="Loading ..." width="16" height="16" /></div>\';
        if( jQuery(\'\'+element).children(\'.ajax-loading-screen\').length === 0 ) {
            jQuery(\'\'+element).append(appendHTML);
        }
        jQuery(\'\'+element).children(\'.ajax-loading-screen\').first().css({
            \'display\': \'block\',
            \'visibility\': \'visible\',
            \'filter\': \'alpha(opacity=100)\',
            \'-ms-filter\': \'"progid:DXImageTransform.Microsoft.Alpha(Opacity=100)"\',
            \'opacity\': \'1\'
        });
    } else {
        /* hide the loading screen */
        jQuery(\'\'+element).children(\'.ajax-loading-screen\').css({
            \'display\': \'\',
            \'visibility\': \'\',
            \'filter\': \'\',
            \'-ms-filter\': \'\',
            \'opacity\': \'\'
        });
        jQuery(\'\'+element).css({
            \'position\': \'\'
        });
    }
}
/* triggering the above via the click event */
jQuery(\'#newquotes\').click( function() {
    var theQuote = ajaxQuote();
    return false;
});

功能。php

/wp-content/themes/%your_theme%/functions.php

function random_quote( $atts ) {
    /* extracts the value of shortcode argument path */
    extract( shortcode_atts( array(
        \'path\' => get_template_directory_uri() . \'/quotes.txt\' // default, if not set
    ), $atts ) );
    $array = file( $path );
    $r = rand(0,count($array)-1);
    $output = \'<div id="randomquotes">\' .
            \'<p>\' . $array[rand(0,count($array)-1)] . \'</p>\' .
        \'</div>\' .
        \'<a id="newquotes" class="button" href="#" title="Gimme a new one!">New Quote</a>\';

    /* supplying the file path to the script */
    wp_localize_script(
       \'ajax-quote\',
        \'ajaxParams\',
        array( \'filePath\' => $path, \'themeURI\' => get_template_directory_uri() . \'/\' )
    );
    return $output;
}
add_shortcode( \'randomquotes\', \'random_quote\');
/* loads the js */
function wpse72974_load_scripts() {
    if ( ! is_admin() ) {
        wp_register_script(
           \'ajax-quote\', get_template_directory_uri() . \'/js/ajax-load-quote.js\',
            array( \'jquery\' ),
            \'1.0\',
            true
        );
        wp_enqueue_script( \'ajax-quote\' );
        /* supplying the theme URI to the script */
        wp_localize_script(
            \'ajax-quote\',
            \'ajaxParams\',
             array( \'themeURI\' => get_template_directory_uri() . \'/\' )
        );
    }
}
add_action ( \'init\', \'wpse72974_load_scripts\' );

1 个回复
SO网友:s_ha_dum

您在此行中有一个错误:var appendHTML = \'<div class="ajax-loading-screen appended"> 注意Javascript中的多行字符串。它的工作方式与PHP不同。

https://stackoverflow.com/questions/805107/how-to-create-multiline-strings

结束

相关推荐

使用PHP邮件;获取标题警告

我在页面中直接使用PHP mail()创建了一个联系人表单。它成功地通过电子邮件将te联系信息发送给收件人。但是,它不会前进到header()中标识的确认页面,并输出以下错误:警告:无法修改标题信息-标题已由/home/redacted/redacted.com/wp-content/themes/diner/includes/theme-functions.php:752)发送。com/wp内容/主题/餐厅/页面。php在线163关于如何解决这个问题,有什么建议或资源链接吗?<?php f