My code doesn’t insert the data into the database when using JQuery.

My Jquery code:
$(“#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{}
?>

My form code:
Input Round

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>

Let me know if any more info is needed.

Read more here: Inserting to a custom WordPress Table with JQuery


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , ,

Wordpress related questions and answers: