您好,我是php初学者,我的代码存在以下问题,我需要添加2个数字并将结果作为第3个数字(总和)回显。总和数字不得在单击“提交”按钮之前显示,仅这两个数字的输入框,当显示总和时,必须隐藏两个输入框,并且只显示总和
<html>
<body>
<form method="post" id="form1">
First Number:
<input type="text" name="number1" /><br>
Second Number:
<input type="text" name="number2" /><br>
<input type="submit" name="submit" value="Add" >
</form>
<?php
$number1 = $_POST['number1'];
$number2 = $_POST['number2'];
$sum = $number1+$number2;
echo "Sum = ".$sum;
?>
</script>
</body>
</html>
答案 0 :(得分:0)
您需要使用isset函数进行检查,以了解是否提交了表单。像下面一样,
<body>
<?php if( !isset($_POST['submit'])){ ?>
<form method="post" id="form1">
First Number:
<input type="text" name="number1" /><br>
Second Number:
<input type="text" name="number2" /><br>
<input type="submit" name="submit" value="Add" >
</form>
<?php } ?>
<?php
if(isset($_POST['submit'])){
$number1 = $_POST['number1'];
$number2 = $_POST['number2'];
$sum = $number1+$number2;
echo "Sum = ".$sum;
}
?>
</script>
</body>
</html>