0

I want to create a php form with checkbox, like this:

< form method="post>
        <input type="checkbox" name="formpractise[]" value="A" /> <br/>
        <br/>
        <input type="checkbox" name="formpractise[]" value="B" /> <br/>
        <br/>
        <input type="checkbox" name="formpractise[]" value="C" />  <br/>
        <br/>
        <input type="checkbox" name="formpractise[]" value="D" /> <br/>
        <br/>
        <input type="checkbox" name="formpractise[]" value="E" /> <br/>
        <br/>
        <input type="checkbox" name="formpractise[]" value="F" /> <br/>
        <br/>
        <input type="submit" name="formsubmit" value="Submit" />
</form>

when the user select checkbox I want to print a message according user choice. Not the choice he made, a message depending his choice.

2
  • I don't know the code exactly, but get the value of the input and use an if statement for each one to print out the desired message.
    – ayyp
    Nov 20, 2011 at 17:48
  • dear @user1056615, you should give some feedback to ppl who dedicated time to your issue. And finally accept an answer won't hurt... :-)
    – maraspin
    Nov 20, 2011 at 19:23

4 Answers 4

0

If you are using the jQuery library, the Javascript would look something like this:

$(function(){
    // The messages
    var messages = {
        A  : "Message for A",
        B  : "Message for B",
        C  : "Message for C",
        D  : "Message for D",
        E  : "Message for E",
        F  : "Message for F"
    };
    $("input").click(function(){
        var key = $(this).val();
        // Only if the checkbox is checked
        if( $(this).is(":checked") )
        {
            // Alert the message
            alert(messages[key]);

            // Fill the HTML of the div with the id messageDIVID
            $("#messageDIVID").html(messages[key]);
        }
    });
});

If you have all your messages inside your PHP code. And you have an array, you can do this

$messages = Array("A" => "Message for A", 
                  "B" => "Message for B", 
                  "C" => "Message for C", 
                  "D" => "Message for D", 
                  "E" => "Message for E", 
                  "F" => "Message for F");

echo "var messages = " . json_encode($messages) . ";";
0

Your checkbox is an array, so you need to take this into account. You can have multiple valid choices. Don't know about your goal, but a simple - and simplistic - solution could be:

$choices = array(A,B,C,D,E,F)
foreach ($choices as $choice) {
  if (in_array($choice, $_POST)) {
     // do something with your message
     // (append, edit, whatsoever)
  }
}
0

Well as you're using checkboxes and multiple selections are possible I would use the binary code to help me select a different message for each and every possible combination. For example if the user selects boxes A, D, E and F you could convert this to binary code. If: F = 1 E = 2 D = 4 C = 8 B = 16 A = 32

The message you would display would be message number 39 (out of a possible 64 different messages) A = 32 + D = 4 + E = 2 + F = 1

32+4+2+1 = 39

<form method="post">
        <label>
          <input type="checkbox" name="formpractise[]" value="32" /> Choice A
        </label> <br/>
        <label>
          <input type="checkbox" name="formpractise[]" value="16" /> Choice B
        </label> <br/>
        <label>
          <input type="checkbox" name="formpractise[]" value="8" /> Choice C
        </label> <br/>
        <label>
          <input type="checkbox" name="formpractise[]" value="4" /> Choice D
        </label> <br/>
        <label>
          <input type="checkbox" name="formpractise[]" value="2" /> Choice E
        </label> <br/>
        <label>
          <input type="checkbox" name="formpractise[]" value="1" /> Choice F
        </label> <br/>
        <input type="submit" value="Submit" />
</form>
<?
if($_POST)
{
    $message[1] = 'you will come into money';
    $message[2] = 'you will meet someone special';
    $message[3] = 'don\'t trust your neighbor';
    $message[4] = 'answers to your questions will come from SO';
    $message[5] = 'there\'s no need to read all of these';
    //...
    //...
    $message[39] = 'wow you selected lucky number 39';
    $message[40] = 'really, you\'re still reading';

    echo $message[array_sum($formpractise)];
}
?>

Just make an array of 64 messages and you'll have a different message for each combination

2
  • The 64th message will never be used. 32 + 16 + 8 + 4 + 2 + 1 = 63 Nov 20, 2011 at 19:10
  • good point. $message[0] most likely needs to be there for no selection ;-)
    – maraspin
    Nov 20, 2011 at 19:22
0

You can implement this in one page:

<?php
$messages = array(      // Array of choices and messages
    'A' => 'Message A',
    'B' => 'Message B',
    'C' => 'Message C',
    'D' => 'Message D',
    'E' => 'Message E',
    'F' => 'Message F'
);

if($_POST['submit'] === 'Submit') {  // If the form is submitted
    // If a choice was made and it is in the messages array
    if(isset($_POST['choice']) && array_key_exists($_POST['choice'], $messages)) {
        die($messages[$_POST['choice']]);  // Display the message and end execution
    } else {
        die('Unknown Message');  // Otherwise, display and error and end execution
    }
}
?>

<form action="" method="POST">
    <p><input type="radio" name="choice" value="A" /> A</p>
    <p><input type="radio" name="choice" value="B" /> B</p>
    <p><input type="radio" name="choice" value="C" /> C</p>
    <p><input type="radio" name="choice" value="D" /> D</p>
    <p><input type="radio" name="choice" value="E" /> E</p>
    <p><input type="radio" name="choice" value="F" /> F</p>
    <p><input type="submit" name="submit" value="Submit" /></p>
</form>

I also believe in this case you are looking for a radio button and not a checkbox. Checkboxes allow for multiple checks in a set of boxes where radio buttons only allow for one to be chosen from within a group.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.