Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Updating your JSON data

From: Working with Data on the Web

Video: Updating your JSON data

In this lesson, I'm going to demonstrate one technique for storing values entered through a form into JSON format. We'll be using PHP, but most server side languages equivalent syntax. As you'll see, it's surprisingly straightforward. So I'll go to my code editor Where I've got index.php from the Chapter Four 04, 03 folder up and running. Now there's a form on this page starting around line 40 I believe. Yep, there it is. That asks for name, email, and date of birth, and other data.

Updating your JSON data

In this lesson, I'm going to demonstrate one technique for storing values entered through a form into JSON format. We'll be using PHP, but most server side languages equivalent syntax. As you'll see, it's surprisingly straightforward. So I'll go to my code editor Where I've got index.php from the Chapter Four 04, 03 folder up and running. Now there's a form on this page starting around line 40 I believe. Yep, there it is. That asks for name, email, and date of birth, and other data.

The code we'll be using is not form specific however, and can be used with most if not all forms. So we're going to put our PHP block however at top of the file. I'll make a little room and start off with our PHP code block. The form on this page is setup using the POST method and an empty action so when it's submitted the information will be posted to the same page. To make sure the code is run only when that happens, we'll add an if statement that checks to see if submit is set in the posted data.

If it is, it means that the Submit button was clicked. So after the keyword POST 'submit'. And then after the whole if statement, a curly brace pair. And now we are at the heart of the function, and it's just four lines of code. Two of which are establishing variables. So, let's first create a variable for our adjacent data file name path.

And I'm going to call this $file and set that equal to, in quotes, data.JSON, semicolon at the end. Next, we'll create a variable to hold the posted JSON data. The JSON encode function that we're going to be using has been around for a bit. But the JSON pretty print functionality that's an attribute, was only added in PHP 5.4, so you'll need a fairly recent version of PHP to take advantage of this.

Without it, the code will be one long string, which is workable, but just not as easy to read. So, the variable Jason underscore string and we'll set that equal to the function JSON_encode. Open and closed parenthesis, two arguments. The first is what are we going to encode. It's the POST array. Follow that with a comma, and then all uppercase.

You may want to hit your caps lock key for this one. JSON_PRETTY_PRINT. Make sure you turn that Caps Lock key off if you haven't yet. And then close it all of with a semicolon. Now, we're ready to write out our data. Because we're just adding record after record, we can use PHP's file put content function with it's file append argument. So the function is file, underscore, put, underscore, content.

Then an open and close parentheses, which file are we going to put it into. And we'll use our variable $file comma, what are we going to put in it, our other variable. $json_string, and then the other parameter, FILE_APPEND. Alright, close that off with a semicolon. Lastly, we'll redirect the user to a thank you page. And to do that we'll use the header function, header parenthesis, single quotes, location, colon, thanks.php, and the semicolon rounds it all out.

Thanks to JSON's wide support within the PHP community and its advanced related functions, that's all the code we need. So let's save the page and try it out on our local web server. I'll refresh the page to make sure we got everything we need, and then let's enter in some data. This time I think I'll honor one of my other cats who is named Tao and taocat@lowery.com, fictitious email address. Let's give Tao Their own personal phone number of course, and lets see if I can remember Tao birth date.

I do believe it was April 2nd and it was a while ago. Well not quite that long ago. All right Tao was a master of the arts as you can imagine. I'll hit submit. All right, we made it to our thank you page without triggering an error. That bodes well. Let's head over and take a look at our data file. Now I already have it open here, so when I switch to it, I get a file change message. I click yes. And there's my Json data.

I think it looks pretty good. One might even say, pretty. Alright let's run one more test, so I'll go back to the browser hit my back key and then refresh to clear that out. And after giving all my pets a little shout out maybe it's time to bring the daughter into it. So we'll just do an initial there. Believe it or not, she has her own cell phone.

And, we'll hit Submit. Back to check the data. File changed. Bingo! We've got another record, nicely formatted. Looks like our work here is done. Excellent.

Show transcript

This video is part of

Image for Working with Data on the Web
Working with Data on the Web

25 video lessons · 10077 viewers

Joseph Lowery
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Working with Data on the Web.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.