I'm in a bit of a blunder.
I am trying to POST data from a form using AJAX, but the data doesn't post at all.
NOTE: I have 3 files [Home.php, Post_Idea.php, Insert_Post.php]
Home.php loads in post_idea.php VIA AJAX. I keep this AJAX code in Home.php:
$("form").submit(function () {
var title = $('#title').val();
var body = $('#body').val();
alert(title + " " + " " + body);
$.ajax({
url: "insert_post.php",
type: "POST",
data: {title: title, body: body},
success: function (result) {
console.log(result);
}
});
});
Insert_Post.php:
<?php
session_start();
require_once("include/database.php");
$user_id = $_SESSION['user_id'];
$title = $_POST['title'];
$body = $_POST['body'];
$img = "cancer_resarch_.jpg";
$votes = 0;
$stm2 = $db->prepare("INSERT INTO posts (user_id, post_title, post_img, post_body, post_votes) VALUES (:user_id ,:title, :body, :img, :votes)");
$stm2->bindParam(':user_id', $user_id);
$stm2->bindParam(':title', $title);
$stm2->bindParam(':body', $body);
$stm2->bindParam(':img', $img);
$stm2->bindParam(':votes', $votes);
$stm2->execute();
The problem is, this code does NOT post at all, yet the alert works and I just get this from Google Chromes Console:
Thank you for your time.