Switch case checkbox

Asked

Viewed 399 times

1

I need you to mark the choices of a checkbox, be informed a different sentence, even if I mark two or more different checkbox. In this case the below, I used switch but I’m having a hard time getting the right answers. How can I do that?

Follows the code:

<!DOCTYPE html>
<html lang="pt-br">

<head>
    <meta charset="UTF-8">
    <title>Avatarizador :)</title>
</head>

<body>

    <form action="" method="post">
        <input type="checkbox" name="erea" value="SSA"> Salvador
        <br/>
        <input type="checkbox" name="erea" value="BRI"> Brisas
        <br/>
        <input type="checkbox" name="erea" value="PTS"> Pontes
        <br/>
        <input type="checkbox" name="erea" value="PTV"> Porto Velho
        <br/>
        <input type="submit" value="Avatarizar">
    </form>

    <?PHP

        /*

        $image_path = "salvador.png";
        $image_path = "brisas.png";
        $image_path = "pontes.png";
        $image_path = "portovelho.png";

        $image_path = "salvador_pontes.png";
        $image_path = "brisas_pontes.png";

        $image_path = "salvador_pontes_portovelho.png";
        $image_path = "brisas_pontes_portovelho.png";

        */


    $e = isset($_POST["erea"])?$_POST["erea"]:"XX";
        switch ($e) {

            /* Pontes + Porto Velho */
            case "PTS":
                $r = "Pontes";
                break;
            case "PTV":
                $r = "Porto Velho";
                break;
            case "PTS":
            case "PTV":
                $r = "Pontes + Porto Velho";
                break;

            /* Salvador */                
            case "SSA":
                $r = "Salvador";
                break;
            case "SSA":
            case "PTS":
                $r = "Salvador + Pontes";
                break;
            case "SSA":
            case "PTV":
                $r = "Salvador + Porto Velho";
                break;
            case "SSA":
            case "PTS":
            case "PTV":
                $r = "Salvador + Pontes + Porto Velho";
                break;

            /* Brisas */       
            case "BRI":
                $r = "Brisas";
                break;
            case "BRI":
            case "PTS":
                $r = "Brisas + Pontes";
                break;
            case "BRI":
            case "PTV":
                $r = "Brisas + Porto Velho";
                break;
            case "BRI":
            case "PTS":
            case "PTV":
                $r = "Brisas + Pontes + Porto Velho";
                break;
            default:
               $r = ""; /* Avatar Puro */
       }
       echo "Voce escolheu por o selo de $r";
    ?>
</body>

</html>

1 answer

1

To generate your phrase based on the selected values, you can do so.

 $('input[type=button]').bind('click',function(){

    var resultado = '';
        $('input[type=checkbox]:checked').each(function() {
        if(resultado!=''){resultado += "+";}
        resultado += $(this).val();
    });    

    alert(resultado);

  });

See if it helps you.

Working example here.

  • But I need him to show the sentences that are inside the case, not what is written in the figures. Then I’ll still have to replace each sentence with a $image_path = "name.png"; in case, where a code will select the correct photos to become watermark. You can see the site I’m modifying here: http://ilhas.16mb.com/

  • If you’re going to replace every phrase pq doesn’t do this with the checkbox values instead of the sentences, or do you have some "Old Port.jpg" with blank space and everything? How you will do with the N variations that you can have in this concatenation of selected values.

  • https://jsfiddle.net/eeuzj8ra/2/ See a new example here, despite the texts you want.

  • I’m trying to recreate this: http://pastebin.com/FJ4s2pC4

  • 1

    Here in the company Pastebin is blocked, you can not play in Fiddler? there I can help you

  • https://jsfiddle.net/nrcdwpy4/2/

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.