• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Save Data To Text File Using PHP

adolfm

NFT Marketplace Expert
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
168
Likes
57
Bits
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".

  1. <?php
  2. session_start

    (

    )

    ;
  3. if

    (

    isset

    (

    $_SESSION

    [

    'ERRMSG_ARR'

    ]

    )

    &&

    is_array

    (

    $_SESSION

    [

    'ERRMSG_ARR'

    ]

    )

    &&

    count

    (

    $_SESSION

    [

    'ERRMSG_ARR'

    ]

    )

    >

    0

    )

    {
  4. foreach

    (

    $_SESSION

    [

    'ERRMSG_ARR'

    ]

    as

    $msg

    )

    {
  5. echo

    '<span style="color:red;">'

    ,

    $msg

    ,

    '</span>'

    ;
  6. }
  7. unset

    (

    $_SESSION

    [

    'ERRMSG_ARR'

    ]

    )

    ;
  8. }
  9. ?>
  10. <form action="save.php" method="POST">
  11. <span style="display: inline-block; width: 150px;">Name</span><input type="text" name="name" required="required" /><br>
  12. <span style="display: inline-block; width: 150px;">Age</span><input type="text" name="age" required="required" /><br>
  13. <span style="display: inline-block; width: 150px;">Gender</span><select name="gender"><option>male</option><option>female</option></select><br>
  14. <span style="display: inline-block; width: 150px;">&nbsp;</span><input type="submit" value="Save" />
  15. </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".

  1. <?php
  2. session_start

    (

    )

    ;
  3. $errmsg_arr

    =

    array

    (

    )

    ;
  4. $errflag

    =

    false

    ;
  5. $name

    =

    trim

    (

    $_REQUEST

    [

    "name"

    ]

    )

    ;
  6. $age

    =

    trim

    (

    $_REQUEST

    [

    "age"

    ]

    )

    ;
  7. $gender

    =

    trim

    (

    $_REQUEST

    [

    "gender"

    ]

    )

    ;
  8. $filename

    =

    "informationlist.txt"

    ;
  9. $MyFile

    =

    fopen

    (

    $filename

    ,

    "a"

    )

    ;
  10. $newline

    =

    $name

    .

    ','

    .

    $age

    .

    ','

    .

    $gender

    .

    "\r

    \n

    "

    ;
  11. fwrite

    (

    $MyFile

    ,

    $newline

    )

    ;
  12. fclose

    (

    $MyFile

    )

    ;
  13. $errmsg_arr

    [

    ]

    =

    $name

    .

    ' data has been save to '

    .

    $filename

    .

    ' file'

    ;
  14. $errflag

    =

    true

    ;
  15. if

    (

    $errflag

    )

    {
  16. $_SESSION

    [

    'ERRMSG_ARR'

    ]

    =

    $errmsg_arr

    ;
  17. session_write_close

    (

    )

    ;
  18. header

    (

    "location: index.php"

    )

    ;
  19. exit

    (

    )

    ;
  20. }
  21. die

    ;
  22. ?>

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.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,499

349,752

349,762

Top