使用JQuery插入自定义的Wordpress表


Inserting to a custom Wordpress Table with JQuery

使用JQuery时,我的代码没有将数据插入数据库。

我的Jquery代码:
$("#addMatches").button().click(function( event ) {
        var that = $('#group-settings-form.standard-form'),
        oldUrl = that.attr('action'),
        testUrl = oldUrl.split('/'),
        url = testUrl[0]+'//'+testUrl[2]+'/'+testUrl[3]+'/wp-content/themes/betting/bet_wager.php',
        data = {};
        var matchRound = $('#matchRound').val(), group_id = $('#group_id').val(); firstPlayer = $('input[name="firstPlayer"]').val();

        that.removeAttr('action');
        that.attr('action',url),
        that.find("[name]").each(function(index,value) {
        var that = $(this),
        name = that.attr('name'),
        value = that.val();
        data[name] = value;  
        });
        console.log(firstPlayer);
        $.post(url,{matchRound:matchRound,group_id:group_id,firstPlayer:firstPlayer},
        function(results) {
        }); console.log(firstPlayer);
            event.preventDefault();window.location.reload();
            });

my WordPress PHP file code:

<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/wordpress/wp-config.php' );
if (isset($_POST['matchRound'])){
        $matchRound = $_POST['matchRound'];$group_id =$_POST['group_id'];$firstPlayer = $_POST['firstPlayer'];$secondPlayer = $_POST['secondPlayer'];
    $addTeam1 = $wpdb->prefix . "dzwebmatch";$wpdb->insert($addTeam1,
    array( 'matchRound' => $matchRound, 'group_id' => $group_id, 'firstPlayer' => $firstPlayer, 'secondPlayer' => $secondPlayer ),
    array( '%d','%d','%s',"%s" ) );
    $wpdb->show_errors();
    $wpdb->print_error();
$wpdb->hide_errors();
}else{}
?>

我的表单代码:输入圆

Player1Player2 ';
        for ($i=0; $i < $ladderRows; $i++)        
         {
           echo "<tr><td><input name='firstPlayer'/></td><td>Vs</td><td><input name='secondPlayer'/></td></tr>";
         }
         ?>
        </table>
        <input type="submit" id="addMatches" value="Add Matches" />
        </form>
如果需要更多的信息请告诉我。

在WP中,ajax请求以稍微不同的方式发送。查看这些链接。

http://www.smashingmagazine.com/2011/10/18/how-to-use-ajax-in-wordpress/

http://wptheming.com/2013/07/simple-ajax-example/

使用wp-load.php加载WordPress内置函数

include_once($_SERVER['DOCUMENT_ROOT'].'/wordpress/wp-load.php' );