Page 1 of 1

Simple PHP

Posted: Fri Mar 10, 2006 7:25 pm
by TRPlace
I am semi new to php and I havent programmed too much except for actionscript.
I have some code that I am having trouble getting to work, and it might just be because I am not familiar with the scope of some things.

Code: Select all

<body>
<?PHP 
	function WriteToFile(){
		$filename="/home/content/w/h/y/whyworkforever/html/test/test.txt";
		$file=fopen($filename,"w");
		fwrite($file,"HERE".$yesPercentage);	
		fclose($file);
	}
	function Read(){
		$filename="/home/content/w/h/y/whyworkforever/html/test/test.txt";
		$file=fopen($filename,"r");
		echo($file);
		fclose($file);
	}
        echo($yesPercentage);
?>
<form name="pollForm" method="post" action="<?PHP WriteToFile(); ?>">
  <p>
    <input name="yesPercentage" type="text"> 
    Yes Number
</p>
  <p>
    <input name="noPercentage" type="text"> 
    No Number
</p>
  <p>
    <input name="maybePercentage" type="text">
 Maybe Number</p>
  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>
<a href=http://www.whyworkforever.org/test/test.php?Read>HERE</a>
</body>
> My first question is why can't I echo inside of a function
> Second, why can't I get the variables to equal anything in the function, but if I echo outside of the function, the value of whichever variable I am trying to see, shows once I have submitted them form and it refreshes.
> Third, How would I call a function from a link? I am testing around trying to call the Read function from the link at the bottom of the script

Thank you for your help. I am trying to get the php I write to interact with flash, if that has any difference, which I don't think it should too much.

Thanks again!

-TJ

Posted: Fri Mar 10, 2006 7:37 pm
by Smackie
first of do you get any errors if so post them please

Re: Simple PHP

Posted: Fri Mar 10, 2006 8:58 pm
by RobertGonzalez
TRPlace wrote:I am semi new to php and I havent programmed too much except for actionscript.
I have some code that I am having trouble getting to work, and it might just be because I am not familiar with the scope of some things.

Code: Select all

<body>
<?PHP 
	function WriteToFile(){
		$filename="/home/content/w/h/y/whyworkforever/html/test/test.txt";
		$file=fopen($filename,"w");
		fwrite($file,"HERE".$yesPercentage); // $yesPercentage needs to be globalized
		fclose($file);
	}
	function Read(){
		$filename="/home/content/w/h/y/whyworkforever/html/test/test.txt";
		$file=fopen($filename,"r");
		echo($file);
		fclose($file);
	}
        echo($yesPercentage); //$yesPercentage is stuck in the function scope, not global
?>
<form name="pollForm" method="post" action="<?PHP WriteToFile(); ?>">
  <p>
    <input name="yesPercentage" type="text"> 
    Yes Number
</p>
  <p>
    <input name="noPercentage" type="text"> 
    No Number
</p>
  <p>
    <input name="maybePercentage" type="text">
 Maybe Number</p>
  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>
<a href=http://www.whyworkforever.org/test/test.php?Read>HERE</a>
</body>
> My first question is why can't I echo inside of a function
> Second, why can't I get the variables to equal anything in the function, but if I echo outside of the function, the value of whichever variable I am trying to see, shows once I have submitted them form and it refreshes.
> Third, How would I call a function from a link? I am testing around trying to call the Read function from the link at the bottom of the script

Thank you for your help. I am trying to get the php I write to interact with flash, if that has any difference, which I don't think it should too much.

Thanks again!

-TJ
I made a few comments in your code above. You need to make your variable global in the function AND return it from the function to use it in your code. Also, try to stay away from REGISTER_GLOBALS.

Code: Select all

<?PHP 
    function WriteToFile(){
        global $yesPercentage;
        // or you can use the superglobal $_POST var
        $filename="/home/content/w/h/y/whyworkforever/html/test/test.txt";
        $file=fopen($filename,"w");
        fwrite($file,"HERE".$yesPercentage); // $yesPercentage needs to be globalized
        fclose($file);
    }
    function Read(){
        $filename="/home/content/w/h/y/whyworkforever/html/test/test.txt";
        $file=fopen($filename,"r");
        echo($file);
        fclose($file);
    }

    $yesPercentage = $_POST['yesPercentage']; // set the value of $yesPercentage
    echo($yesPercentage); 
?>

Posted: Sat Mar 11, 2006 10:44 pm
by TRPlace
Thanks for the help. I am still having a little bit of trouble.
> I don't understand what the super global is, but right now I don't think it's too important
> I revised the code a little to show a couple examples of problems I am having

Code: Select all

<?PHP 
    function WriteToFile(){
        global $yesPercentage;
		//echo($yesPercentage."HERE");
		echo("HERE".$yesPercentage);
        // or you can use the superglobal $_POST var
        $filename="/home/content/w/h/y/whyworkforever/html/test/test.txt";
        $file=fopen($filename,"w");
        fwrite($file,$yesPercentage); // $yesPercentage needs to be globalized
        fclose($file);
    }
    function Read(){
        $filename="/home/content/w/h/y/whyworkforever/html/test/test.txt";
        $file=fopen($filename,"r");
        echo($file);
        fclose($file);
    }

    //$yesPercentage = $_POST['yesPercentage']; // set the value of $yesPercentage
    echo("yesPercentage=".$yesPercentage); 
?>
In the line here....[exerpt]

Code: Select all

global $yesPercentage;
//echo($yesPercentage."HERE");
echo("HERE".$yesPercentage);
Either way I echo it, it tries to go to a website....in my case it goes to http://www.whyworkforever.org/test/HERE, and the starting URL is http://www.whyworkforever.org/test/test.php

The globalization did help, and I do remember that from my VB classes, but it has been a while so I needed a refresher, thanks :D

Also I have a link in the HTML...

Code: Select all

<a href=http://www.whyworkforever.org/test/test.php?Read>HERE</a>
I want it to process that function, but it just refreshes the page, but I think that could be part of that echo problem I am having.

I was going to try to put in a "return $yesPercentage" line in the WriteToFile function, but I thought I would get one thing done at a time.

I hope this was well explained so some kind person can help. Thanks for your fast response on the previous message.

It's weird how some of the easiest things get forgotten, but once they get refreshed in your memory, other things start to get unlocked and remembered.