I Should Work But it Doesnt Help! Switch Statements,,

Its obvious what this should do, if i press button A it should display the message in A and so on but it doesnt work, This is more or less the same code from a textbook whats wrong with the code

//page.inc

"> <?php print "$mess" ?> <?php print $_POST['Button'] ?>

//Code.php

<?php session_start(); switch(@$_POST['Button']) { ####################################### case "A": $mess = "you have pressed A"; include ("Page.inc"); break; ###################################### case "B": $mess = "you have pressed B"; include ("Page.inc"); break; ######################################## default: $mess = "you have pressed C"; include ("Page.inc"); } ?>

First, you do not need the session_start(); line as you do not have any session variables…

Next remove the “@” from in front of your $_POST line.

Lastly, most people do not use two buttons with the same name. It is quicker and easier to use different names. So, name=“button1” and name=“button2”. This makes your PHP code easier too.
You would have a separate routine to handle each button pressed…
if(isset($_POST[‘button1’]){
//handle all routines for button #1
}
same for each button… Button#1’s code above will not be executed unless it is pressed, so pressing button2 will skip over the above code.

Hope all this helps… Good luck…

Also, aside from what ernie stated, it looks like its on 2 different pages, but its not stated in the action. So either change the action to reflect the second page or put the php above the html.

[php]

<?php switch($_POST['Button']) { case "A": $mess = "you have pressed A"; break; case "B": $mess = "you have pressed B"; include ("Page.inc"); break; default: $mess = "you have pressed C"; } ?> <?=$mess?> [/php]

One other thing, that last print won’t work. All you’ll get is Array. If want to see what’s contained in the POST array, you’ll need to either run it through a loop, use print_r() or var_dump(). Including the page doesn’t really do any good since you’re starting on that page.

Sponsor our Newsletter | Privacy Policy | Terms of Service