tags:

views:

27

answers:

3

I am trying to simply set a variable and have it passed back to the same PHP script as called, however it doesn't work.

The first time the script is run I see what I expect to see on screen which is

Your store is USA and your language is en

If I then choose UK and press submit I see the following line

Your store is and your language is en

My sample code is

<?php
if(isset($_POST['submit'])){
    $store = $_GET['store'];
    $lang=en;
}
else 
{
$store=143441;
$lang=en;
} 
switch ($store)
 {
case "143441":
$storename="USA";
break;
case "143444":
$storename="UK";
break;
}
?>
<head>
</head>
<body>
<form name="store" method="post" action="test.php">
<select name="Store">
<option value="143441">USA</option>
<option  value="143444">UK</option>
</select>
<INPUT TYPE="submit" name="submit" value="submit">
</form>
<?php  echo "Your store is " . $storename . " and your language is " . $lang; ?>
</body>
</html>
A: 

You should use $_POST['store'] instead of $_GET['store'] since it's a POST request parameter.

<?php
if(isset($_POST['submit'])){
    $store = $_POST['store'];
    $lang='en';
}
else{
    $store=143441;
    $lang='en';
} 
switch ($store){
case "143441":
   $storename="USA";
   break;
case "143444":
   $storename="UK";
   break;
}
?>
Lekensteyn
+1  A: 

Your sample code:

if(isset($_POST['submit'])){
    $store = $_GET['store'];

Your problem is that you're mixing $_POST and $_GET.

Since your form is doing a POST action, you should be using $_POST for both of those lines.

You could also use $_REQUEST if you're not sure whether it'll be a post or a get request, but generally it'd be better to use $_POST in your case, since you know it'll always be a post.

Spudley
+1  A: 

In the first if clause use

$store = $_POST['Store'];  //be aware of the upper case!!!!!

instead of

$store = $_GET['store'];

and everything will be fine.

Thariama
Brilliant, thanks! I had also tried _POST but it was the upper case that caught me out, well spotted :)
kitenski