Jump to content

Migrating forms : PHP4 to 5


Ericles

Recommended Posts

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. 8) 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!

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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"; 
}

Link to comment
Share on other sites

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.  :shrug:

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.  :D

Thanks for the help everyone. I'll probably be back with more when I can't figure out the arrays.  ;D

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.