2

Wordpress プラグインを作成していて、ajax を使用してデータを送信したいと考えています。管理パネルで ajax を使用してフォームを送信すると、次のエラーが発生します。

致命的なエラー: 13 行目の /home1/crave/public_html/wp-content/plugins/MiniCMS/add_contenttype.php の非オブジェクトに対するメンバー関数 insert() の呼び出し

呼び出されるスクリプトは次のとおりです。エラー行に注釈が付けられます。

<?php
global $wpdb;

$name = $_POST["name"];
$id = '1';
$text_inputs = $_POST["text_inputs"];
$paragraph_inputs = $_POST["paragraph_inputs"];
$map_inputs = $_POST["map_inputs"];
$file_inputs = $_POST["file_inputs"];

$contentTypeTable = $wpdb->prefix . "minicms_content_type";

//This is line 13, the problem child:
$wpdb->insert( $contentTypeTable, array(
    'name' => $name,
    'id' => $id,
    'text_inputs' => $text_inputs,
    'paragraph_inputs' => $paragraph_inputs,
    'map_inputs' => $map_inputs,
    'file_inputs' => $file_inputs
));
?>

$wpdb が動作しない理由を誰か知っていますか?

4

1 に答える 1

4

WordPress のように ajax を使用する必要があります。

WordPress Doc から:

まず、AJAX リクエストをトリガーする JavaScript をいくつか追加します。

<?php
add_action( 'admin_footer', 'my_action_javascript' );

function my_action_javascript() {
?>
<script type="text/javascript" >
jQuery(document).ready(function($) {

    var data = {
        action: 'my_action',
        whatever: 1234
    };

    // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
    $.post(ajaxurl, data, function(response) {
        alert('Got this from the server: ' + response);
    });
});
</script>
<?php
}

次に、そのリクエストを処理する PHP 関数を設定します。

add_action('wp_ajax_my_action', 'my_action_callback');

function my_action_callback() {
    global $wpdb; // this is how you get access to the database

    $whatever = intval( $_POST['whatever'] );

    $whatever += 10;

        echo $whatever;

    die(); // this is required to return a proper result
}

参照:
1. http://codex.wordpress.org/AJAX_in_Plugins
2. http://wp.tutsplus.com/articles/getting-started-with-ajax-wordpress-pagination/

于 2013-11-10T08:11:44.090 に答える