我无法获取我的PHP代码将我的$ result变量写入XML文件。它确实编写了我刚刚为测试添加的$ number1变量,但它不想添加$ result。我需要这个,所以我可以为我的计算器制作一个记忆和记忆回忆按钮。这是一个学校项目,我必须使用XML。感谢。
<?php
$number1 = $_POST['number1'];
$number2 = $_POST['number2'];;
$operator = $_POST['operator'];
if ($operator == 'subtract'){
subtract();
}
if ($operator == 'add'){
add();
}
if ($operator == 'multiply'){
multiply();
}
if ($operator == 'divide'){
divide();
}
function add() {
global $number1;
global $number2;
$result = $number1 + $number2;
echo $result;
}
function subtract() {
global $number1;
global $number2;
$result = $number1-$number2;
echo $result;
}
function multiply() {
global $number1;
global $number2;
$result = $number1*$number2;
echo $result;
}
function divide() {
global $number1;
global $number2;
$result = $number1/$number2;
echo $result;
}
$handle = fopen('Data.xml', 'w');
fwrite($handle, "<inputs>\n\t<input>\n\t\t<answer>".$result."</answer>\n\t\t<number1>".$number1."</number1>\n\t</input>\n</inputs>");
fclose($handle);
echo "Write to input.xml successfully";
?>
<html>
<center>
<h1 style="color:blue;">Rohit's Math Machine</h1>
<form action="Code.php" method="POST">
Enter Number 1: <input type="text" name="number1" method="POST"><br><br>
<select name="operator" method="POST">
<option value="subtract">subtract</option>
<option value="add">add</option>
<option value="multiply">multiply</option>
<option value="divide">divide</option>
</select><br><br>
Enter Number 2: <input type="text" name="number2" method="POST"><br><br>
<input type="submit" value="Submit">
</form>
</center>
</html>
&#13;