我有4个单选按钮+
,-
,*
,/
我打算如果选择+
它会添加结果在两个文本框中显示它并将其显示到第三个texbox,因此如果我选择-
符号,它将减去两个texbox并将其显示到第三个文本框,依此类推。任何帮助都将不胜感激。 this is the picture
<html>
<body>
<?php
function add($x,$y){
$add=$x + $y;
return $add;
}
function subtract($x,$y){
$subtract=$x - $y;
return $subtract;
}
function multiply($x,$y){
$multiply=$x * $y;
return $multiply;
}
function divide($x,$y){
$divide=$x / $y;
return $divide;
}
?>
<form action="#" method="post">
Num1:<input name="num1"><br>
<input type='radio' name='express' value='add'>+
<input type='radio' name='express' value='subtract'>-
<input type='radio' name='express' value='multiply'>*
<input type='radio' name='express' value='divide'>/
<br>
Num2:<input name="num2">
Result<input value='<?php
?>'>
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])){
$x=$_POST['num1'];
$y=$_POST['num2'];
$z=$_POST['express'];
if($z == 'add'){
echo add($x,$y);
}
else if($z=='subtract'){
echo subtract($x,$y);
}
else if($z=='multiply'){
echo multiply($x,$y);
}
else{
echo divide($x,$y);
}
}
?>
</body>
</html>
答案 0 :(得分:0)
<html>
<body>
<?php
$Result = '';
if(isset($_POST['submit'])){
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$express = $_POST['express'];
switch ($express) {
case 'add':
$Result = $num1 + $num2;
break;
case 'subtract':
$Result = $num1 - $num2;
break;
case 'multiply':
$Result = $num1 * $num2;
break;
case 'divide':
if(0 != $num2 ){
$Result = $num1 / $num2;
}
break;
default:
break;
}
}
?>
<form action="#" method="post">
Num1:<input name="num1"><br>
<input type='radio' name='express' value='add'>+
<input type='radio' name='express' value='subtract'>-
<input type='radio' name='express' value='multiply'>*
<input type='radio' name='express' value='divide'>/
<br>
Num2:<input name="num2">
Result<input value='<?php echo $Result;?>'>
<input type="submit" name="submit">
</form>
</body>
</html>
我建议您使用JavaScript并将PHP代码与HTML代码分开来实现它。
答案 1 :(得分:0)
这是一个jQuery版本。我确信在jQuery中比我更熟练的人可以制作更精明的版本:
<强>演示:强>
http://jsfiddle.net/pn1v1az7/4/
<强> jQuery的:强>
function CalculateVals(Obj)
{
// Set default value as 0
var Result = 0;
// Get the value of the checked radio box
var UseOpr = $("input[name='express']:checked").val();
// Get the value of input num1
var n1 = $("input[name='num1']").val();
// Get the value of input num2
var n2 = $("input[name='num2']").val();
// See if the value for one is numeric, if not make it 0
var Num1 = (!isNaN(n1))? n1:0;
// Same as above
var Num2 = (!isNaN(n2))? n2:0;
// Loop through to find the operator being used
switch(UseOpr) {
case('+'):
// Using the plus like "+Num1" converts string to number, just in case...
Result = +Num1 + +Num2;
break;
case('-'):
Result = +Num1 - +Num2;
break;
case('*'):
Result = +Num1 * +Num2;
break;
case('/'):
Result = +Num1 / +Num2;
}
// Insert the mathematical result into box 3
$("#result").val(Result);
}
// If the form changes
$("form").change(function() {
// Run the above calculation function by passing the form as an argument
CalculateVals($(this));
});
<强>形式:强>
<form action="#">
Num1:<input name="num1"><br>
<input type='radio' name='express' value='+' />+
<input type='radio' name='express' value='-' />-
<input type='radio' name='express' value='*' />*
<input type='radio' name='express' value='/' />/
<br>
Num2:<input name="num2" />
Result<input id="result" name="result" value="0">
</form>