PHP: Creating Custom Feedback Forms in Joomla
There are various components provided by Joomla CMS to create forms, one of which is Fabric, but sometimes we also want to create our own custom forms for our own reasons, the form will be made from an article which then the final action is handled by the php file that we created.
Here’s an easy way to create a custom feedback form in Joomla:
1. Create table di mysql
Run the following SQL query command after connecting the mysql database, the command below to create a feedback table, the storage of our feedback form.CREATE TABLE `feedback` (
`id` int(11) NOT NULL,
`title` varchar(255),
`description` text,
`email` varchar(255)
)
2. Create a new article with the following html
The command below is a regular html command to create a form display, such as headers, input fields title, description, and email. The action attribute in the form tag can be adjusted according to the location of the php file that we created.<form action="../insertfeedback.php" method="POST">
<h2>Submit your suggestions</h2>
<table>
<tbody>
<tr>
<td>Title</td>
<td>
<input id="title" style="width: 300px" name="title" type="text" />
</td>
</tr>
<tr>
<td>Description</td>
<td>
<textarea
id="description"
style="width: 350px"
name="description"
rows="5"
></textarea>
</td>
</tr>
<tr>
<td>Email</td>
<td>
<input id="email" style="width: 300px" name="email" type="text" />
</td>
</tr>
<tr>
<td colspan="2">
<input name="submit" type="submit" value="Submit" />
</td>
</tr>
</tbody>
</table>
</form>
3. Create a new file with the name insertfeedback.php and place it in the root of the joomla instance
<?php
ini_set('display_errors', true);
//error_reporting(E_ALL);
define('_JEXEC', 1);
define('JPATH_BASE', dirname(__FILE__));
define('DS', DIRECTORY_SEPARATOR);
require_once(JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once(JPATH_BASE . DS . 'includes' . DS . 'framework.php');
/*To use Joomla's database class*/
require_once(JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'platform.php');
// untuk joomla lama bisa mengganti file dengan factory.php
//require_once (JPATH_BASE.DS.'libraries'.DS.'joomla'.DS.'platform.php');
$db = JFactory::getDBO();
$session = JFactory::getSession();
$title = JRequest::getVar("title");
$description = JRequest::getVar("description");
$email = JRequest::getVar("email");
$sql = "INSERT INTO feedback (title, description, email)
VALUES (" . $db->quote($title) . ", " . $db->quote($description) . ", " . $db->quote($email) . ")";
$db->setQuery($sql);
$db->query();
// print message
echo "Your feedback has been submitted";
// or refers to other page/article
//header('Location: http://www.google.com');
//exit;
The above command will handle the input from the form and save it into the feedback table that we created.
$db = JFactory::getDBO(); // membuat koneksi ke database