如何创建函数和调用函数

时间:2013-03-20 09:23:58

标签: php html

有人能给我一个创建一个简单函数的例子,它返回两个输入字段的总和吗?

<html>
  <body>
    number1:<input type="text" name="number1" />
    number2:<input type="text" name="number2" />
    <input type=submit name="submit" value="submit"/>
  </body>
</html>

4 个答案:

答案 0 :(得分:2)

我认为这就是您要搜索的内容: http://php.net/manual/en/tutorial.forms.php

答案 1 :(得分:1)

function addition( $num1,$num2)
{

    echo "Result = " . $num1 + $num2;
}

and call the  function  by 


if(isset($_POST["submit"]))
{

  $num1=$_POST['number1'];
  $num2=$_POST['number2'];
addition( $num1,$num2);
}

答案 2 :(得分:0)

尝试以下方法:

<?php
if(isset($_POST))
{
    echo "Result = " . $_POST['number1'] + $_POST['number2'];
}
?>
<html>
<head>
<title> Example of the radio blur method</title>
</head>
<body>

<form name="form1">
<input type="text" name="number1" value="">
<input type="text" name="number2" value="">
<input type="submit" name="submit" value="Add">
</form>
</body>
</html>

答案 3 :(得分:0)

尝试;

<html>
<body>
<form action='<?php echo $_SERVER["PHP_SELF"] ?>' method='post'>
number1:<input type='text' name='number1'/>
number2:<input type='text' name='number2'/>
<input type='submit' name='submit' value='submit'/>
</form>
<br/>
<?php
if($_POST["submit"]){
   echo "$_POST[number1] + $_POST[number2] = ".($_POST["number1"] + $_POST["number2"])."<br/>\n";
}
?>
</body>
</html>