DS Log
In my blog, I delve into the world of programming web technologies, Linux, Unix-like, and graphic design using free tools on Linux.
KINGCODE
KingCode Editor (ex Texty Editor) is my project developed using Java Swing. Project is still in development and in beta version. I plan to add additional features focused for PYTHON, PHP, JAVA, C, JS and BASH.
Read more ↗
VUE on Linux
In this guide, I'll walk you through the step-by-step process of setting up Vue.js on your Linux system, empowering you to create dynamic and interactive web applications. Let's harness the power of Vue.js together on the Linux platform!
Read more ↗
Symfony PHP
Dive into the world of Symfony PHP with this comprehensive introduction. In this guide, you'll learn the essential steps to create and manage posts and users, empowering you to build dynamic web applications with ease.
Read more ↗
Trying Linux from Windows
How to set up a PHP development server on Ubuntu 22.04
Text editors
List of text editors for developers.
Read more ↗
Fonts
Important fonts everyone needs to know.
Read more ↗
Try Linux from Windows
Here are some quick videos I made showing how to try out Linux Mint on Windows.
Read more ↗
Saturday, July 13, 2024
CodeIgniter - post form
Assuming we already have CodeIgniter installed.
We make a controller:
php spark make:controller Post --suffix
Then we create the HTML template ./app/Views/posts.php and add the content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
</head>
<body>
<form action="<?= site_url('post') ?>" method="post">
<p>Name: <?php echo $name ?></p>
<p>Email: <?php echo $email ?></p>
<br>
<p>Unesi podatke: </p>
<input type="text" name="name" placeholder="Enter name"/> <br><br>
<input type="email" name="email" placeholder="Enter email"/><br><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
We add the route to "/app/Config/routes.php":
$routes->get('post', 'PostController::index');
$routes->post('post', 'PostController::index');
In PostController::index we enter:
public function index()
{
$templateData = [
'name' => null,
'email' => null,
];
if ($this->request->getMethod() == "post")
{
$templateData['name'] = $this->request->getVar("name");
$templateData['email'] = $this->request->getVar("email");
}
return view('posts', $templateData);
}
Post request
We create controller named "Fetch":
php spark make:controller Fetch --suffix
HTML template ./app/Views/fetch.php and add content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
</head>
<body>
<input class="ds-input" placeholder="POST SEND" /> <br><br>
<input class="ds-output" placeholder="POST RETURN" readonly /> <br><br>
<button onclick="clickAction()"> Klik </button>
</body>
</html>
<script>
const clickAction = async () => {
const inputElement = document.querySelector('.ds-input');
const outputElement = document.querySelector('.ds-output');
var postData = new FormData();
postData.append('name', inputElement.value);
try {
const response = await fetch('/fetch', {
method: 'POST',
body: postData,
});
const jsonUserData = await response.json();
outputElement.value = jsonUserData.name;
console.log( jsonUserData );
}
catch (error) {
console.error(error);
}
}
</script>
Routes (/app/Config/routes.php)
$routes->get('fetch', 'FetchController::index');
$routes->post('fetch', 'FetchController::index');
FetchController:
<?php
namespace App\Controllers;
use App\Controllers\BaseController;
class FetchController extends BaseController
{
public function index()
{
if ($this->request->getMethod() == "post")
{
$name = $this->request->getVar("name");
echo json_encode(['name'=> $name]);
die();
}
return view('fetch');
}
}
Instead of the "vanilla" answer, we can also use the built-in:
$data = [
'success' => true,
'id' => 123,
];
$this->response->setStatusCode(500);
return $this->response->setJSON(data);