Introduction
I’m a complete novice when it comes to html, css, php and Modx.
Despite lacking the knowledge I succeeded to run a comments system using PHP and Ajax under Xampp → localhost. This works without error and flawless. I used the comment system presented here
Now I want to run it under a Modx install.
My problem
Of course (for obvious reasons) it doesn’t run because Modx uses snippets for the php and it is not a simple copy and paste of the php files into snippets. There’s also some JS script involved in the main page. I’m not sure if I can drop that into a chunk. or just refer to it in the head section.
Basically there are 5 files:
1. index.php
2. comment-add.php (see below for code)
<?php
/**
* This script is to add the comment to database.
*/
use Phppot\DataSource;
require_once __DIR__ . '/DataSource.php';
$database = new DataSource();
$sql = "INSERT INTO tbl_comment(parent_comment_id, comment, comment_sender_name) VALUES (?,?,?)";
$paramType = 'iss';
$paramValue = array(
$_POST["comment_id"],
$_POST["comment"],
$_POST["name"]
);
$result = $database->insert($sql, $paramType, $paramValue);
echo $result;
3. comment-list.php (see below for code)
<?php
/**
* This script is to list the comments in a nested order.
*/
use Phppot\DataSource;
require_once __DIR__ . '/DataSource.php';
$database = new DataSource();
$sql = "SELECT * FROM tbl_comment ORDER BY parent_comment_id asc, comment_id asc";
$result = $database->select($sql);
echo json_encode($result);
4. DataSource.php
5. jquery-3.2.1.min.js
Expected behavior
I’m not expecting a complete solution or crashcourse how to implement this into Modx. But I’m eager to learn. So the main question is: How to port and integrate this into Modx? For example the file “comment-list.php”. I have to transfer that code into a snippet. But what should I change so I can use it into a snippet? And how can I use the $result in another snippet, resource page, chunk etc.?
$result = $database->select($sql);
echo json_encode($result);
Or, in the “datasource.php” there are multiple references to the mandatory php files like:
function listComment() {
$.post("comment-list.php"
But the php code of “comment-list.php” has to be in a snippet. How should I refer to that snippet in the datasource.php to replace this phrase “comment-list.php”?
The same for:
$.ajax({
url: "comment-add.php",
How to refer to “comment-add.php”?
I realize this is an extensive question. But I hope somebody can hint me in the right direction.
Environment
I know that the code is not bulletproof or even vulnerable but that is for later to correct.
MODX version 3.0.1 pl, PHP version 8.1