Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to PHP
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share
Lecture Comments (8)

0 answers

Post by Kitt Parker on January 7, 2015

The curly brace method for multidimensional arrays throws me off a bit.  I feel like it breaks consistency and thus is not ideal.  I would think the ideal solution would have been to just call a multi-dimensional array value with the same syntax.  I wonder why it turned out this way.

0 answers

Post by Anand Richard on July 8, 2013

I keep getting merely the echo'ed statements and not the $_GET array values at all. I have copied the code exactly as it is but no results.

0 answers

Post by eoctech21 on May 22, 2013

Is it possible to submit the form to it's own unique page? To somehow change the action in <form method="get" action="test.php"> and have it be a randomly generated number, similar to how uploading a YouTube video works.

I don't want the information I store in the form to be lost as soon as I get off the page. Users should be able to just copy/paste the link and see what I put.

0 answers

Post by Dixon Kavanaugh on April 25, 2013


Jut a point of clarification for my own understanding. In the homework solutions folder step-4 you have given the names "accountIndexed" & "accountAssoc" - these are names you are using not required syntax, correct?


3 answers

Last reply by: Matthew M.
Wed Apr 11, 2012 3:41 PM

Post by David L on March 1, 2012

Hello, these videos are great. I have a question.

I wanted to test the syntax for "Outputting arrays in a string". 25:29 in the video.

My code:

$_GET['name'] = 'john';
echo "his name is $_GET[name];

It yields this error:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING ... on Line 3.

I have tried changing line 3 to:
echo "his name is $_GET['name']";

as well as changed the variable from $_GET to just $get.

Why is this? I am on a Mac and using XAMPP and the version PHP is 5.3.1 so I am not sure that is the reason for the error. Old version maybe?

Accessing Form Data in PHP

  • $_GET is a pre-defined associative array created by PHP that holds all input data submitted to a PHP script via the GET method.
  • One can have arrays created in $_GET by using a square-bracket syntax when specifying the name attribute for form input controls. For example:
    • <input type="text" name="address[city]" />
  • Variable interpolation in double-quoted strings can be used to output the values of both indexed and associative arrays. There is both a 'simple' and 'complex' syntax available for this.
  • Multi-dimensional array values in double-quoted strings should be output using the complex, or 'curly brace', syntax.
  • Additional Resources:

Accessing Form Data in PHP

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:17
    • Lesson Overview
  • $_GET Array 0:48
    • $_GET Array
    • Accessing the Value of a Name/Value Pair Submitted Via GET
  • Name Form Example 2:54
    • Name Form and the $_GET Array
  • Using Arrays with Input Controls 6:34
    • Using Arrays with Input Controls
    • Common Example
  • Indexed Array Form Example 8:16
    • Indexed Array Form Example
  • Associative Arrays with Input Controls 10:14
    • Associative Arrays with Input Controls
  • Associative Arrays Example 11:41
    • Associative Arrays Example
  • Echo Form Example 15:18
    • Echo Form Example
  • Outputting Arrays In String 23:42
    • Variable Interpolation
    • Outputting a Value in an Indexed Array Within a String
    • Simple' Syntax, 'Complex' Syntax, and 'Curly Brace' Syntax
  • Outputting Arrays In String Example 26:25
    • Outputting Arrays In String Example
  • Homework Challenge 29:39
    • Homework Challenge

Transcription: Accessing Form Data in PHP

Hello, and welcome back to's Introduction to PHP course.0000

In today's lesson, we are going to be expanding the material from last lesson, where we learned how to input data to a script.0004

Today, we are going to learn how to access that data from a PHP script.0012

Specifically, we are going to learn about what is known as the $_get array, which is how you access input that was passed to a script by the GET method.0018

We are going to talk about how we can use input controls to submit information to PHP in the form of an array.0032

We are also going to talk about how to output array values within strings, using variable interpolation.0040

What is the get array, is it is called? The $_get array is an associative array that is created by PHP every time a PHP page is requested by the GET method.0050

What it does is: it goes out, and it extracts any name/value pairs that were appended as part of the query string of the URL.0065

And it puts them into this array: basically, what it does is: for each name/value pair that is appended to the URL,0073

it creates a key, and it sets the key equal to the name of each name/value pair, 0081

and it sets the value associated with that key equal to the value of the name/value pair.0089

For example, if we have a text field, and we have it set at the name firstName,0095

when you submit a form containing that text field, whatever value is assigned to that text field is going to be associated with firstName.0104

So, it's going to be firstName=matthew as part of the query string.0114

What is going to happen is: PHP is going to go and create this get array that is always available in your PHP files.0119

And it is going to create a key called firstName, and it is going to associate with that key the value that you submitted.0128

So essentially, what it is going to be doing--if you remember from our lesson on arrays...0135

So, if we the text field, if we type the text value, essentially what PHP is going to do 0157

is create a key, a name firstName in the get array and assign it the value value.0163

Let's take a look at a script, or a basic HTML file, that shows how some of this works.0176

What we have done is: we have created a basic HTML form that has one text field in it, kind of like we just talked about.0186

And what it allows you to do is submit your name--and actually, before we do that, let's look at the HTML code.0193

It is a form that uses the get method, because that is what we have been using.0202

The action, if you will notice, is something different from what we talked about in the last lesson; it's actually a separate file.0207

In this case, it's a PHP file; and now we are going to learn how to access that information.0212

So, this PHP file, processNameForm.php, is going to be able to use that information that we provide in a text field.0218

If we go ahead and I submit my name, it is going to go to processNameForm.php, 0228

and as you can see, it appended the name/value pair person=matthew to the URL.0237

And what I have done is actually just used the print_r function to output what the GET associative array looks like.0242

And, as you can see, the GET array contains one key and one value, and the key is person, and the value is matthew.0251

And if we actually go back and look at our HTML, we can see that, on the input tag for our text field, the name is person.0262

And so, person directly corresponds with the person key in the GET array.0271

Let's go look at the code that processes the form that processes the name.0282

It's called processNameForm.php, and basically, we have some PHP code here at the beginning that outputs what we had just showed you.0286

It just outputs the contents of the GET array.0295

Let's go ahead and delete that and do something a little more useful.0301

I've commented out a section of code here that is going to output a Thank You message, and it is going to quote the person's name listed on the form.0304

I have to get rid of these comment tags.0316

If we go back to our page and try a different name and click Submit, now what we have done is output a message0324

that says "Thank you," with the name submitted, Joe Smith, "for entering your name; you now have accessed PHP data for the first time.0334

"The sky is the limit," and I created a little link to return to the form.0341

Now, if we go back and look at the code here, all it did was: it has a couple of echo statements that echo some HTML code,0344

and then, one echo statement that references the GET array, 0353

and because the name of the text field which we submitted with the form was called person, we denote the person key and the GET array.0358

What that is going to do is go to the GET array, get the value associated with the person key (which in our case, I think, was Joe Smith),0369

and then it is going to output it--so it is basically going to add it to the output string.0377

That is an introduction into how GET works with simple name/value pairs.0382

Things can get a little more advanced: PHP has a nice feature,0395

in that it allows you to create arrays in the GET associative array that it provides for you.0399

The way you do that is: you provide, in the name attribute of different input elements, a name with opening/closing square brackets.0408

When you do that, and when you submit the form, what that tells PHP is that any name/value pair that has the name items,0422

for example, item...this one here...equals 1001--it is going to create a key called items, and then it is going to associate with that key an array.0433

And for every name/value pair that has the name items, it is going to add that value to that array.0450

A typical use of this is in select dropdown menus, where you can select multiple items from the list.0458

And the way that you access the values within your PHP script is: as we learned in the multidimensional array section of the array lesson,0469

you specify GET, and then, in this case, items; so that is saying "Get me the array associated with the key items."0481

And then, you specify a numeric index to access the different values in the array.0489

If we go and look at another form called indexArrayForm.html, we can see, we have a select box where we can select multiple things.0497

The way you do that is with the control key--Ctrl+click.0508

We can submit it, and what you can see here is: again, to demonstrate how the GET array is formed, you can see that 0513

(I'll blow it up a little bit), using the print_r statement, the GET array contains one key/value pair.0525

The key is named items, and it contains two elements--1001 and 3001--which are the ID's of the product that we selected.0533

They have the indexes 0 and 1.0544

Now, if we actually go and look at the HTML for the form, we can see that we have declared, as in the slide, a select dropdown menu,0546

given it the name items with the opening and closing square bracket, and then we have simply added option tags with different values.0557

What is going to happen is: when you submit that form, the browser is going to append to the URL (because we are using the GET method in this form)0567

items= , and then it is going to list the item ID of each item we chose from the list.0578

So, if we go back and look at when we submitted the form, you can see, in the top URL here, there is items listed twice in the query string--0584

items=1001 and items=3001, and because there are brackets associated with those items, PHP is going to know that 0597

that is going to refer to an array, and it's going to create an array with those values.0604

We just saw how PHP--you can use indexed arrays, or numeric arrays, in your forms; you can also use associative arrays.0616

For example, we can have two text input boxes, or text fields, and we can assign them a name like this,0625

where the array would be called person, and the key of the array would be called first.0637

We can also do it for another one with last.0643

So basically, what this is going to do is: when you submit this form, PHP is going to parse the URL.0647

It is going to create a key in the GET array named person, to which it is going to associate 0653

an associative array with the key first and the key last, along with whatever values were submitted to those corresponding text boxes.0663

One thing to note is that, unlike in PHP code (when we reference something in an associative array, you enclose it in quotation marks),0676

when you do this in HTML, you don't do that; you leave out the quotation marks; and PHP will know how to handle that.0691

Let's take a look at how associative arrays work.0701

We have a form associativeArrayForm.html, and it is going to allow us to input a first and last name.0713

We submit the form; we are going to do the print_r again on the GET array, and it is going to show you that the GET array0722

contains one key/value pair; the first key, or the only key, is named person, and it is associated with a data value,0733

which is an associative array that has two keys, first and last, and first is set to matthew; last is set to machaj,0742

which were the first and last names that were entered into the text field.0750

And so, if we go and look at the HTML form itself, we can see that we have two text input fields that we created.0757

And using the square bracket syntax, we typed person[first] to correspond to the firstName in the person array,0766

and then, this one right here corresponds to the lastName in the person array.0780

So, these are directly correlated to these in the GET array.0783

Now, if we go and look at the code...let's get rid of the print_r statement, and we can do something a little more useful than that.0793

This is going to demonstrate how to access the different values in the GET array that created an associative array called person.0805

One way we can do that is: we can declare a variable called person and set it equal to 0820

the value associated with the person key in the GET associative array, which, as we saw in our case, is this array here.0827

It contains a first key and a last key, and then the values associated with them.0836

And then, what we do is: we just create an output message to output the person's name, giving the welcome message.0841

And so, we have a couple of echo statements; here is just a welcome statement.0848

And then, because this is an associative array, we use the square bracket syntax for associative arrays 0852

to access the name associated with the key first, which was what was entered in the first text box.0858

And then, we also output the last name, which we access by the last key.0865

And if we go ahead and load this in the browser--let's try a different name...oh, I guess I didn't save it; let's try one more time...0871

You can see, it says Welcome, and then John Smith; and this John Smith...this is the first name 0895

that was outputted from the person associative array, and Smith was the last name that is part of the person array, as you can see here.0901

So, I have also created another script that shows more form input controls and how they work, similar to in the last lesson.0915

We go to echoForm.html (shrink this down a little bit): this is similar to a form we saw in the last lecture.0928

It has a text field, a password field, a radio button, a check box, a multiple selection box, text field, hidden field, and so forth.0938

If we go and put in some information like username, password, male, are you married, and then select a couple of our favorite RGB colors,0951

enter a comment, and click Submit...we are going to use the print_r function again on the GET array, 0963

and that is going to show you what was passed on by the GET method.0974

We can see that, in the GET array, there was a text field named username, and the value associated with it was username.0979

There is a password field, a text field (I misspelled password) that is associated with the value password.0986

Additionally, on this form, for the two questions sex and the check box "Are you married?" I have created an associative array0996

called personal info, and given it two keys, sex and married.1012

And if we go and look at the HTML for the form, we can see that, for the radio buttons, we have declared the name personalInfo,1019

and then put in square brackets the name of the key we want to assign that value to--in this case, sex.1036

And then, if we look down here at the married check box, we have assigned it the name personalInfo as well,1043

which means to put it in the same associative array as this up here, except it's going to use the key married.1050

And so, that is why, when we submit the form, it creates this personalInfo array.1059

Additionally, we have also created a numerically indexed array called favColors,1065

and what that was for, was for when you had the option to select multiple favorite RGB colors.1069

And so, what happens in that one is: as we had seen in the indexed array example before, for the select tag,1079

we give it the name favColors, and then follow it by the opening/closing square brackets, 1089

which tells PHP, when it finds any key/value pairs that have the name favColors, that they are going to be part of one array.1094

Because no index is specified, it is going to be a numerically indexed array.1102

And then, any options that are selected are added to this array.1105

So, in this case (let's go ahead and try again) we'll select blue and green, and we submit it.1109

We can see that it has added green and blue to the favColors array; and they have the index 0 and 1.1116

Additionally, the large text area Comments has the string that we included associated with it.1124

And also, down here, you can see, it has the value of a hidden tag.1131

We had a hidden input tag in our HTML; you go down to the bottom and look at it: input type, and we called it hidden.1136

We assigned it the name hidden, and then we gave it the value IP,1147

which may be some data that is of use to our script, but we don't necessarily need the user to see.1154

And so, when we loaded up the form and output the GET array, we can see that that hidden value,1160

even though it wasn't actually submitted by the user, does get put into the GET array.1169

Let's look again at the the processEchoForm.php.1184

What it does is: as the other files have done, it just uses a print_r statement to output the contents of GET.1193

But we can use it, again, to do something more useful.1201

In this case, we are just going to simply echo (and that is why it is called an echo form) all of the data that was submitted by the form.1206

As you can see here, we have a bunch of echo statements, in which we create an unordered list,1213

and for each list element, we output the value associated with each input field.1221

For example, for the username text box, we are going to use the GET array.1228

Access the GET array; give it the key username, which was the name in echoForm.html, which was associated with the username text field.1232

And we do that for all of the different input boxes; so we do it for password...1248

Additionally, as you can see down here, we had mentioned that we created an associative array, personalInfo, with the keys sex and married.1253

And as you can see here, the way we access that data from the GET array is using the double square bracket syntax used for multidimensional arrays.1262

And to review that again, what this first part here says is: Give me the value associated with the personalInfo key in the GET array.1272

That happens to be an associative array, and it contains different keys, and one of them is sex.1283

And so, this says to get the value associated with the key sex.1289

This one does the same thing, except it does "Get the value associated with the key married."1294

Now, down here, what we have done is: for the favorite colors, we look back at the HTML; we named it favColors with an opening and closing bracket,1300

which is going to create an indexed array in the GET array; PHP is going to do that automatically.1316

And what we can do is: we can output, in this case, the first color selected.1324

The first item in an indexed array always begins with the 0 index, if you remember.1328

And because this is a multidimensional array, we use the square brackets index again.1335

And this, again, says, "Give me the value in the GET array associated with the key favColors, which is itself going to be an array."1339

"And then, give me the first element in that, which depends on what we selected in the text box."1346

This just outputs the comments and hidden (remove this last comment and save).1354

If we reload the form, and let's pick red and green this time...female...password...New comment, and hit Submit, we can see the formatted output.1359

It says the username we entered was username; password was pawd; it outputs the sex of the user and whether they are married or not.1380

It outputs the first color selected, which in this case was red.1391

We change it, and then green and is going to output green, because green will be the first element added to the favColors array.1395

And then, you can see, it outputs, in a prettier format, the content of the comment input element and what was the value of the hidden input element.1407

So now, I want to continue on a topic that we have covered in a previous lecture, about variable interpolation.1423

And in this case, we are going to talk about how to output array variables within a double-quoted string.1428

We know that we can do that for, for example, integer, float, and other string variables.1434

We can include them within the double quotes of another string, and the echo statement will go ahead and replace the variable with its value.1439

But we can also do that for arrays; this is quite useful.1446

There is particular syntax for that: for indexed arrays, such as here, what you do is: 1450

you access the value of the array, simply as you would in your PHP code.1467

You simply type the variable name, which is a dollar sign followed by the variable name, 1471

and then square brackets with the integer index enclosed in between them.1477

And what that is going to do is replace this variable value with whatever the second value is in this indexed array.1482

That is pretty intuitive, because it's exactly the same way you would access it in PHP code.1494

Now, for associative arrays, it's a little bit different.1501

It's similar syntax, except that, if you notice, there are no quotes around the key name.1505

So, you access it in the same way you would in a PHP file, except you leave the quotes out.1512

This is known as the simple syntax; there is also a complex, or curly brace, syntax, which is used for outputting more complex things within strings.1520

And as we go through the course, we will find different, more complex things--for example, like object values and function output--to output.1530

But, if you want to use the quotes, and you feel more comfortable that way, you can enclose everything in curly braces and add the quotes.1540

Now, this curly brace syntax is also used for complex things--one of those being if you want to output a multidimensional array.1550

Let's say assoc was a multidimensional array; this is how you would specify that in your code.1566

Let's go take a look back at the process echoForm.php, and put some of this into action.1587

What you will see is: it is going to turn this...right now, we have three echo statements to output 1594

a list item that says username, and then outputting the username that was provided.1599

We can actually turn these three lines of code into one, and that is through variable interpolation.1603

Because this is an associative array, we go ahead and remove the quotation marks.1611

And if we go and load this page up, and submit it, you can see that it outputs the username, just as we expected it to before.1621

As you can see, it definitely makes the code a little easier to read--less lines to accomplish the same thing--and it's pretty intuitive.1641

You know that it is going to replace this GET[username] with whatever value is in the GET array, associated with the key username.1649

I'm just going to go ahead and quickly do this for these other values.1658

This is a multidimensional array, so we have to use, as mentioned, this special curly brace syntax.1670

And with curly brace syntax, we leave the quotation marks in.1675

The same goes for multidimensional arrays containing one dimension, or one of the arrays, as an indexed array.1696

I'm sorry, I forgot up here to get rid of the single quotes.1728

And so now, we have gone and changed all of those lines, which was three lines per list item, into one line.1731

And when we go ahead and load this page up again, it is going to output the same information, 1739

but we are making use of variable interpolation with associative arrays and indexed arrays.1744

Let's go ahead and...if we go ahead and submit it, we can see that everything outputs as we had expected from before.1752

That is how to make use of variable interpolation for arrays.1771

And you find that you will be using that a lot, throughout your PHP code.1775

For today's homework challenge, I am going to have you get practice with using the GET array.1781

You are going to be using it all the time in your PHP scripts, because you are constantly interacting with the user and receiving form input.1786

I want to have you create a regular HTML file that contains an HTML form, and just put it in the lecture_12 directory.1793

And have the form contain all of these different input controls.1802

And then, create another file, a PHP script, that is just basically going to echo the data submitted by the HTML form.1809

And make sure that, when you create this PHP script, you set the action attribute of the form element to whatever you named your HTML file up here.1818

And so...actually, the form element is going to be in the HTML file; make sure that you set the action 1832

equal to the name of the PHP script that is going to be echoing the data.1837

Then, what I want you to do is experiment with outputting and accessing values of the GET array.1843

So, use non-array names for your controls--for example, like name=text1; and just practice outputting those different values from your PHP script.1849

Then, change some of the controls, so that they have array names, like we talked about.1867

Maybe you will have a select dropdown box that will have the name...maybe it's states; and create it like that.1870

And do that for both the indexed array and the associative array variety, and practice outputting it.1884

And, when you output the value of the GET array, practice using what we learned in the last slide, 1889

which was variable interpolation that you can use with double-quoted strings to output the values of arrays.1897

Experiment with both the simple and complex syntax.1904

The simple syntax is just the variable name, and the complex syntax actually uses the curly braces around it.1908

That ends today's lesson; thank you for watching, and I look forward to seeing you next lesson.1916