Sumit
Sumit

Reputation: 277

Passing variable with URL but not able to make decision with Switch statement

On page1.php, i use

<a href="page2.php?choice=<?php echo $value?> & item1=<?php echo $abs1?> & item2=<?php echo $abs2?>" title="Link to next page">Download</a>

On page2.php, i receive these variables correctly as iam able to echo those. But Switch statement doesn't work.

echo $_REQUEST["choice"];echo "<br>"; //printing variable to debug
echo $_REQUEST['item1'];echo "<br>";  //printing variable to debug
echo $_REQUEST['item2'];echo "<br>";  //printing variable to debug

switch((string)$_REQUEST["choice"]) {
    case "Value1":
            echo "Value 1 selected";
            break;
    case "Value2":
            echo "Value 2 selected";
            break;
    case "Value3":
            echo "Value 3 selected";
            break;
        default:
                                 echo "No Value selected";
    }

It always give: No Value selected . Please help, Thanks in advance.

Upvotes: 0

Views: 161

Answers (3)

opatut
opatut

Reputation: 6864

You have an URL like the following:

page2.php?choice=VALUE & item1=VALUE & item2=VALUE

I noticed some spaces in there (before and after the &), so your choice value will have a trailing space. Either trim the choice string, or remove the whitespaces from the URL.

Upvotes: 2

Pheonix
Pheonix

Reputation: 6052

Use a var_dump($_REQUEST) to see the data you are receiving and also trim() value you are receiving.

switch(trim($_REQUEST["choice"])) {
    case "Value1":
            echo "Value 1 selected";
            break;
    case "Value2":
            echo "Value 2 selected";
            break;
    case "Value3":
            echo "Value 3 selected";
            break;
        default:
                                 echo "No Value selected";
    }

Upvotes: 1

ajreal
ajreal

Reputation: 47321

<?php echo $value?> & item1=<?php echo $abs1?>
                   ^ whitespace

better readability (tons of alternatives)

echo "<a href=\"page2.php?choice={$value}&item1={$abs1}&item2={$abs2}\" title=\"Link to next page\">Download</a>";

Upvotes: 3

Related Questions