我正在将 html 的输入语句附加到 php 中的变量,但是当我在输入标签周围使用 div 时,变量不会传输



在此代码中,当我在第一个输入标签周围使用div与类(用于从用户中获取数字)时,输入中的值不会传输到php变量,它也不会显示任何类型的答案。

我只是问我将如何通过div 类将输入的值转移到 php 变量,因为在不使用div 的情况下,值会被传输。

<form method="" class="form-horizontal">
<label class="num1label" for="num1">Enter the first number:</label>
<div class="aling-center" name="number1">
<input type="number" class="num2" name="num1" id="num1" placeholder="Enter the First Number"><br>
</div>
<label class="num2label" for="num2">Enter the second number:</label>
<input type="number"class="num2" id="num2" name="num2" placeholder="Enter the Second Number"><br>
<label for="operator">Choose your operator:</label>
<select name="operator" id="operator">
<option>None</option>
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Division</option>
</select>
<button type="submit" name="submit" class="btn btn-success" value="submit">Calculate</button>
<p>This is the answer:</p>
</form>
</div>
<?php
if (isset($_GET['submit'])) {
$num1=$_GET['number1/num1'];
$num2=$_GET['num2'];
$operator=$_GET['operator'];
switch ($operator) {
case "None":
echo "0";
break;
case "Add":
echo $num1+$num2;
break;
case "Subtract":
echo $num1-$num2;
break;
case "Multiply":
echo $num1*$num2;
break;
case "Division":
echo $num1/$num2;
break;
default:
echo "0";
break;
}
}  
?>

您的表单缺少方法,并且您正在使用 GET 获取数据。另外,您的表单中没有操作,因此我假设您的PHP代码在同一页面上。把它放在你的表格中。

<form method="get" action="#" class="form-horizontal">

使用 PHP 代码获取数据时,更改此行:

$num1=$_GET['num1'];

另外,输入 print_r($_GET),以便您可以看到表单返回的完整集。然后,您将看到哪些内容通过,哪些没有通过,以便您可以对代码进行调整。

最新更新