βοΈ AppML β Common API Methods: Master Data Interaction with CRUD
π§² Introduction β What Are AppML API Methods?
AppML uses a simplified, HTML-friendly approach to interact with backend services through standard API methods. Whether you’re fetching records, saving form data, or filtering tables, AppML supports the most common HTTP-based operationsβwithout writing JavaScript or external libraries.
π― In this article, youβll learn:
- The most common AppML API methods (GET, POST, PUT, DELETE)
- How each method is used with
appml-data
andappml-submit
- Examples of real-world use: fetching data, saving forms, deleting records
- Backend handling tips for PHP, Node.js, and other stacks
π What Are AppML API Methods?
AppML supports the following common HTTP methods to communicate with APIs:
π Method | π§ Purpose | π Usage in AppML |
---|---|---|
GET | Retrieve data | Auto-triggered by appml-data |
POST | Create new data | Triggered on form submission |
PUT | Update data | Triggered when record has id |
DELETE | Delete record | Manually triggered via controller |
These map directly to standard CRUD operations.
π§ͺ 1. GET β Fetch Data from API
β Example: Fetch and Display Users
<div appml-data="api/users.php">
<p>{{name}} β {{email}}</p>
</div>
β
Sample users.php
echo json_encode([
["name" => "Alice", "email" => "alice@example.com"],
["name" => "Bob", "email" => "bob@example.com"]
]);
π AppML uses GET
to fetch this JSON and render it using the view.
π 2. POST β Submit New Data via Form
When a user fills out a form and hits a button
with appml-submit
, AppML sends a POST
request with the form data.
β HTML Form Example
<div appml-model="models/product-model.json" appml-data="api/save-product.php">
<input name="name">
<input name="price" type="number">
<button appml-submit>Add Product</button>
</div>
β
Backend (save-product.php
)
$data = json_decode(file_get_contents("php://input"), true);
// Save $data['name'], $data['price'] to database...
π¦ AppML automatically sends a JSON payload.
π 3. PUT β Update Existing Record
When a form includes a primary key (e.g., id
), AppML switches from POST
to PUT
, meaning you’re editing an existing record.
β Example
<input name="id" value="23" hidden>
<input name="name" value="Updated Product">
AppML detects the id
and uses a PUT
request to update the data.
ποΈ 4. DELETE β Remove a Record
Deletion is typically triggered manually through a controller function:
β Delete Record via Controller
function deleteItem() {
myAppML.deleteRecord();
}
β
This sends a DELETE
request with the recordβs ID, based on the modelβs primary key.
π§ Best Practices for API Methods
π‘ Tip | β Why It Matters |
---|---|
Use id to determine POST vs PUT | AppML switches based on presence of ID |
Return proper HTTP codes | Helps AppML handle response correctly |
Validate inputs on server | Avoids malicious requests |
Sanitize data before DB insert | Prevents SQL injection |
Send back a success message | Useful for myAppML.message display |
π Summary β Recap & Key Takeaways
AppML integrates tightly with backend APIs using standard methods like GET, POST, PUT, and DELETE. These actions allow seamless data interaction through clean HTML syntax and optional controller logic.
π Key Takeaways:
- Use
appml-data
to triggerGET
on page load - Use
appml-submit
to triggerPOST
orPUT
depending on the presence ofid
- Trigger
DELETE
withmyAppML.deleteRecord()
- Backends must accept and respond to JSON payloads
- Use proper security, validation, and messaging practices
βοΈ These API methods make AppML a great choice for building real CRUD apps without a heavy framework.
β FAQs β Common API Methods in AppML
β How does AppML decide between POST and PUT?
β
If the model includes a primary key (id
) and itβs filled in, AppML sends a PUT
. Otherwise, it uses POST
.
β Can I use query parameters in the URL?
β
Yes. AppML supports appml-data="api/users.php?status=active"
for filtered queries.
β Is DELETE handled automatically?
β No. You must trigger it manually with myAppML.deleteRecord()
or custom logic.
β Can I override the HTTP method manually?
β οΈ Not directly in HTML, but you can override it in your backend or by modifying myAppML.httpMethod
.
β Do I need to return any specific format?
β
Return application/json
or application/xml
. JSON is preferred for compatibility.
Share Now :