Help with AJAX request

[ad_1]

Not sure if this should go here. If not, let me know and I’ll move it.

I’m working on creating an event plugin. Part of this is that I want to have a comment system for each event. I’m having trouble using AJAX to call the page to save the comment to the database. I checked and know jQuery is being loaded. My code is below. Any help or suggestions are greatly appreciated.

**Form**

<h2>Add Comment</h2>
<form method=”post” id=”comment_form”>
<textarea name=”comment” id=”comment” class=”commentbox” rows=”5″ cols=”120″ required></textarea><br />
<input type=”hidden” name=”eventid” id=”eventid” value=”<?php echo $id; ?>>” />
<input type=”submit” name=”submit” id=”submit” value=”Add Comment” class=”commentbtn”>
</form>
<span id=”comment_message”></span>
<br />
<div id=”display_comment”></div>

**AJAX Script**

<script>
$(document).ready(function () {
$(‘#comment_form’).on(‘submit’, function (event) {
event.preventDefault();
var form_data = $(this).serialize();
$.ajax({
url:”http://intranet/wp-content/plugins/communitybb/savecomment.php”,
method:”POST”,
data:form_data,
dataType:”JSON”,
success:function(data) {
if (data.error != ”) {
$(‘#comment_form’)[0].reset();
$(‘#comment_message’).html(data.error);
}
}
})
});
})
</script>

**savecomment.php**

<?php
$wp_path = explode(‘wp-content’, dirname(__FILE__));
require($wp_path[0].’wp-load.php’);
global $wpdb;

$userid = get_current_user_id();
$eventid = $_POST[“eventid”];
$comm = $_POST[“comment”];
date_default_timezone_set(“America/New_York”);
$date = date(‘Y-m-d H:i:s’);

$inserttable = $wpdb->prefix . “cmc_communitybb_event_comments”;
$data = array(
‘eventid’ => $eventid,
‘userid’ => $userid,
‘content’ => $comm,
‘datesaved’ => $date,
);
$wpdb->insert($inserttable, $data);

$error = “<label class=’text-success’>Comment Added</label>”;

$data = array(
‘error’ => $error;
);

echo json_encode($data);
?>

&#x200B;

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer