当我说'new'时,我的意思是这是我第一次尝试php。
无论如何。我不断收到此错误通知"未定义的索引:在第 33 行输入 c:''x''calculator.php",但它仍然回响"你忘了选择数学类型!"并且计算器工作正常。仅当我没有为数学类型(+-/*)选择任何单选框时,才会发生此错误通知。
//Part of the form
<form action="calculator.php" method="post">
<input type="text" name="1stnumber">
<input type="text" name="2ndnumber">
<input type="radio" name="type" value="addition">
<input type="radio" name="type" value="subtraction">
<input type="submit" name="send">
<?php
//My variables
$number = $_POST['1stnumber']
$numbero = $_POST['2ndnumber']
$mathtype = $_POST['type'] /* **<-line 33** */
//The calculation part of the form here, which is working
//Tell the user if he didn't pick a math type (+-)
if(is_null($mathtype)){
echo "You forgot to pick mathtype!"
}
?>
也尝试过其他..我看不出第 33 行和 if(is_null()) 行之间有什么问题!
对不起,如果它看起来很差,凌乱,或者如果有什么没有意义。也可能是一些错别字。任何帮助,不胜感激。
只需在拿起之前检查类型是否发布
if(isset($_POST['type']))
{
$mathtype = $_POST['type'];
}
else
{
echo "Type was not selected";
}
使用 checked
属性设置默认选定选项。
<label><input type="radio" name="type" value="addition" checked="checked"> +</label>
<label><input type="radio" name="type" value="subtraction"> -</label>
不要忘记输入需要 html 中的标签,如果省略,您可以使用 placeholder
属性,但这显然是不可能的 type="radio"
; 因此将input
包装在label
中,旁边有文本描述,例如 + 或 -
另外,这是一个复制和粘贴错误,bc 所有 php 语句都必须以分号结尾;
$number = $_POST['1stnumber']; // <- terminate
$numbero = $_POST['2ndnumber']; // <- terminate
$mathtype = $_POST['type']; // <- terminate
echo "You forgot to pick mathtype!"; // <- terminate
检查您尝试从 $_POST 中检索的变量是否确实已设置始终是一种很好的做法,请尝试以下操作:
<?php
//My variables
if (isset($_POST['1stnumber'])) {
$number = $_POST['1stnumber'];
}
if (isset($_POST['2ndnumber'])) {
$numbero = $_POST['2ndnumber'];
}
if (isset($_POST['type'])) {
$mathtype = $_POST['type']; /* **<-line 33** */
}
//The calculation part of the form here, which is working
//Tell the user if he didn't pick a math type (+-)
if (is_null($mathtype)) {
echo "You forgot to pick mathtype!";
}
?>
检查表单是否已发布:
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
//My variables
$number = $_POST['1stnumber']
$numbero = $_POST['2ndnumber']
$mathtype = $_POST['type'] /* **<-line 33** */
//The calculation part of the form here, which is working
//Tell the user if he didn't pick a math type (+-)
if(is_null($mathtype)){
echo "You forgot to pick mathtype!"
}
}
?>
否则,is_null检查也将在第一次加载时执行(在表单发布之前)。