Can't Pass Variables thru switch : Version 4.2

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cathari
Forum Newbie
Posts: 11
Joined: Mon May 13, 2002 9:51 pm

Can't Pass Variables thru switch : Version 4.2

Post by cathari »

Hi,

The switch statement that I used before on Version 4.0.6 is not working on the new version 4.2. What alternatives can I use that will produce the same result.

Here's the code:

I'm getting this message : Undefined variable: action

Thanks.

Code: Select all

<?php

$PHP_SELF = $_SERVER&#1111;'PHP_SELF'];

function index()&#123;
$PHP_SELF = $_SERVER&#1111;'PHP_SELF'];
?>
<form action="<?php echo $PHP_SELF ?>" method="POST">
<input type="hidden" name="action" value="submit"
    <p>Input a word <input type="text" size="20" name="you_wrote">
    <input type="submit" name="submit" value="submit"></p>
</form>
<?php
&#125;

function verify()&#123;
 echo("You wrote ".$_REQUEST&#1111;'you_wrote']);
&#125;

switch($action)&#123;
	case "submit":
		verify();
	break;
		
	default:
		index();
	break;

&#125;
?>
denisb
Forum Newbie
Posts: 2
Joined: Tue May 14, 2002 8:33 am

Post by denisb »

Try set in php.ini register_globals = on
:)
User avatar
Phirus
Forum Commoner
Posts: 37
Joined: Thu Apr 18, 2002 4:10 pm
Contact:

Post by Phirus »

I dont exactly understand what you are trying to do..your code is a bit messed up..

Heres what I think you are trying to achieve:

Code: Select all

<?
	if(isset($_POST&#1111;'submit']))&#123;
	
		function verify()&#123; 
			echo "You wrote &#123;$_POST&#1111;'you_wrote']&#125;"; 
		&#125; 
	
	verify();

&#125;else&#123;

?>

<form action="<?=$_SERVER&#1111;'PHP_SELF'];?>" method="POST"> 
<input type="hidden" name="action" value="submit"> 
    <p>Input a word <input type="text" size="20" name="you_wrote"> 
    <input type="submit" name="submit" value="submit"></p> 
</form>
	
<? &#125; ?>
Hope that helps,

PhIrUs
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

denisb wrote:Try set in php.ini register_globals = on
You should always try and write code that doesn't require register_globals to be on. It makes your code more secure if it's off.

In order to access data sent from a form you need to use the $_POST or $_GET arrays (depending on which method you used).
http://www.php.net/manual/en/reserved.v ... ables.post

So instead of $action use $_POST['action'] to refer to it.

Also use $_POST['you_wrote'] instead of $_REQUEST['you_wrote'] as $_REQUEST is a much bigger array consisting of the contents of $_GET, $_POST, $_COOKIE, and $_FILES.

That said, I'd go with Phirus' code to do what it looks like you're trying to achieve. Just thought I'd give a bit of an explanation :) .

Mac
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

The simple solution:

change:

Code: Select all

switch($action)&#123;
to

Code: Select all

switch($_POST&#1111;'action']){
Then ask me why.
cathari
Forum Newbie
Posts: 11
Joined: Mon May 13, 2002 9:51 pm

Post by cathari »

Hey, thanks it worked...

But i'm I got a notice: Undefined Index : action...
Post Reply