PDA

View Full Version : Global variable problem in PHP?


Justin
04-07-1999, 05:54 PM
Are you wanting the script to remember variables between runs? Like, run the script, script is finished, run again, and it remembers? I'm afraid that wouldn't work (that would be static variables, BTW).

The reason is that the script is in fact dead once it's done it's thing http://www.aota.net/ubb/smile.gif Even an ordinary Windows program - the only way it can remember from a previous session is to save settings in the Registry or an INI file - not in a variable. While there is no registry and writing to a file would be too much work, hidden form fields are the way to go here http://www.aota.net/ubb/biggrin.gif

Example:

$header = "<html><head>
<title>My Dynamic Page</title>
</head>
<body>";

Later...

print "<form action=script.php3 method=post>
<input type=hidden name=tophtml value=\"$header\">
.....";

However, that is a lot of extra html to be downloaded and re-uploaded - there should be a much easier way to do what you are wanting to do. I don't know what it is you are trying to do, but there will be an easier way than storing html snippets in a form field or variable...

---

Ok, just scrolled down and took a closer look. Here's what you should do:

$fruit = "rutabaga";
...
print "<input type=hidden name=fruit value=$fruit>";

Then assemble it later in the script, instead of doing the work over and over - just hold the info a little at a time until it's all gathered and then put it all together http://www.aota.net/ubb/smile.gif


------------------
Justin Nelson
FutureQuest Support




[This message has been edited by Justin (edited 04-07-99).]

SneakyDave
04-07-1999, 06:31 PM
Hey Justin, I think that'll work. I don't know why I thought the static fields were still "available" after the script was reloaded, all I can blame it on was that it was late.

I was trying to use the hidden form field, but I couldn't get it to work right, I think this way will work better anyway. http://www.aota.net/ubb/biggrin.gif


By the way, in no way was I trying to infer that rutabagas are my favorite fruit, nor that they are the root of my PHP ignorance.


Thanks again.
Sneaky

SneakyDave
04-07-1999, 09:35 PM
Hey Justin, I can't get it to work right. The hidden variable only stores about 6 characters of my text that I want to reference.

Can I email you offline (again!) with what I'm trying to do?

Thanks for your time

Sneaky

SneakyDave
04-07-1999, 09:53 PM
Hold the phone, I got it to work another way.....

For whatever reason, the hidden form variable didn't like more than 8 characters even with a MAXLENGTH and SIZE parameter on it, but I got around it.

Justin, thanks for your input, it inspired me to try something else, and I think its going to work.

I'd like to get the hidden form variable to work as expected though, so if you have the time, send me your phone number, and I'll discuss the problem with you. Its HIGHLY CLASSIFIED information, dealing with bacon and its effect on the John Hancock building in Chicago.

Thanks again..
Sneaky

Justin
04-08-1999, 12:30 AM
If there is whitespace in the hidden field, you have to quote it. However, I NEVER use double quotes in HTML - just single one's (like that appostrophie I just used there hehe). You don't have to escape them for some reason in PHP.

In fact, PHP is far more forgiving that Perl with that stuff - the only thing I have found that needs escaping is " and that's about it http://www.aota.net/ubb/smile.gif

But anyway, you'd need to do this:

print "<input type=hidden name=food value='$food'>";

Then when that form is submitted, it passes the variable $food with the same value it just had http://www.aota.net/ubb/smile.gif

I love php to death, and combine that with MySQL - well, then you have my next site (also classified - deals with other pork products (mainly pork rhinds) and a trailer park near the Gateway Arch).

BTW - I already know about bacon's effects on the Hancock center - the grease has a 'slick' effect on the slanted top causing bird droppings to slide down, land on cars and cause accidents and traffic jams on Lake Shore Drive. I saw the white paper on codename "PidgeonJam" myself, and we are taking it quite seriously (I live near Chicago http://www.aota.net/ubb/smile.gif)

Wow, I'm in a silly mood tonight, huh? http://www.aota.net/ubb/biggrin.gif Feel free to email me about the php if you want. On a side note I absolutely cannot call a perl script from php either - only works locally http://www.aota.net/ubb/redface.gif

SneakyDave
04-08-1999, 01:06 AM
The subject may be incorrect, but this is the problem I'm having....

I have a PHP script that build somes HTML and puts it in a variable, such as this:

$fruit .= "<HTML><BODY>";
$fruit .= "My favorite is the rutabaga";
$fruit .= "</BODY></HTML>";
print $fruit;

And then there is a form where a user may fill out some additional information, and press a submit button that reloads the PHP script with a different parameter.

The problem I have is that after the script is reloaded, the $fruit variable is null, I can't use it after the script reloads, although I don't initialize it anywhere in the code.

Now, I may be being stupid assuming the variable would not be reinitialized, but I thought that was the case with PHP. I even tried assigning the $fruit variable to a hidden variable in a form ($FormFruit), and THEN trying to reference it after the script is reloaded, but that didn't work either.

Do I need to either somehow assign the $fruit variable as a global variable (which probably isn't the correct term), or do I have to make sure that the $fruit variable is passed in a form under an "assumed" name, just so that I can reference it somewhere else in my script?

Any help will be appreciated. Thanks
Sneaky

(I edited this to remove the "code" ubb code because of a bug in which the "/code" command doesn't work).

[This message has been edited by SneakyDave (edited 04-07-99).]

SneakyDave
04-08-1999, 12:46 PM
Hey, I think that '$food' idea might work. It was doing some crazy things the other way with the double quotes, as in "value="$food">";, it assumed the ">"; at the end of that line was part of the VALUE...
Anyway, even with the struggle, I'll have it done faster than doing it in Perl.

And on the other note, I've given up on the Perl script call from PHP. I'm just going to try to do it all in PHP using a flat file. I'm not a big tech support specialist on FQ to afford MySQL, although using it would be a blast I'm sure.

I'm off to do some reconnaisance(sp?) on the effects of pigs feet being thrown on the WalMart MegaCenter down the street. Wish me luck.

Sneaky

SneakyDave
04-09-1999, 11:43 AM
Justin, just a note to say that the ' worked a lot smoother than the ". I wish I would have known that before, it makes the whole script a lot easier to read in a lot of cases.

Justin
04-09-1999, 12:22 PM
Just a quick note: be careful not to include a ' in the variable itself...

$fruit = "Don't Care";

The resulting HTML would be:

<input type=hidden name=fruit value='Don't Care'>

See the problem? Just be careful, or even strip them out of the value before putting them in the form...

------------------
Justin Nelson
FutureQuest Support

kk
04-09-1999, 04:53 PM
Hi,

Try AddSlashes() - it'll quote ', ", \ with
backslash (mean \', \", \\ in order).

and do StripSlashes() for reverse operation.

Somehow, if you don't want to quote or want to quote only some characters. Try str_replace() instead. http://www.aota.net/ubb/wink.gif

PS: urlencode()/ urldecode() can also do similar thing.

Yours,
-kk-
Thai-IMEX.com
(Yes! It's powered by FutureQuest & PHP & MySQL)

SneakyDave
04-10-1999, 01:12 AM
Yes, I was burned on that one earlier. Since it was html code, I replaced it with (ampersand)#039; code.


[This message has been edited by SneakyDave (edited 04-09-99).]