Author Topic: I Should Work But it Doesnt Help! Switch Statements,,  (Read 160 times)

Jack

  • New Member
  • *
  • Posts: 8
  • Karma: 0
    • View Profile
I Should Work But it Doesnt Help! Switch Statements,,
« on: March 17, 2012, 01:09:46 PM »
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
<html>
<body>
<form action = "<?php print $_SERVER['PHP_SELF']?>">

<input type ="submit" name  ="Button" value = "A"/>

<input type ="submit" name  ="Button" value = "B"/>


<?php print "$mess" ?>

<?php print $_POST['Button'] ?>
</form>
</body>
</html>


----------------------------------------------------------------------------------------------------------------------------------


//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");
}
?>

ErnieAlex

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 1847
  • Karma: 32
    • View Profile
Re: I Should Work But it Doesnt Help! Switch Statements,,
« Reply #1 on: March 17, 2012, 01:27:38 PM »
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...

richei

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 1129
  • Karma: 22
    • View Profile
Re: I Should Work But it Doesnt Help! Switch Statements,,
« Reply #2 on: March 17, 2012, 06:36:21 PM »
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 Code: [Select]

<?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";
}
?>

<html> 
<body>
<form action="" action="post">
    <input type ="submit" name  ="Button" value = "A"/>
    <input type ="submit" name  ="Button" value = "B"/>
</form>

<?=$mess?> 
</body>
</html>


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.
« Last Edit: March 17, 2012, 06:48:06 PM by richei »