adolfm
NFT Marketplace Expert
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
This tutorial will teach you on how to save data on text file using php. The feature of this tutorial is it will allow you to save information without using any database. It also provide a confirmation message that you have successfully save the data in the text file. To understand more this tutorial follow the steps bellow.
Creating Our Form
The code bellow will display the form and the confirmation message generate by our php server. Copy the code bellow and save it as "index.php".
Creating Our Save Scripts
the code bellow will allows us to save data into the text file using the "fwrite" function in php. Copy the code bellow and save it as "save.php".
That's it you've been successfully a system that save data to text file.
Download
Creating Our Form
The code bellow will display the form and the confirmation message generate by our php server. Copy the code bellow and save it as "index.php".
- <?php
- session_start
(
)
;
- if
(
isset
(
$_SESSION
[
'ERRMSG_ARR'
]
)
&&
is_array
(
$_SESSION
[
'ERRMSG_ARR'
]
)
&&
count
(
$_SESSION
[
'ERRMSG_ARR'
]
)
>
0
)
{
- foreach
(
$_SESSION
[
'ERRMSG_ARR'
]
as
$msg
)
{
- echo
'<span style="color:red;">'
,
$msg
,
'</span>'
;
- }
- unset
(
$_SESSION
[
'ERRMSG_ARR'
]
)
;
- }
- ?>
- <form action="save.php" method="POST">
- <span style="display: inline-block; width: 150px;">Name</span><input type="text" name="name" required="required" /><br>
- <span style="display: inline-block; width: 150px;">Age</span><input type="text" name="age" required="required" /><br>
- <span style="display: inline-block; width: 150px;">Gender</span><select name="gender"><option>male</option><option>female</option></select><br>
- <span style="display: inline-block; width: 150px;"> </span><input type="submit" value="Save" />
- </form>
Creating Our Save Scripts
the code bellow will allows us to save data into the text file using the "fwrite" function in php. Copy the code bellow and save it as "save.php".
- <?php
- session_start
(
)
;
- $errmsg_arr
=
array
(
)
;
- $errflag
=
false
;
- $name
=
trim
(
$_REQUEST
[
"name"
]
)
;
- $age
=
trim
(
$_REQUEST
[
"age"
]
)
;
- $gender
=
trim
(
$_REQUEST
[
"gender"
]
)
;
- $filename
=
"informationlist.txt"
;
- $MyFile
=
fopen
(
$filename
,
"a"
)
;
- $newline
=
$name
.
','
.
$age
.
','
.
$gender
.
"\r
\n
"
;
- fwrite
(
$MyFile
,
$newline
)
;
- fclose
(
$MyFile
)
;
- $errmsg_arr
[
]
=
$name
.
' data has been save to '
.
$filename
.
' file'
;
- $errflag
=
true
;
- if
(
$errflag
)
{
- $_SESSION
[
'ERRMSG_ARR'
]
=
$errmsg_arr
;
- session_write_close
(
)
;
- header
(
"location: index.php"
)
;
- exit
(
)
;
- }
- die
;
- ?>
That's it you've been successfully a system that save data to text file.
Download
You must upgrade your account or reply in the thread to view the hidden content.