PDA

View Full Version : Self PHP


boondocksaint
05-04-2005, 06:37 PM
Hi, I'm starting to learn PHP.

Usually; when working with forms the action is another page; but here I'm trying to use the same page for action.
The problem is that since PHP code which looks at the variable that is posted from the form is on the same page. When first opening the page, I get Undefined Variable messages.
<?
if ($themes == 'Neutral'){ // Theme: Neutral
echo '<body bgcolor="gray">';
} else if ($themes == 'Dark'){ // Theme: Dark
echo '<body bgcolor="black">';
} else if ($themes == 'Light'){ // Theme: Light
echo '<body bgcolor="white">';
} else {
// Do nothing!
}
?>
<table align="center"><tr><td>
<form action="<? $PHP_SELF ?>" method="post">
<select name="themes">
<option value="Neutral" selected="selected">Neutral</option>
<option value="Dark">Dark</option>
<option value="Light">Light</option>
</select>
<p align="center"><input name="submit" type="submit" value="OK"></p>
</form>
</td></tr></table>
<?
echo '</body>';
?>

Unfortunately defining the variable in the script would overwrite the value recieved from the form. How would I go about getting rid of the problem and still use the same file (as in: no "handle_themes.php", etc...)?

Thank you.

Vexed
05-04-2005, 07:21 PM
I think you forgot to get the posted variable? something like this should work:

<?

$themes=$_POST['themes'];

if($themes!="")
{
if ($themes == 'Neutral'){ // Theme: Neutral
echo '<body bgcolor="gray">';
} else if ($themes == 'Dark'){ // Theme: Dark
echo '<body bgcolor="black">';
} else if ($themes == 'Light'){ // Theme: Light
echo '<body bgcolor="white">';
} else {
// Do nothing!
}
}
?>
<table align="center"><tr><td>
<form action="<? $PHP_SELF ?>" method="post">
<select name="themes">
<option value="Neutral" selected="selected">Neutral</option>
<option value="Dark">Dark</option>
<option value="Light">Light</option>
</select>
<p align="center"><input name="submit" type="submit" value="OK"></p>
</form>
</td></tr></table>
<?
echo '</body>';
?>

Phait
05-04-2005, 07:35 PM
I'd dissect this but I can't get back into my coding groove since quite awhile, but check this out:

http://www.phait-accompli.com/crap/php/skin/

Source is in there (.phps)

ShepMode
05-06-2005, 10:07 AM
Ok there are two things you should be aware of here:

1) $themes does not exist on the first visit to the page. You should check if it exists before doing your echoing. Eg


if (isset($themes)) {
// Do the stuff you want to do here.
}


2) You should be accessing POST variables with $_POST['variable_name'] instead of $variable_name. If you can access a POST variable with $variable_name then it means register_globals is on in php.ini, which it should not be for security reasons. Even if you're only at a learning stage, you should turn register_globals off so you're more refined with your coding.

boondocksaint
05-07-2005, 08:28 PM
Point taken. I've turned it off.

Okay, when working with arrays and forms...
I want to have a pull-down menu with a list of comics and when the submit button is clicked, then the user is sent to
comics.php?id=$key
with $key being one of keys from the comic array (below)...

I have so far:
<?
// Make $comic Array
$comic = array (
'test1' => 'test1',
'test2' => 'test2',
'test3' => 'test3'
);

// Make Archive
echo "<form action=\"comics.php/?id=$key\" method=\"post\">";
echo '<select name="id">';
foreach($comic as $key => $value){
echo "<option value\"$key\">$value</option>";
}
echo '</select>';
echo '<input type="submit" value="OK">';
echo "</form>";
?>

So after choosing test1 and clicking go, the user should be sent to comics.php?id=test1
Right? Right.
The problem is of course my least favorite message. Undefined variable. And I understand why.
How would I go about telling it to create $key out of each, well, key from the array?
Know what I'm talking about?

ShepMode
05-08-2005, 05:11 AM
The form method is set as post. If you set it to GET, the variable will be sent as GET instead of POST. http://forums.3drealms.com/ubbthreads/images/graemlins/smile.gif