Ericles Posted April 21, 2011 Share Posted April 21, 2011 A while back my webhost upgraded to PHP5 and broke a few of my simple scripts - primarily forms using a text box for entry. I renamed and relinked the forms to .php4 and it "solved" the problem. I've decided to fix them correctly and use proper PHP 5 code. What's the easiest way to actually retrieve the value of a text input in PHP5? My old reference tricks return null. When I started reading up I saw only PHP4 examples and then in over my head with global variables, httaccess and php.ini. I don't want to mess about with the default settings. I'm sure I zoomed right past a simple answer on my way to that mess. A simple example script: <FORM NAME="form1" ID="form1" METHOD="POST" ACTION="simple.php"> <INPUT Type='Text' Name="i1Text" ID="i1Text" Value="Default"> <INPUT Type="Submit" Name="Submit1" Value="Read the Text"> </FORM> <?PHP if (isset($_POST['Submit1'])) { $RefText=$i1Text; print "Text: $RefText"; <!-- Returns blanks unless .php4 --> } ?> So, how do I actually get to that text value? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/234379-migrating-forms-php4-to-5/ Share on other sites More sharing options...
mens Posted April 21, 2011 Share Posted April 21, 2011 Whoah, PHP4 hey? Anyways, the manual has a lot of valueble information on this, see the use of POST and GET global variables here. Although, it is still possible to use a PHP4 method, using HTTP_POST_VARS. The basic usage would be: if (!empty($_POST['name'])) { $clientName = $_POST['name']; } { ... HTML ... } <form method="post"> <input type="text" name="name"> <input type="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/234379-migrating-forms-php4-to-5/#findComment-1204601 Share on other sites More sharing options...
KevinM1 Posted April 21, 2011 Share Posted April 21, 2011 The reason why you're not getting any values is because the register_globals ini directive is turned off by default in PHP 5. This is a good thing, as it forces you to retrieve values passed into your scripts from the appropriate superglobal arrays: $_GET and $_POST. Register_globals was a security flaw, as it blindly transformed anything coming into a script into a variable. So, someone could simply write: http://yoursite.com?admin=1 And now there's a variable named $admin in your script containing the value of '1'. Very dangerous if you don't validate your variables. PHP 5, like I said, removes that flaw. It forces you to explicitly grab the values you want. In your case, you'd need to use: if (!empty($_POST['ilText'])) { $Reftext = $_POST['ilText']; } else { // error } Quote Link to comment https://forums.phpfreaks.com/topic/234379-migrating-forms-php4-to-5/#findComment-1204608 Share on other sites More sharing options...
Ericles Posted April 21, 2011 Author Share Posted April 21, 2011 Anyways, the manual has a lot of valueble information on this, see the use of POST and GET global variables here. Good stuff! I'll use some of these samples when I get this one little issue working. The reason why you're not getting any values is because the register_globals ini directive is turned off by default in PHP 5. This is a good thing, as it forces you to retrieve values passed into your scripts from the appropriate superglobal arrays: $_GET and $_POST. Roger that. This is why I'm recoding rather than just going with the .PHP4 extension. I'm still doing something wrong however as the code below hits the ERROR line. $_POST is definitely not empty as the script spits out "not empty Error". Any suggestions? if (!empty($_POST)) { print "not empty"; } if (!empty($_POST['ilText'])) { $Reftext = $_POST['ilText']; } else { print "Error"; } Quote Link to comment https://forums.phpfreaks.com/topic/234379-migrating-forms-php4-to-5/#findComment-1204646 Share on other sites More sharing options...
KevinM1 Posted April 21, 2011 Share Posted April 21, 2011 Can you show your entire script? Quote Link to comment https://forums.phpfreaks.com/topic/234379-migrating-forms-php4-to-5/#findComment-1204656 Share on other sites More sharing options...
Ericles Posted April 22, 2011 Author Share Posted April 22, 2011 I played with it a bit more but no luck. This is the entire thing - one script to perform all duties. <html><head> <title>Simple Text Box Test</title> </head> <body> <FORM NAME="form1" ID="form1" METHOD="POST" ACTION="simple.php"> <INPUT Type='Text' Name="i1Text" ID="i1Text" Value="Default"> <INPUT Type="Submit" Name="Submit1" Value="Read the Text"> </FORM> <?PHP if (!empty($_POST)) { print "not empty<br>"; } if (isset($_POST['Submit1'])) { $PostText=$_POST['ilText']; print "PostText: $PostText"; $RefText=$i1Text; print "RefText: $RefText"; if (!empty($_POST['ilText'])) { $PostText = $_POST['ilText']; print "New Post: $PostText"; } else { print "<br>Error"; } } ?> </body></html> I've stared at this thing long enough that anything obvious is now invisible. I expected to be having fun reading SQL and building arrays and I'm stuck on reading input from the screen. Ah well, maybe muddy waters, let stand, will become clear in the morning. Quote Link to comment https://forums.phpfreaks.com/topic/234379-migrating-forms-php4-to-5/#findComment-1204738 Share on other sites More sharing options...
kenrbnsn Posted April 22, 2011 Share Posted April 22, 2011 You're problem stem from the fact that in some fonts a "1" looks like a "l" (small el). In your form you have the name "i1Text", but you test form $_POST['ilText'] Either change the 1 to a "l" or change the "l" to a 1 Ken Quote Link to comment https://forums.phpfreaks.com/topic/234379-migrating-forms-php4-to-5/#findComment-1204749 Share on other sites More sharing options...
Ericles Posted April 22, 2011 Author Share Posted April 22, 2011 Dios mio! A typo! That was introduced when I copied/pasted from here to the script. Oh well. Ok, well then the $_POST solution does is the answer I've been looking for then and at least I know I'm not losing my mind. Thanks for the help everyone. I'll probably be back with more when I can't figure out the arrays. Quote Link to comment https://forums.phpfreaks.com/topic/234379-migrating-forms-php4-to-5/#findComment-1204830 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.