-5

I'm trying to use the switch statements but it's not working. My problem is for example i input LazyBoy in the textbox, that should echo LazyBoy else echo another string.

<?php

  $classmap = $_POST['classmap'];

  switch ($classmap) {
    case "LazyBoy":
        echo "You're Lazy!";
        break;
    case "GrayHounds":
        echo "You're Gray!";
        break;
  }

?>

Here is the form -

<form action="checkout.php" method="post" >
<input type="hidden" name ="classmap" value="<?php include('db.php');
    $origin = $_POST['origin'];
    $class = $_POST['class'];
    $daten = $_POST['daten'];
    $result = mysql_query("SELECT * FROM route WHERE route LIKE '%$origin%' AND type LIKE '%$class%' AND date LIKE '%$daten%' ");
    while($row = mysql_fetch_array($result))
    {
        echo $row['type'];
    }   ?>">

  </form>
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • 1
    Is your textbox definitely named 'classmap'? – Boann Sep 29 '15 at 17:05
  • 6
    please post `var_dump($_POST)` output – Timofey Sep 29 '15 at 17:06
  • Have you tried any debugging here? What are you POSTing to this page? What does `var_dump($_POST)` show? – gen_Eric Sep 29 '15 at 17:07
  • Yes Boann, yes i will have 3 conditions thats why i will use switch statement. – Kim Ocampo Octaviano Sep 29 '15 at 17:07
  • I have a textbox name "classmap" then i will get its value and have this conditions. if classmap = "1" echo 1 else echo 2. – Kim Ocampo Octaviano Sep 29 '15 at 17:09
  • You can try to echo your variable $classmap directly, so you will see what's in it, and continue searching from there. Also, beware of case sensitive text. – Philippe Sep 29 '15 at 17:10
  • The problem is your form's value. You cannot put all of that PHP code in there. By posting an incomplete question you have caused a lot of people to guess what your problem is. Make sure to *always* post all of the relevant information for any question you have. – Jay Blanchard Sep 29 '15 at 17:34
  • 2
    **What**! What is in your database table column _routers.type_ Does it all belong in that single hidden field?? – RiggsFolly Sep 29 '15 at 17:37
  • It is unclear what you're trying to achieve here. Talk us through the logic by editing your post and explaining what you expect the input and output to be. – Jay Blanchard Sep 29 '15 at 17:37
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Sep 29 '15 at 17:37
  • 1
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Sep 29 '15 at 17:37
  • Im trying to echo the values from the textbox, and it's right. when i try to compare if this textbox is equals to ="LazyBoy" or "GrayHounds" its not working. – Kim Ocampo Octaviano Sep 29 '15 at 17:40
  • again, post `var_dump($_POST)` output in `checkout.php` – Timofey Sep 29 '15 at 17:45
  • array(4) { ["route"]=> string(2) "23" ["qty"]=> string(160) " " ["date"]=> string(12) " 0000-00-00" ["classmap"]=> string(16) " GrayHounds" } – Kim Ocampo Octaviano Sep 29 '15 at 17:48
  • what are you passing in value of class map. what is that db.php ? – Nana Partykar Sep 29 '15 at 17:58
  • 1
    You need to get in the habit of up-voting and [accepting answers](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) that help you to solve your issues. – Jay Blanchard Sep 29 '15 at 19:37
  • "GrayHounds" should be 10 characters long but your string is 16 characters long. It's obviously full of space. – Boann Sep 30 '15 at 02:13

4 Answers4

0

First you have to post values to define the classmap index.

<form method="post">
    <input type="text" name="classmap" value="LazyBoy"/>
    <input type="submit" value="submit"/>
</form>
<?php


  $classmap = (isset($_POST['classmap']) ? $_POST['classmap'] : null);


  switch ($classmap) {
    case "LazyBoy":
        echo "You're Lazy!";
        break;
    case "GrayHounds":
        echo "You're Gray!";
        break;
  }

?>
Malith
  • 970
  • 7
  • 17
  • 2
    Why should the OP "try this"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO. – Jay Blanchard Sep 29 '15 at 17:22
  • @Jay Blanchart - you are right. thanks. i just said to try this because I tested this code. – Malith Sep 29 '15 at 19:27
0

You just need to check if the post variable is set and/or add a default case :

if(isset($_POST['classmap'])) {
     $classmap = $_POST['classmap'];

     switch ($classmap) {
        case "LazyBoy":
            echo "Your Lazy!";
            break;
        case "GrayHounds":
            echo "Your Gray!";
            break;
        default:
            echo "Something";
            break;
     }
}
else {
     echo "Something";
}
Halvra
  • 285
  • 1
  • 9
0

Make sure you post to the right script. Try to put default statement to debug.

$classmap = $_POST['classmap'];

switch ($classmap) {
   case "LazyBoy":
       echo "You're Lazy!";
       break;
   case "GrayHounds":
       echo "You're Gray!";
       break;
   default:
       echo "does not match with previous cases";
}
Halvra
  • 285
  • 1
  • 9
qtran
  • 1
  • 2
-1

Your Code is correct. I guess there is a Problem with the $_POST.

Try to verify it.

$classmap = "LazyBoy";
Minority
  • 1
  • 1