Jump to content
Larry Ullman's Book Forums

Recommended Posts

I probably have some kind of visual/spacial related learning disability. I know that a form sends data to the server where php processes it and outputs it to the browser. Words alone don't always work for me. I have to draw things as if I were trying to explain it to someone else. Some times that helps me to see that my logic simply doesn't compute. Thought I was good to go with arrays...

 
$var = array('dog', 'cat', '2', 'blue');
 
$var now contains all the values in the array. But, in the book, the syntax (using post) is:
 
$title = $_POST['title'];
$name= $_POST['name'];
$email= $_POST['email'];
 
To this newb it appears that the array concept has been abandoned for a multi-variable "device". I realize it's my inability to grasp it. 
 
(Tried to post the drawing but got "that extension not allowed" with png, gif, jpg. Finally gave up and put in a link instead)
 
My drawing shows what I think happens. (I like to think I--at least-- have that much right.) But it still seems clunky. Would like to get a grip on this and share with other seniors (dob: 12-7-45). 
 
 
 
 
 
 

Share this post


Link to post
Share on other sites

One thing I'd point out is that $_POST['email'] is not equal to $_POST['bb@aol.com']. However, if bb@aol.com is entered for the email field in a form, then $_POST['email'] will be equal to the string 'bb@aol.com'.

 

To make things simpler to understand, just imagine that $_POST is like any other array variable.

By that rationale, we could replace the name $_POST with any other valid variable name like $arr, $a, $b, $benny, $cars, etc. For the sake of argument, let's use the variable name $arr instead of $_POST. Now we're left with $arr['email'].

 

The thing about arrays is that there are two types of keys: integer and string. By default, arrays use sequential integer keys starting at 0 and incrementing by 1 for each item in the array. In that case, things like $arr[0], $arr[1], etc. would refer to valid values in an array stored in the variable $arr.

 

However, strings can also be used for keys, which is the case for the $_POST array. As such, $_POST['email'] (or $arr['email']) is essentially the same thing as $arr[0]. Both 'email' and 0 are keys used to refer to a value in the array. It's just a matter of whether you use an integer or a string to index that value in the array.

 

Given all the above, the creators of PHP decided a long time ago that the most logical way to store POST form values in an array was to create a superglobal array called $_POST that has string index values, and make those string indexes equal to the name attributes of the corresponding input, select and textarea elements in the form.

 

As a final note, arrays that contain string indexes are also sometimes called associative arrays, in case you ever come across that phrase.

  • Upvote 1

Share this post


Link to post
Share on other sites

Okay. Letting that soak in. Feel another pic coming on.

Yep. http://chattanoogacentral1964.com/drawing.php

 

 
What a minute. Maybe it just clicked. 
 
text area goes over as  "now is the time for all good men to come to the aid of their country"
 
so that ...
 
$comments = $_POST['comments' => 'now is the time for all good men to come to the aid of their country.'];
 
print "<p>If we hope to win this war-- $comments Who's with me?</p>"
 
Nope -- it produces parser error. Doesn't like the double arrow. Is the concept right at least?

Share this post


Link to post
Share on other sites

There are three issues with your code:

 

1) You cannot take an existing array (i.e., the $_POST array), add a new index and value to it, and then in the same operation, assign that array to another variable. You need to separate that out into two separate operations. The following works:

 

$_POST = array('comments' => 'now is the time for all good men to come to the aid of their country.');  
$comments = $_POST;
 
2) You cannot print out an array like you can an integer or a string. You need to provide an array index to print out a particular value, and also, you have to wrap the array expression in curly brackets so that the PHP parser doesn't get confused. The following would work:
 
print "<p>If we hope to win this war-- {$comments['comments']} Who's with me?</p>";
 
3) As you can see with my line of code above, you must end the statement with a semicolon, of you will get a syntax error.
 
So putting that all together, you get the following working script:
 
<?php
  
  $_POST = array('comments' => 'now is the time for all good men to come to the aid of their country.');  
  $comments = $_POST;
  print "<p>If we hope to win this war-- {$comments['comments']} Who's with me?</p>";

Share this post


Link to post
Share on other sites

I think I sabotaged my own thread by entering some code AND entering a link for something else in the same reply box. But, basically, I feel that I have it thanks to your hanging in there with me.

 

I knew that you couldn't print out an array without doing something you wouldn't do a simple variable/value pair. I also knew about the need for a semi-colon. Newbie mistake. And so your point #1 really explains things. I just need to really study it so that it eventually becomes very clear. I've always been my worst enemy for wanting to take the clock apart instead of just reading the time. It's too early in the game for that. I need to just learn how to do things and come back to the why later. Thank you so much. I'll be back with more questions, Hopefully they will be more typical.

Share this post


Link to post
Share on other sites
Just can't leave it alone. Please ck to see if this is a correct interpretation of your efforts to help me:

 

<?php

 

$_POST = array('comments' => 'now is the time for all good men to come to the aid of their country.');  

$comments = $_POST;

// above: $_POST is used as a variable. $dog would have worked just as well. $_POST was assigned to the contents of the array.

// 'comments' is used as a key because it came over with the html form. 

// $comments gets assigned to the contents of the array formerly known as $_POST

// to print or echo $comments, we must specify an indexed part of the array (below)

 

print "<p>If we hope to win this war-- {$comments['comments']} Who's with me?</p>";

// above: $comments is the array being referenced in curly braces

// 'comments' is key being reference in the array. key is aka "0".

// the value of the 'comments' key is "now is the ...."

 

 $_POST = array('comments' => 'now is the time for all good men to come to the aid of their country.');  

  $comments = $_POST;

  print "<p>If we hope to win this war-- {$comments['comments']} Who's with me?</p>";

  // php knows to insert the value* of the key** of the array*** 

  // * now is the time....

  // ** 'comments'

  // *** $comments

  

?>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...