我想用复选框创建一个php表单,如下所示:
< 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>
当用户选择复选框时我想根据用户选择打印消息。不是他做出的选择,取决于他的选择。
答案 0 :(得分:0)
如果您使用的是jQuery库,那么Javascript看起来像这样:
$(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]);
}
});
});
如果您的PHP代码中包含所有消息。你有一个数组,你可以这样做
$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) . ";";
答案 1 :(得分:0)
您的复选框是一个数组,因此您需要考虑这一点。您可以有多个有效选择。不知道你的目标,但一个简单而简单的解决方案可能是:
$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)
}
}
答案 2 :(得分:0)
您可以在一个页面中实现:
<?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>
我也相信在这种情况下你正在寻找一个单选按钮,而不是一个复选框。复选框允许在一组框中进行多次检查,其中单选按钮仅允许从组中选择一个。
答案 3 :(得分:0)
当您使用复选框并且可以进行多项选择时,我会使用二进制代码来帮助我为每种可能的组合选择不同的消息。 例如,如果用户选择框A,D,E和F,则可以将其转换为二进制代码。 如果: F = 1 E = 2 D = 4 C = 8 B = 16 A = 32
您将显示的消息是消息号39(可能包含64条不同的消息) 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)];
}
?>
只需制作一个包含64条消息的数组,每个组合都会有不同的消息