Jump to content

How to see what is in $_POST array?


TomTees

Recommended Posts

It would be in the page that receives the information from your form, so the page the form submits to.

The page where your form is wouldn't have any of the posted data, unless it was also posted to from another page.

 

So there is no data in the $_POST array until after you click "submit" and that data is sent over in the HTML header to the other page and so it can't be accessed from the originating page, right?

 

 

TomTees

 

 

Link to comment
Share on other sites

Zacly.

 

When I put in the code you suggested, I got this...

 

Array

(

    [firstname] =>

    [btnSubmit] => Register

)

 

Is it that simple?

 

Does the $_POST array simply how the name of each form control (i.e. Key) and the value that you type in (i.e. value)?

 

Could it ever contain something else?

 

 

 

TomTees

 

 

Link to comment
Share on other sites

What else would you want it to contain?

 

Oh, nothing in particular, just trying to get a handle on things.

 

 

It can contain values of any variables from the first page through hidden fields for example.

 

 

How do you do "hidden fields?

 

 

 

TomTees

 

Link to comment
Share on other sites

Sorry if I am full of questions this morning, but I'm just trying to re-learn everything I have forgotten when I first took up web development!!  :-[

 

 

 

<input type="hidden" name="whatever" value="some_value">

 

 

What is the purpose of using hidden HTML form controls?

 

And you are saying that these "hidden" values are not viewable by the person using the form, however they are transmitted with all of the "UN-hidden" form values when the user clicks "submit"?

 

So the $_POST array would contain both hidden and un-hidden values, right?

 

 

 

TomTees

 

 

Link to comment
Share on other sites

Correct.

 

For example you can do this:

 

 

$variable = "A hidden value";//Then you make your form here...echo "<input type='hidden' name='something' value='$variable'>";// Rest of your form and submit button

 

 

So then on the next page you could do...

 

 

$variable = $_POST['something'];echo $variable;//This would echo out "A hidden value"

 

Link to comment
Share on other sites

 

And you are saying that these "hidden" values are not viewable by the person using the form, however they are transmitted with all of the "UN-hidden" form values when the user clicks "submit"?

 

So the $_POST array would contain both hidden and un-hidden values, right?

 

 

 

Not exactly. The hidden form element is not displayed by the borwser, but it is present in the HTML source. To see it, all one needs to do is 'View Source' in the browser.

 

The value is present in the $_POST array, like any other form value.

Link to comment
Share on other sites

You can do it in a loop:

 

<?phpforeach ($_POST as $field => $value) {$$field = $value;}?>

 

The "$$field" is a variable variable

 

But, why bother creating another variable, just use the values directly from the $_POST array:

 

<?phpecho "Your email address as entered is {$_POST['email']}";?>

 

 

Ken

Link to comment
Share on other sites

Not sure I'm following.

"put these key/value pairs into variables in my "action" page, for example..."

 

 

Let's say my form is on the "index.php" page.

 

And let's say my "action" page that the HTML form is pointing to is "handler.php" page.

 

When the user clicks "submit", you all have reassured me that the form values will be stored in key/value pairs in the $_POST array, right?

 

And you have said that the $_POST array - which contains these key/value pairs - will be viewable/available to the "action" page (i.e. "handler.php"), right?

 

Well, let's say now that I want to take these key/values pairs out of the $_Post array and store them in local variables in my "handler.php" page.

 

My question was, "How do I do that?"

 

Hope that is clearer.

 

 

 

TomTees

 

 

 

Link to comment
Share on other sites

You can do it in a loop:

 

<?phpforeach ($_POST as $field => $value) {$$field = $value;}?>

 

The "$$field" is a variable variable

 

But, why bother creating another variable, just use the values directly from the $_POST array:

 

<?phpecho "Your email address as entered is {$_POST['email']}";?>

 

 

Ken

 

Link to comment
Share on other sites

Not exactly. The hidden form element is not displayed by the browser, but it is present in the HTML source. To see it, all one needs to do is 'View Source' in the browser.

 

The value is present in the $_POST array, like any other form value.

 

 

So I shouldn't be storing the # to my Swiss Bank Account in hidden form elements?!  :)

 

 

 

TomTees

 

Link to comment
Share on other sites

You can do it in a loop:

<?php
foreach ($_POST as $field => $value) {
$$field = $value;
}
?>

The "$$field" is a variable variable

 

I'm not sure that I follow this code...

 

 

But, why bother creating another variable, just use the values directly from the $_POST array:

<?php
echo "Your email address as entered is {$_POST['email']}";
?>

 

Because it is easier to work with variables that an ugly {$_POST['email']}.

 

 

 

TomTees

 

Link to comment
Share on other sites

Not exactly. The hidden form element is not displayed by the browser, but it is present in the HTML source. To see it, all one needs to do is 'View Source' in the browser.

 

The value is present in the $_POST array, like any other form value.

 

So I shouldn't be storing the # to my Swiss Bank Account in hidden form elements?!  :)

 

 

 

TomTees

 

 

Not until you send me the URL of that page, you shouldn't.

Link to comment
Share on other sites

I've got to ask if you understand the basic client(browser)/server relationship that is going on and what the flow of information is in this process?

 

The browser makes a HTTP request for a web page, lets say your index page with the HTML of your form on it. If your form happens to contain hidden form field(s), they are part of the HTML that makes up that form and they are output to the browser.

 

The web server simply outputs the requested page. The web server then goes on to service other HTTP requests. It is not sitting there expecting your form or any other form to be submitted and in fact it does not know or care that the page that it just output contains a form at all.

 

The browser renders the HTML/CSS/Javascript on that page. At this point in time, you have a form in front of you (well assuming that your HTML/CSS was valid and that if there is any javascript that javascript is enabled in the browser.)

 

When (or if) you finally submit that form, the browser makes a HTTP request to the URL that is in the action="" attribute. As part of that HTTP request, the browser sends the form field name/values to the server. If the form happened to contain hidden form fields when it was originally output, those hidden fields are sent to the server as well.

 

When the page that is in the action="" attribute of the form is requested, the form field name/values that were submitted are available to it and it can do whatever it wants or needs to do when it processes that data.

 

The action="" attribute can be anything you want. It can be empty or not present and the form will submit to the same URL as the page the form is on (the form and the form processing code is on the same page.) It can be for a different page on your server (the form and the form processing code are on separate pages on your server.) It can even be to a page on a completely different server.

Link to comment
Share on other sites

Okay, I'm getting confused...

 

(Not sure what I'm trying to do?!)

 

A user submits a form on page 1.  On page 2, I want to grab data from the $_POST array, sanitizes it and whatever else, and then I want to do one of the following...

 

1.) Assign the values to predefined variables

 

2.) Create variable names based on the $_POST array's key field names, and then assign those newly created variables the $_POST array's values

 

3.) Assign the key/value pairs in the $_POST array to my own array, but keeping the same key/value structure.

 

I definitely want/need to get the values out of the $_POST array because I am going to be changing that data and need to store it somewhere else when I'm done.

 

What the best way to do this is beyond me.

 

One more thing...

 

I am trying to learn OOP, so however I capture and store the data from my $_POST array, I want it stored in a class (e.g. "User").  That really shouldn't affect anything, but I did want to mention it!!

 

Can someone help me get a handle on all of this?!

 

 

 

TomTees

 

 

Link to comment
Share on other sites

I've got to ask if you understand the basic client(browser)/server relationship that is going on and what the flow of information is in this process?

 

Yes, I think I followed (and already knew) your thorough description, although I'm admittedly a bit rusty on things like the hidden fields.  (I forgot about those!)

 

Thanks,

 

 

TomTees

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.