BorysSokolov Posted December 1, 2012 Share Posted December 1, 2012 Hello. I've recently started learning PHP, and this has been irking me since: <?php $myTextbox = $_POST['myTextbox']; if(isset($myTextbox)&&!empty($myTextbox)){ echo 'You Typed: '.$myTextbox; } ?> <html> <head> <title>My 39 Site!</title> </head> <body> <form name = 'myForm' action = 'my39file.php' method = 'post'> <input type = 'text' name = 'myTextbox' value = '<?php echo $myTextbox; ?>'/> <input type = 'submit' name = 'submit'/></br></br> </form> </body> </html> It's a simple code I wrote. It has no errors, and it works just fine. The problem is that I don't understand WHY it works. I've been told numerously that the compiler reads code from top the bottom, but I'm failing to see how the rule applies in this case. For instance, at the beginning of the code, I declared the variable $myTextbox and set it equal to the value of the textbox I created in HTML below. This boggles me. The textbox is created AFTER I declare the variable, so how does the computer know what form element I'm talking about? I'm probably just omitting something. Could anybody enlighten me? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/271432-simple-php-beginner-question/ Share on other sites More sharing options...
Pikachu2000 Posted December 1, 2012 Share Posted December 1, 2012 That code should be giving you an 'undefined index' notice when you first call it. If it isn't, you don't have error reporting set up at a high enough level for developing. Quote Link to comment https://forums.phpfreaks.com/topic/271432-simple-php-beginner-question/#findComment-1396602 Share on other sites More sharing options...
Solution Christian F. Posted December 1, 2012 Solution Share Posted December 1, 2012 (edited) As Pika mentioned you should be getting a notice there, because you're assigning the variable before checking if the source exists. Also, after setting a variable it will always be set, so checking it with isset () is redundant. What you should have done, is test whether or not $_POST['myTextbox'] was set (and not empty), as that's the variable (or rather, array index) which may or may not be set. Now, for the reason it works: As noted above the first time you view the page you are indeed getting the result you expect, you just don't see it and it's not an error. While the $myTextbox variable is set, it's still empty because the $_POST index wasn't set. That's why the PHP parser skips the output inside the IF-block. Once it's done with the test, you've closed the PHP parsing. Meaning that the server will send the rest as pure text straight to the browser. After which the script will die, as there is nothing more to parse. However, when you submit the form you're sending a request for the same page to be viewed again. This time with some extra data POSTed back, from the form. So this time around the $_POST index is set, contains a value, and thus can pass this on to the $myTextbox variable. Which then isn't empty any more, so that the code inside the IF-block is read and executed. After which the script continues on, just like in the above scenario. With closing the PHP parsing and sending the text (in this case formatted as HTML) to the browser. What is important here is to be able to distinguish between what happens on the server, the actual PHP code, and what gets sent to the client (read: browser) for it to parse. You are dealing with two different languages, which are parsed by two different applications on two different computers at two different times. They only thing they have in common, is the coincidence that the first language (PHP) is told to generate output in the form of the second (HTML). PHP knows nothing of what happens on the client side, unless the client explicitly sends it said data. As in the case with the form in your code. Edited December 1, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/271432-simple-php-beginner-question/#findComment-1396610 Share on other sites More sharing options...
floridaflatlander Posted December 1, 2012 Share Posted December 1, 2012 (edited) I declared the variable $myTextbox and set it equal to the value of the textbox I created in HTML below You don't define it until you click submit then the value is POSTED to $myTextbox = $_POST['myTextbox']; Also put your php code in if (isset($_POST['submit'])) { // this name must match your form input name at <input type = 'submit' name = 'submit'/> $myTextbox = $_POST['myTextbox']; if(isset($myTextbox)&&!empty($myTextbox)){ echo 'You Typed: '.$myTextbox; } } as you play more put the footer under the echo and under the footer place exit() to stop the script. Edited December 1, 2012 by floridaflatlander Quote Link to comment https://forums.phpfreaks.com/topic/271432-simple-php-beginner-question/#findComment-1396633 Share on other sites More sharing options...
MDCode Posted December 1, 2012 Share Posted December 1, 2012 if (isset($_POST['submit'])) { // this name must match your form input name at <input type = 'submit' name = 'submit'/> if($_SERVER['REQUEST_METHOD'] === "POST") { This would work better since IE has problems with submitting. Quote Link to comment https://forums.phpfreaks.com/topic/271432-simple-php-beginner-question/#findComment-1396701 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.