bobgiscoolelwdjkanwd
Market Demand Exploiter
LEVEL 1
200 XP
Introduction
In this tutorial, you will learn an effective way of Creating a CRUD Operation of JSON File Data in PHP language. This tutorial aims to provide a reference or guide for students or for those who are new to PHP Language to enhance their knowledge and programming capabilities of handling JSON File Data using the said programming language. Here, I have provided a snippets and sample web application's source code that is free to download.
What is CRUD?
In programming, CRUD stands for Create, Read, Update, and Delete functions. These are four basic functions common to web applications especially the applications that store and manage dynamic data.
How to Create CRUD for JSON File?
To Create CRUD functionalities or operations for your JSON file we can use the built-in functions of PHP called file_put_contents() and file_get_contents(). With these 2 functions, we can simply manipulate or update the JSON File Data in PHP.
Example
Let us assume that we have a JSON File named data.json. This JSON file contains the following structure.
The JSON file will be filled with object items with the above structure and let's call it the list of members. Here's an example filled JSON file:
PHP CRUD Functions
Then, to manipulate or manage the JSON File Data. You can use the following snippet. The CRUD Functions below are written using an Object-Orient Programming or OOP Approach in PHP.
Let's assume that the PHP Script File above is named master.php.
Explanations
Here are the explanations of each function you can see in the Master Class above:
get_all_data() - this function can be used for retrieving the JSON Data and returning it as an array of objects of the details of each member. This function is an example of a Read Function.
get_data([(int) $id]) - this function can be used for retrieving a single data from the JSON file whereas the ID is equal to the given ID on the parameter. If data with the given ID exists on the file then this will return the object data of the member otherwise it will only return an object w/ no data. This function is also an example of a Read Function.
insert_to_json() - this function can be called to insert new member data on the JSON File. The function requires POST data of name, contact, and address. The function will automatically generate an incrementing ID of the data. This function represents the Create Function of CRUD.
update_json_data() - this function can be called to update an existing object data on the JSON File. Like the insert_to_json function, this function also requires POST Data of name, contact, and address but here, POST id data is also required which will be used to identify the object data to update. This function represents the Update Function or the CRUD.
delete_data([(int) $id]) - this function is called for deleting an object data on the JSON File. This function requires an ID as the first and only parameter of the function to identify the object data to remove from the file. This function represents the Delete Function of CRUD.
Interface
Now using the snippets above, we can now create web pages for displaying the list of members and create the add/edit form interfaces.
Main Page
On the main page, we will list all the members from the JSON File. Here, we will also add the buttons where the user can redirect to the add member form, edit member details form, and delete member action page. Please note that on this file you must include the Master Class that I have provided above.
index.php
Form Page
The following snippet is used for both Add and Edit Forms. If this page is loaded with an ID parameter on the URL and is existing on the JSON FILE, the page will serve as an Edit Form otherwise Add Form.
member_form.php
That's it! Now you can test the simple application on your end and see if it works well and achieve our goal for this tutorial. You can also download that source code zip file on this page by clicking the Download button below this article.
Snapshots
Main Page
Form Page
There you go. That's the end of this tutorial. I hope this JSON File CRUD operation using PHP Tutorial will help you with what you are looking for and that you'll find this useful for your current or future PHP Projects.
Explore more on this website for more Tutorials and Free Source Codes.
Happy Coding :)
Download
In this tutorial, you will learn an effective way of Creating a CRUD Operation of JSON File Data in PHP language. This tutorial aims to provide a reference or guide for students or for those who are new to PHP Language to enhance their knowledge and programming capabilities of handling JSON File Data using the said programming language. Here, I have provided a snippets and sample web application's source code that is free to download.
What is CRUD?
In programming, CRUD stands for Create, Read, Update, and Delete functions. These are four basic functions common to web applications especially the applications that store and manage dynamic data.
- Create - this function is being called for inserting or creating new data.
- Read - this function is being called to fetch the dynamic data to display.
- Update - this function is being called when updating the existing dynamic data.
- Delete - this function is being called for deleting data.
How to Create CRUD for JSON File?
To Create CRUD functionalities or operations for your JSON file we can use the built-in functions of PHP called file_put_contents() and file_get_contents(). With these 2 functions, we can simply manipulate or update the JSON File Data in PHP.
Example
Let us assume that we have a JSON File named data.json. This JSON file contains the following structure.
- {
- "id"
:
#,
- "name"
:
""
,
- "contact"
:
""
,
- "address"
:
""
- }
The JSON file will be filled with object items with the above structure and let's call it the list of members. Here's an example filled JSON file:
- [
- {
- "id"
:
1
,
- "name"
:
"John Smith"
,
- "contact"
:
"0912354789"
,
- "address"
:
"Sample Address"
- }
,
- {
- "id"
:
2
,
- "name"
:
"Claire Blake"
,
- "contact"
:
"09789456123"
,
- "address"
:
"Sample Address 102"
- }
- ]
PHP CRUD Functions
Then, to manipulate or manage the JSON File Data. You can use the following snippet. The CRUD Functions below are written using an Object-Orient Programming or OOP Approach in PHP.
- <?php
- class
Master {
- /**
- * Get All JSON Data
- */
- function
get_all_data(
)
{
- $json
=
(
array
)
json_decode
(
file_get_contents
(
'data.json'
)
)
;
- $data
=
[
]
;
- foreach
(
$json
as
$row
)
{
- $data
[
$row
->
id
]
=
$row
;
- }
- return
$data
;
- }
- /**
- * Get single JSON Data
- */
- function
get_data(
$id
=
''
)
{
- if
(
!
empty
(
$id
)
)
{
- $data
=
$this
->
get_all_data
(
)
;
- if
(
isset
(
$data
[
$id
]
)
)
{
- return
$data
[
$id
]
;
- }
- }
- return
(
object)
[
]
;
- }
- /**
- * Insert Data into JSON File
- */
- function
insert_to_json(
)
{
- $name
=
addslashes
(
$_POST
[
'name'
]
)
;
- $contact
=
addslashes
(
$_POST
[
'contact'
]
)
;
- $address
=
addslashes
(
$_POST
[
'address'
]
)
;
- $data
=
$this
->
get_all_data
(
)
;
- $id
=
array_key_last(
$data
)
+
1
;
- $data
[
$id
]
=
(
object)
[
- "id"
=>
$id
,
- "name"
=>
$name
,
- "contact"
=>
$contact
,
- "address"
=>
$address
- ]
;
- $json
=
json_encode
(
array_values
(
$data
)
,
JSON_PRETTY_PRINT)
;
- $insert
=
file_put_contents
(
'data.json'
,
$json
)
;
- if
(
$insert
)
{
- $resp
[
'status'
]
=
'success'
;
- }
else
{
- $resp
[
'failed'
]
=
'failed'
;
- }
- return
$resp
;
- }
- /**
- * Update JSON File Data
- */
- function
update_json_data(
)
{
- $id
=
$_POST
[
'id'
]
;
- $name
=
addslashes
(
$_POST
[
'name'
]
)
;
- $contact
=
addslashes
(
$_POST
[
'contact'
]
)
;
- $address
=
addslashes
(
$_POST
[
'address'
]
)
;
- $data
=
$this
->
get_all_data
(
)
;
- $data
[
$id
]
=
(
object)
[
- "id"
=>
$id
,
- "name"
=>
$name
,
- "contact"
=>
$contact
,
- "address"
=>
$address
- ]
;
- $json
=
json_encode
(
array_values
(
$data
)
,
JSON_PRETTY_PRINT)
;
- $update
=
file_put_contents
(
'data.json'
,
$json
)
;
- if
(
$update
)
{
- $resp
[
'status'
]
=
'success'
;
- }
else
{
- $resp
[
'failed'
]
=
'failed'
;
- }
- return
$resp
;
- }
- /**
- * Delete Data From JSON File
- */
- function
delete_data(
$id
=
''
)
{
- if
(
empty
(
$id
)
)
{
- $resp
[
'status'
]
=
'failed'
;
- $resp
[
'error'
]
=
'Given Member ID is empty.'
;
- }
else
{
- $data
=
$this
->
get_all_data
(
)
;
- if
(
isset
(
$data
[
$id
]
)
)
{
- unset
(
$data
[
$id
]
)
;
- $json
=
json_encode
(
array_values
(
$data
)
,
JSON_PRETTY_PRINT)
;
- $update
=
file_put_contents
(
'data.json'
,
$json
)
;
- if
(
$update
)
{
- $resp
[
'status'
]
=
'success'
;
- }
else
{
- $resp
[
'failed'
]
=
'failed'
;
- }
- }
else
{
- $resp
[
'status'
]
=
'failed'
;
- $resp
[
'error'
]
=
'Given Member ID is not existing on the JSON File.'
;
- }
- }
- return
$resp
;
- }
- }
Let's assume that the PHP Script File above is named master.php.
Explanations
Here are the explanations of each function you can see in the Master Class above:
get_all_data() - this function can be used for retrieving the JSON Data and returning it as an array of objects of the details of each member. This function is an example of a Read Function.
get_data([(int) $id]) - this function can be used for retrieving a single data from the JSON file whereas the ID is equal to the given ID on the parameter. If data with the given ID exists on the file then this will return the object data of the member otherwise it will only return an object w/ no data. This function is also an example of a Read Function.
insert_to_json() - this function can be called to insert new member data on the JSON File. The function requires POST data of name, contact, and address. The function will automatically generate an incrementing ID of the data. This function represents the Create Function of CRUD.
update_json_data() - this function can be called to update an existing object data on the JSON File. Like the insert_to_json function, this function also requires POST Data of name, contact, and address but here, POST id data is also required which will be used to identify the object data to update. This function represents the Update Function or the CRUD.
delete_data([(int) $id]) - this function is called for deleting an object data on the JSON File. This function requires an ID as the first and only parameter of the function to identify the object data to remove from the file. This function represents the Delete Function of CRUD.
Interface
Now using the snippets above, we can now create web pages for displaying the list of members and create the add/edit form interfaces.
Main Page
On the main page, we will list all the members from the JSON File. Here, we will also add the buttons where the user can redirect to the add member form, edit member details form, and delete member action page. Please note that on this file you must include the Master Class that I have provided above.
index.php
- <?php
- session_start(
)
;
- require_once(
'master.php'
)
;
- $master =
new Master(
)
;
- $json_data =
$master->
get_all_data();
- ?>
- <!DOCTYPE html>
- <html
lang
=
"en"
>
- <head
>
- <meta
charset
=
"UTF-8"
>
- <meta
http-equiv
=
"X-UA-Compatible"
content
=
"IE=edge"
>
- <meta
name
=
"viewport"
content
=
"width=device-width, initial-scale=1.0"
>
- <title
>
PHP - JSON CRUD</
title
>
- <link
rel
=
"stylesheet"
href
=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css"
integrity=
"sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A=="
crossorigin=
"anonymous"
referrerpolicy=
"no-referrer"
/
>
- <script
src
=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/js/all.min.js"
integrity=
"sha512-naukR7I+Nk6gp7p5TMA4ycgfxaZBJ7MO5iC3Fp6ySQyKFHOGfpkSZkYVWV5R7u7cfAicxanwYQ5D1e17EfJcMA=="
crossorigin=
"anonymous"
referrerpolicy=
"no-referrer"
></
script
>
- <link
rel
=
"stylesheet"
href
=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity=
"sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi"
crossorigin=
"anonymous"
>
- <script
src
=
"https://code.jquery.com/jquery-3.6.1.min.js"
integrity=
"sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ="
crossorigin=
"anonymous"
></
script
>
- <script
src
=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity=
"sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3"
crossorigin=
"anonymous"
></
script
>
- <style
>
- html, body{
- min-height:100%;
- width:100%;
- }
- </
style
>
- </
head
>
- <body
>
- <nav class
=
"navbar navbar-expand-lg navbar-dark bg-primary bg-gradient"
>
- <div
class
=
"container"
>
- <a
class
=
"navbar-brand"
href
=
"./"
>
PHP - JSON CRUD</
a
>
- <div
>
- <a
href
=
"https://sourcecodester.com"
class
=
"text-light fw-bolder h6 text-decoration-none"
target
=
"_blank"
>
SourceCodester</
a
>
- </
div
>
- </
div
>
- </
nav>
- <div
class
=
"container px-5 my-3"
>
- <h2
class
=
"text-center"
>
Create, Read, Update, and Delete Operation for JSON Data in PHP</
h2
>
- <div
class
=
"row"
>
- <!-- Page Content Container -->
- <div
class
=
"col-lg-10 col-md-11 col-sm-12 mt-4 pt-4 mx-auto"
>
- <div
class
=
"container-fluid"
>
- <!-- Handling Messages Form Session -->
- <?php if(
isset(
$_SESSION[
'msg_success'
]
)
|| isset(
$_SESSION[
'msg_error'
]
)
)
: ?>
- <?php if(
isset(
$_SESSION[
'msg_success'
]
)
)
: ?>
- <div
class
=
"alert alert-success rounded-0"
>
- <div
class
=
"d-flex justify-content-between align-items-center"
>
- <div
class
=
"col-auto flex-shrink-1 flex-grow-1"
><?=
$_SESSION[
'msg_success'
]
?></
div
>
- <div
class
=
"col-auto"
>
- <a
href
=
"#"
onclick
=
"$(this).closest('.alert').remove()"
class
=
"text-decoration-none text-reset fw-bolder mx-3"
>
- <i
class
=
"fa-solid fa-times"
></
i
>
- </
a
>
- </
div
>
- </
div
>
- </
div
>
- <?php unset(
$_SESSION[
'msg_success'
]
)
; ?>
- <?php endif; ?>
- <?php if(
isset(
$_SESSION[
'msg_error'
]
)
)
: ?>
- <div
class
=
"alert alert-danger rounded-0"
>
- <div
class
=
"d-flex justify-content-between align-items-center"
>
- <div
class
=
"col-auto flex-shrink-1 flex-grow-1"
><?=
$_SESSION[
'msg_error'
]
?></
div
>
- <div
class
=
"col-auto"
>
- <a
href
=
"#"
onclick
=
"$(this).closest('.alert').remove()"
class
=
"text-decoration-none text-reset fw-bolder mx-3"
>
- <i
class
=
"fa-solid fa-times"
></
i
>
- </
a
>
- </
div
>
- </
div
>
- </
div
>
- <?php unset(
$_SESSION[
'msg_error'
]
)
; ?>
- <?php endif; ?>
- <?php endif; ?>
- <!--END of Handling Messages Form Session -->
- <div
class
=
"card rounded-0 shadow"
>
- <div
class
=
"card-header"
>
- <div
class
=
"d-flex justify-content-between"
>
- <div
class
=
"card-title col-auto flex-shrink-1 flex-grow-1"
>
Member List</
div
>
- <div
class
=
"col-atuo"
>
- <a
class
=
"btn btn-primary btn-sm btn-flat"
href
=
"member_form.php"
><i
class
=
"fa fa-plus-square"
></
i
>
Add Member</
a
>
- </
div
>
- </
div
>
- </
div
>
- <div
class
=
"card-body"
>
- <div
class
=
"container-fluid"
>
- <table
class
=
"table table-stripped table-bordered"
>
- <colgroup
>
- <col
width
=
"5%"
>
- <col
width
=
"20%"
>
- <col
width
=
"20%"
>
- <col
width
=
"35%"
>
- <col
width
=
"20%"
>
- </
colgroup
>
- <thead
>
- <tr
>
- <th
class
=
"text-center"
>
ID</
th
>
- <th
class
=
"text-center"
>
Name</
th
>
- <th
class
=
"text-center"
>
Contact#</
th
>
- <th
class
=
"text-center"
>
Address</
th
>
- <th
class
=
"text-center"
>
Action</
th
>
- </
tr
>
- </
thead
>
- <tbody
>
- <?php foreach(
$json_data as $data)
: ?>
- <tr
>
- <td
class
=
"text-center"
><?=
$data->
id ?></
td
>
- <td
><?=
$data->
name ?></
td
>
- <td
><?=
$data->
contact ?></
td
>
- <td
><?=
$data->
address ?></
td
>
- <td
class
=
"text-center"
>
- <a
href
=
"member_form.php?id=<?= $data->
id ?>" class="btn btn-sm btn-outline-info rounded-0">
- <i
class
=
"fa-solid fa-edit"
></
i
>
- </
a
>
- <a
href
=
"delete_data.php?id=<?= $data->
id ?>" class="btn btn-sm btn-outline-danger rounded-0" onclick="if(confirm(`Are you sure to delete <?=
$data->
name ?> details?`) === false) event.preventDefault();">
- <i
class
=
"fa-solid fa-trash"
></
i
>
- </
a
>
- </
td
>
- </
tr
>
- <?php endforeach; ?>
- </
tbody
>
- </
table
>
- </
div
>
- </
div
>
- </
div
>
- </
div
>
- </
div
>
- </
div
>
- </
div
>
- </
body
>
- </
html
>
Form Page
The following snippet is used for both Add and Edit Forms. If this page is loaded with an ID parameter on the URL and is existing on the JSON FILE, the page will serve as an Edit Form otherwise Add Form.
member_form.php
- <?php
- session_start(
)
;
- require_once(
'master.php'
)
;
- $master =
new Master(
)
;
- if(
$_SERVER[
'REQUEST_METHOD'
]
==
'POST'
)
{
- if(
isset(
$_POST[
'id'
]
)
&& is_numeric(
$_POST[
'id'
]
)
&& $_POST[
'id'
]
>
0){
- $save = $master->update_json_data();
- }else{
- $save = $master->insert_to_json();
- }
- if(isset($save['status'])){
- if($save['status'] == 'success'){
- if(isset($_POST['id']) && is_numeric($_POST['id']) && $_POST['id'] > 0)
- $_SESSION['msg_success'] = 'New Member has been added to JSON File Successfully';
- else
- $_SESSION['msg_success'] = 'Member Details has been updated to JSON File Successfully';
- header('location: ./');
- exit;
- }
- }else{
- $_SESSION['msg_error'] = 'Details has failed to save due to some error.';
- }
- }
- $data = $master->get_data(isset($_GET['id']) ? $_GET['id'] :'');
- ?>
- <!DOCTYPE html>
- <html
lang
=
"en"
>
- <head
>
- <meta
charset
=
"UTF-8"
>
- <meta
http-equiv
=
"X-UA-Compatible"
content
=
"IE=edge"
>
- <meta
name
=
"viewport"
content
=
"width=device-width, initial-scale=1.0"
>
- <title
>
Member Form | PHP - JSON CRUD</
title
>
- <link
rel
=
"stylesheet"
href
=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css"
integrity=
"sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A=="
crossorigin=
"anonymous"
referrerpolicy=
"no-referrer"
/
>
- <script
src
=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/js/all.min.js"
integrity=
"sha512-naukR7I+Nk6gp7p5TMA4ycgfxaZBJ7MO5iC3Fp6ySQyKFHOGfpkSZkYVWV5R7u7cfAicxanwYQ5D1e17EfJcMA=="
crossorigin=
"anonymous"
referrerpolicy=
"no-referrer"
></
script
>
- <link
rel
=
"stylesheet"
href
=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity=
"sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi"
crossorigin=
"anonymous"
>
- <script
src
=
"https://code.jquery.com/jquery-3.6.1.min.js"
integrity=
"sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ="
crossorigin=
"anonymous"
></
script
>
- <script
src
=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity=
"sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3"
crossorigin=
"anonymous"
></
script
>
- <style
>
- html, body{
- min-height:100%;
- width:100%;
- }
- </
style
>
- </
head
>
- <body
>
- <nav class
=
"navbar navbar-expand-lg navbar-dark bg-primary bg-gradient"
>
- <div
class
=
"container"
>
- <a
class
=
"navbar-brand"
href
=
"./"
>
PHP - JSON CRUD</
a
>
- <div
>
- <a
href
=
"https://sourcecodester.com"
class
=
"text-light fw-bolder h6 text-decoration-none"
target
=
"_blank"
>
SourceCodester</
a
>
- </
div
>
- </
div
>
- </
nav>
- <div
class
=
"container px-5 my-3"
>
- <h2
class
=
"text-center"
>
Member Form</
h2
>
- <div
class
=
"row"
>
- <!-- Page Content Container -->
- <div
class
=
"col-lg-10 col-md-11 col-sm-12 mt-4 pt-4 mx-auto"
>
- <div
class
=
"container-fluid"
>
- <!-- Handling Messages Form Session -->
- <?php if(
isset(
$_SESSION[
'msg_success'
]
)
|| isset(
$_SESSION[
'msg_error'
]
)
)
: ?>
- <?php if(
isset(
$_SESSION[
'msg_success'
]
)
)
: ?>
- <div
class
=
"alert alert-success rounded-0"
>
- <div
class
=
"d-flex justify-content-between align-items-center"
>
- <div
class
=
"col-auto flex-shrink-1 flex-grow-1"
><?=
$_SESSION[
'msg_success'
]
?></
div
>
- <div
class
=
"col-auto"
>
- <a
href
=
"#"
onclick
=
"$(this).closest('.alert').remove()"
class
=
"text-decoration-none text-reset fw-bolder mx-3"
>
- <i
class
=
"fa-solid fa-times"
></
i
>
- </
a
>
- </
div
>
- </
div
>
- </
div
>
- <?php unset(
$_SESSION[
'msg_success'
]
)
; ?>
- <?php endif; ?>
- <?php if(
isset(
$_SESSION[
'msg_error'
]
)
)
: ?>
- <div
class
=
"alert alert-danger rounded-0"
>
- <div
class
=
"d-flex justify-content-between align-items-center"
>
- <div
class
=
"col-auto flex-shrink-1 flex-grow-1"
><?=
$_SESSION[
'msg_error'
]
?></
div
>
- <div
class
=
"col-auto"
>
- <a
href
=
"#"
onclick
=
"$(this).closest('.alert').remove()"
class
=
"text-decoration-none text-reset fw-bolder mx-3"
>
- <i
class
=
"fa-solid fa-times"
></
i
>
- </
a
>
- </
div
>
- </
div
>
- </
div
>
- <?php unset(
$_SESSION[
'msg_error'
]
)
; ?>
- <?php endif; ?>
- <?php endif; ?>
- <!--END of Handling Messages Form Session -->
- <div
class
=
"card rounded-0 shadow"
>
- <div
class
=
"card-header"
>
- <div
class
=
"d-flex justify-content-between"
>
- <div
class
=
"card-title col-auto flex-shrink-1 flex-grow-1"
>
Member List</
div
>
- <div
class
=
"col-atuo"
>
- <button
class
=
"btn btn-primary btn-sm btn-flat"
id
=
"add"
><i
class
=
"fa fa-plus-square"
></
i
>
Add Member</
button
>
- </
div
>
- </
div
>
- </
div
>
- <div
class
=
"card-body"
>
- <div
class
=
"container-fluid"
>
- <?php if(
isset(
$data->
id)): ?>
- <p
class
=
"text-muted"
><i
>
Update the details of <b
><?=
isset(
$data->
name) ? $data->name : '' ?></
b
></
i
></
p
>
- <?php else: ?>
- <p
class
=
"text-muted"
><i
>
Add New Member</
b
></
i
></
p
>
- <?php endif; ?>
- <form
id
=
"member-form"
action
=
""
method
=
"POST"
>
- <input
type
=
"hidden"
name
=
"id"
value
=
"<?= isset($data->
id) ? $data->id : '' ?>">
- <div
class
=
"mb-3"
>
- <label
for
=
"name"
class
=
"form-label"
>
Member's Name</
label
>
- <input
type
=
"text"
class
=
"form-control rounded-0"
id
=
"name"
name
=
"name"
required=
"required"
value
=
"<?= isset($data->
name) ? $data->name : '' ?>">
- </
div
>
- <div
class
=
"mb-3"
>
- <label
for
=
"contact"
class
=
"form-label"
>
Contact #</
label
>
- <input
type
=
"text"
class
=
"form-control rounded-0"
id
=
"contact"
name
=
"contact"
required=
"required"
value
=
"<?= isset($data->
contact) ? $data->contact : '' ?>">
- </
div
>
- <div
class
=
"mb-3"
>
- <label
for
=
"address"
class
=
"form-label"
>
Address</
label
>
- <textarea
rows
=
"3"
class
=
"form-control rounded-0"
id
=
"address"
name
=
"address"
required=
"required"
><?=
isset(
$data->
address) ? $data->address : '' ?></
textarea
>
- </
div
>
- </
form
>
- </
div
>
- </
div
>
- <div
class
=
"card-footer text-center"
>
- <button
class
=
"btn btn-primary rounded-0"
form=
"member-form"
><i
class
=
"fa-solid fa-save"
></
i
>
Save Member</
button
>
- <a
class
=
"btn btn-light border rounded-0"
href
=
"./"
><i
class
=
"fa-solid fa-times"
></
i
>
Cancel</
a
>
- </
div
>
- </
div
>
- </
div
>
- </
div
>
- <!-- End of Page Content Container -->
- </
div
>
- </
div
>
- </
body
>
- </
html
>
That's it! Now you can test the simple application on your end and see if it works well and achieve our goal for this tutorial. You can also download that source code zip file on this page by clicking the Download button below this article.
Snapshots
Main Page
Form Page
There you go. That's the end of this tutorial. I hope this JSON File CRUD operation using PHP Tutorial will help you with what you are looking for and that you'll find this useful for your current or future PHP Projects.
Explore more on this website for more Tutorials and Free Source Codes.
Happy Coding :)
Download
You must upgrade your account or reply in the thread to view hidden text.