You are here: Stupid Newbie Question « PHP Language « IT news, forums, messages
Stupid Newbie Question

Posted by Carla Fong on 12/21/07 23:49

Hello - I'm trying to learn HTML and PHP and have hit a wall with some
odd behavior I can't fathom. I'm pretty good with C and this just seems
strange.

Host is running Apache with PHP version 5.2.5 according to the phpinfo()
function.

In the code skeleton below I want to create a text string to use for
initializing the value of several form input elements. I think I am
creating a global variable $_Line1 and assigning it an initial value of
"Virgin Text". (Yes, I know globals are the spawn of Satan, but please
bear with me.)

For the 'action' of the form I call a function that twiddles with the
$_Line1 variable.

My expectation was that the first time the page loads the value "Virgin
Text" would be in the form input element since the form had not been
submitted yet. After clicking the "click to do TestPhp function" button
I expected the form value to change to the 'After Function' text.

It doesn't.

When the page first loads, the form input variable displays 'After
Function' - but I have never yet called the function to change the
variable. It's as if the function has been called in the process of
loading the page.

Can someone explain what I'm doing wrong or why this behaves this way?
Shouldn't the value of $Line1 be 'Virgin Text' until the function is
called once?

Thanks in advance - Carla

Email reply address is spam-trapped. remove an '.xspamx' for direct contact.


<HTML>

<?php
// create global variable and preset the value to something
$_Line1 = "Virgin Text";
?>

<?php
function TestPhp()
{
global $_Line1; // allow access to global variable
$_Line1 = "After Function"; // change value of global
}
?>
<HEAD>
<TITLE>PHP Puzzle</TITLE>
</HEAD>

<?php
// a debug statement that shows $_POST is empty prior to form action
if (empty ($_POST)) echo "POST data is empty";
else echo "POST data is not empty";
?>

<form method=POST action="<?php TestPhp();?>">
<tr>
<td> Input Prompt:</td> <td> <input type=text name="Line_1"
value = "<?php echo $_Line1; ?>" maxlength=24
</td></tr><BR>

<tr> <td><input type="reset"></td> <td><input type="Submit"
value="click to do TestPhp function" align ="center"</TD></tr>
</form>

$_POST contains:
<?php
// this dumps the $_POST array data pairs for debug output
print_r ($_POST);
?>
</BODY>

</HTML>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация