TomTees Posted September 25, 2010 Share Posted September 25, 2010 Is there an easy way to dump out the contents of a $_POST array and display them on my screen? (I'm trying to get more comfortable with what is stored in the $_POST array and how it is structured.) Thanks, TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/ Share on other sites More sharing options...
Pikachu2000 Posted September 25, 2010 Share Posted September 25, 2010 echo '<pre>'; print_r($_POST); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115466 Share on other sites More sharing options...
TomTees Posted September 25, 2010 Author Share Posted September 25, 2010 echo '<pre>'; print_r($_POST); echo '</pre>'; Do I put that in the page with my HTML form, or can it be in the "action" page that appears after the form is submitted? TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115468 Share on other sites More sharing options...
Miss_Rebelx Posted September 25, 2010 Share Posted September 25, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115470 Share on other sites More sharing options...
TomTees Posted September 25, 2010 Author Share Posted September 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115476 Share on other sites More sharing options...
Pikachu2000 Posted September 25, 2010 Share Posted September 25, 2010 Zacly. Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115478 Share on other sites More sharing options...
TomTees Posted September 25, 2010 Author Share Posted September 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115481 Share on other sites More sharing options...
Miss_Rebelx Posted September 25, 2010 Share Posted September 25, 2010 What else would you want it to contain? It can contain values of any variables from the first page through hidden fields for example. Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115482 Share on other sites More sharing options...
TomTees Posted September 25, 2010 Author Share Posted September 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115484 Share on other sites More sharing options...
Pikachu2000 Posted September 25, 2010 Share Posted September 25, 2010 <input type="hidden" name="whatever" value="some_value"> Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115485 Share on other sites More sharing options...
TomTees Posted September 25, 2010 Author Share Posted September 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115486 Share on other sites More sharing options...
Miss_Rebelx Posted September 25, 2010 Share Posted September 25, 2010 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" Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115487 Share on other sites More sharing options...
TomTees Posted September 25, 2010 Author Share Posted September 25, 2010 So let's say I had a simple form that yielded the following... Array ( => [email protected] [password] => secret [firstName] => Tom [lastNamet] => Tees [btnSubmit] => Register ) Now let's say that I want to put these key/value pairs into variables in my "action" page, for example... $email = $password = $firstName = $lastNamet = What is the easiest way to do that? Manually assigning the values? $email = $_POST something? $password = $_POST something? $firstName = $_POST something? $lastNamet = $_POST something? Or maybe a loop? TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115492 Share on other sites More sharing options...
Miss_Rebelx Posted September 25, 2010 Share Posted September 25, 2010 Not sure I'm following. "put these key/value pairs into variables in my "action" page, for example..." Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115493 Share on other sites More sharing options...
Pikachu2000 Posted September 25, 2010 Share Posted September 25, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115494 Share on other sites More sharing options...
kenrbnsn Posted September 25, 2010 Share Posted September 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115498 Share on other sites More sharing options...
TomTees Posted September 25, 2010 Author Share Posted September 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115500 Share on other sites More sharing options...
Pikachu2000 Posted September 25, 2010 Share Posted September 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115502 Share on other sites More sharing options...
TomTees Posted September 25, 2010 Author Share Posted September 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115509 Share on other sites More sharing options...
TomTees Posted September 25, 2010 Author Share Posted September 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115521 Share on other sites More sharing options...
Pikachu2000 Posted September 25, 2010 Share Posted September 25, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115524 Share on other sites More sharing options...
TomTees Posted September 25, 2010 Author Share Posted September 25, 2010 Not until you send me the URL of that page, you shouldn't. Ha ha! TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115525 Share on other sites More sharing options...
PFMaBiSmAd Posted September 25, 2010 Share Posted September 25, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115526 Share on other sites More sharing options...
TomTees Posted September 25, 2010 Author Share Posted September 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115528 Share on other sites More sharing options...
TomTees Posted September 25, 2010 Author Share Posted September 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/214356-how-to-see-what-is-in-_post-array/#findComment-1115532 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.