-1

我是一个没有任何脚本的简单计算器,但它不起作用,当我点击按钮时,哪个函数被声明它不能正常工作,

<html>

<head>
</head>

<body>

<form method="post" action="">
<table align="center" border="1px" bordercolor="#CC0033" bgcolor="#99CC00"> 
<tr>
<td>
Number 1:</td><td><input type="text" name="number1"></td>
</tr>

<tr>
<td>

Number 2:</td><td> <input type="text" name="number2"></td>
</tr> 

<tr>
<td> 
Get Result:</td><td> <input type="text" name="total"> </td>
</tr>

<tr>
<td class="2">
<input type="button"  value="ADD" onclick="addition()">
<input type="button"  value="SUB" onclick="subtraction()">
<input type="button" value="MUL" onclick="multiply();">
<input type="button"   value="DIV" onclick="division()">
<input type="button"  value="MOD" onclick="modulus()">
</td>
</tr>
</table>
</form>

</body>
</html>

Bellow 是一个 php 函数,我是 crated.but is not working

<?php

  error_reporting(E_ALL ^ E_NOTICE);

//if()
    function addition(){

        $number1=$_POST['number1'];
        $number2=$_POST['number2'];
        echo $total=$number1+$number2;

    }


    function subtraction(){

        $number1=$_POST['number1'];
        $number2=$_POST['number2'];
        echo $total=$number1-$number2;


    }


    function multiply(){

        $number1=$_POST['number1'];
        $number2=$_POST['number2'];
        echo $total=$number1*$number2;
    }


    function division(){

        $number1=$_POST['number1'];
        $number2=$_POST['number2'];
        echo $total=$number1/$number2;
    }


    function modulus(){

        $number1=$_POST['number1'];
        $number2=$_POST['number2'];
        echo $total=$number1%$number2;


    }


 ?>
4

4 回答 4

1

您不能直接从 html 上下文调用 php 函数。像你一样使用一个$_POST变量number1number2就像这样:

<input type="submit" name="mode" value="MOD">

在php方面:

if(isset($_POST['mode']) && $_POST['mode']=='MOD') modulus();

...或者干脆使用javascript。

于 2013-03-20T08:45:06.153 回答
1

试试这个。你应该制作javascript函数。

<html>

<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
function addition()
{
    var total = parseInt($('#number1').val()) + parseInt($('#number2').val());
    $('#total').val(total);
}

function subtraction()
{
    var total = parseInt($('#number1').val()) - parseInt($('#number2').val());
    $('#total').val(total);
}

function multiply()
{
    var total = parseInt($('#number1').val()) * parseInt($('#number2').val());
    $('#total').val(total);
}

function division()
{
    var total = parseInt($('#number1').val()) / parseInt($('#number2').val());
    $('#total').val(total);
}

function modulus()
{
    var total = parseInt($('#number1').val()) % parseInt($('#number2').val());
    $('#total').val(total);
}
</script>
</head>

<body>

<form method="post" action="">
<table align="center" border="1px" bordercolor="#CC0033" bgcolor="#99CC00"> 
<tr>
<td>
Number 1:</td><td><input type="text" name="number1" id="number1"></td>
</tr>

<tr>
<td>

Number 2:</td><td> <input type="text" name="number2" id="number2"></td>
</tr> 

<tr>
<td> 
Get Result:</td><td> <input type="text" name="total" id="total"> </td>
</tr>

<tr>
<td class="2">
<input type="button"  value="ADD" onclick="addition()">
<input type="button"  value="SUB" onclick="subtraction()">
<input type="button" value="MUL" onclick="multiply();">
<input type="button"   value="DIV" onclick="division()">
<input type="button"  value="MOD" onclick="modulus()">
</td>
</tr>
</table>
</form>

</body>
</html>
于 2013-03-20T08:58:57.437 回答
1

JavaScript 中没有定义点击事件!!!实现目标的一种方法是单击全部删除并尝试以下按钮:

<input type="submit" name="operation" value="ADD">
<input type="submit" name="operation" value="SUB">
<input type="submit" name="operation" value="MUL">
<input type="submit" name="operation" value="DIV">
<input type="submit" name="operation" value="MOD">

这样,当单击“一个”按钮时,您将按如下方式测试 $_POST['operation'];并让您的函数将结果返回到 $total

<?php 
$operation = isset($_POST['operation']) ? $_POST['operation'] : NULL;
switch($operation) {
    case 'ADD':
        $total = addition();
        break;

    case 'SUB':
        $total = subtraction();
        break;

    case 'MUL':
        $total = multiply();
        break;

    case 'DIV':
        $total = division();
        break;

    case 'MOD':
        $total = modulus();
        break;

    default:
        $total = '';
}
?>
于 2013-03-20T09:07:10.467 回答
0

您已经在 PHP 中声明了函数,并且在onclick尝试调用 javascript 函数的事件上调用该函数。

如果你想调用 php 函数,你应该尝试

<input type="button" name="add" value="ADD">

<?php
    if(isset($_POST['add']))
    {
          addition();
    }
?>
于 2013-03-20T08:47:17.083 回答