Page 1 of 1

Can't Pass Variables thru switch : Version 4.2

Posted: Tue May 14, 2002 2:31 am
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;
?>

Posted: Tue May 14, 2002 8:33 am
by denisb
Try set in php.ini register_globals = on
:)

Posted: Tue May 14, 2002 8:53 am
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

Posted: Wed May 15, 2002 2:28 am
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

Posted: Wed May 15, 2002 6:39 am
by jason
The simple solution:

change:

Code: Select all

switch($action)&#123;
to

Code: Select all

switch($_POST&#1111;'action']){
Then ask me why.

Posted: Wed May 15, 2002 7:34 pm
by cathari
Hey, thanks it worked...

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