【问题标题】:How do I make my radio buttons work with if statements for calculations?如何使我的单选按钮与 if 语句一起用于计算?
【发布时间】:2016-01-19 22:32:03
【问题描述】:

所以对于课堂作业,我们使用 HTML 和 PHP 制作一个表单,要求用户输入两 (2) 个数字,并使用单选按钮在加法、减法、乘法或除法形式之间进行选择,然后输入猜测至于答案是什么。我们的任务是向用户回显,说明他们的答案是否正确。

我遇到麻烦的部分是“if”和“else”语句。例如,如果我选择 1 和 2 作为我的数字,猜 3 并选择加法作为我的计算方法,我不仅会得到回应“恭喜你!你选择了正确的答案 3!”但我也从除法、减法和乘法中得到我的 else 语句,指出我做错了。

这是我的代码:(请记住——生日功能尚未创建。) 此外,我在表单验证方面遇到了困难——使用户实际上输入了必填字段。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Howdy</title>
</head>

<body>

<h1>PHP_SELF</h1>

<?php


//process the form if the submit button was pressed
if (isset($_POST['submit'])) {

//form validation goes here


/////////////////////////////////////////VARIABLES ARE BEING MADE HERE
//simplify the form variables
//later on we will do this in form validation

//create a variable called firstname and store in it
//the value from the POST array for firstname from the form
$firstname = $_POST['firstname'];

//creating variables for num1 and num2 that user inputed
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];

//creating a variable called sum
//this is for when the user chooses to ADD 
//num1 and num2
$sum = $num1 + $num2;

//creating a variable called difference
//this is for when the user chooses to SUBTRACT
//num1 and num2
$difference = $num1 - $num2;

//creating a variable called product
//this is for when the user chooses to MULTIPLY
//num1 and num2
$product  = $num1 * $num2;

//creating a variable called quotient
//this is for when the user chooses to DIVIDE
//num1 and num2
$quotient = $num1 / $num2;

//creating a variable called guess and store it in the
//value from the POST array for the guess from the form
$guess = $_POST['guess'];

//creating a variable called birthday and store it in the
//value from the POST array for the user's birthday
$birthday = $_POST['birthday'];



if ($sum == $guess) {

echo "<p>Congratulations $firstname. You answered correctly with $guess.<p>\n";

} else if ($sum != $guess){

echo "<p>$firstname, you answered incorrectly. The correct answer is $sum.</p>\n";

} 


if ($difference == $guess) {

echo "<p>Congratulations $firstname. You answered correctly with $guess.<p>\n";

} else if ($difference != $guess){

echo "<p>$firstname, you answered incorrectly. The correct answer is $difference.</p>\n";

} 


if ($product == $guess) {

echo "<p>Congratulations $firstname. You answered correctly with $guess.<p>\n";

} else if ($product != $guess){

echo "<p>$firstname, you answered incorrectly. The correct answer is $product.</p>\n";

}       



if ($quotient == $guess) {

echo "<p>Congratulations $firstname. You answered correctly with $guess.<p>\n";

} else if ($quotient != $guess){

echo "<p>$firstname, you answered incorrectly. The correct answer is $quotient.</p>\n";

} 


} //end of the isset submit conditional statement


//show the form if it is the user's first time her OR if any of the required forms are missing

if(!isset($_POST ['submit']) OR empty($firstname) OR empty($num1) OR empty($num2)) { ?>

<h2>Please fill out the following: </h2>



<!--FORM BEGINS-->
<form action= "<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p><label for="firstname">Please enter your first name: </label>
<input id="firstname" type="text" size="30" name="firstname" value="<?php if(isset($firstname)) echo $firstname; ?>"/><p>

<!--Challenge Dealio-->
<p><label for="Num1">Please enter a number: </label>
<input id="Num1" type="number" size="30" name="num1" value="<?php if(isset($num1)) echo $num1; ?>" /></p>

<p><label for="Num2">Please enter another number: </label>
<input id="Num2" type="number" size="30" name="num2" value="<?php if(isset($num2)) echo $num2; ?>"/><p>

<p>Please choose one of the following: </p>
<p>
    <input name="add" id="answer" type="radio" value="add" />Add<br />
    <input name="subtract" id="answer" type="radio" value="subtract" />Subtract<br />
    <input name="multiply" id="answer" type="radio" value="multiply" />Multiply<br />
    <input name="divide" id="answer" type="radio" value="divide" />Divide<br />
</p>  

<p><label for="guess">Please put in a guess for the answer: </label>
<input id="guess" type="number" size="30" name="guess" value="<?php if(isset($guess)) echo $guess; ?>"/></p>

<p><label for="birthday">Please enter your birth date: </label>
<input id="birthday" type="date" size="30" name="birthday" value="<?php if(isset($birthday)) echo $birthday; ?>"/></p>

<!--Submit Button-->
<input type="submit" name="submit" value="Enter" />
</form>

<?php
} //end form conditional statement
?>



</body>
</html>




What did I do wrong? And how can I go about fixing this?

【问题讨论】:

    标签: php if-statement button radio


    【解决方案1】:

    首先,所有单选按钮应该具有相同的名称和不同的值。在同一页面中,您不能拥有相同的 ID

     <p>
         <input name="action" id="add" type="radio" value="add" />Add<br />
         <input name="action" id="subtract" type="radio" value="subtract" />Subtract<br />
         <input name="action" id="multiply" type="radio" value="multiply" />Multiply<br />
         <input name="action" id="divide" type="radio" value="divide" />Divide<br />
     </p>  
    

    然后在你的php中执行选择的选项,

     if($_POST['action'] == "add") {
         $result = $num1 + $num2;
     } else if($_POST['action'] == "subtract") {
         $result = $num1 - $num2;
     } else if($_POST['action'] == "multiply") {
         $result  = $num1 * $num2;
     } else if($_POST['action'] == "divide") {
         $result = $num1 / $num2;
     }
    

    将所有值重命名为$result

     if($result == $guess) {
          echo "<p>Congratulations $firstname. You answered correctly with $guess.<p>\n";
     } else {
          echo "<p>$firstname, you answered incorrectly. The correct answer is $difference.</p>\n";
    
    } 
    

    不用检查那么多次,取结果到1个变量,只检查一次。

    编辑:生日部分 在视图中

    <select name="year">add options here</select>
    <select name="month">add options here</select>
    <select name="day">add options here</select>
    

    在 PHP 中,

    $month = $_POST['month'];
    $year = $_POST['year'];
    $day = $_POST['day'];
    
    $date = $year ."-". $month ."-".$day;
    
    $date = date("Y-m-d",strtotime($date));
    
    if(date('m-d') == date('m-d', $date)) {
        // today is users birthday. show any message you want here.
     }
    

    【讨论】:

    • 好的,很好。现在,如果他们的猜测不正确,我将如何向用户回显?是差不多还是差不多?
    • 非常感谢!这对我帮助很大!如果您不介意,我可能还有更多问题! :)
    • 如果回答有帮助,请采纳。接受答案。
    • @WalkinDeadGirll 有任何疑问可以在回答中提出,我会帮你的
    • 这是关于我的生日功能的事情......我有点不知道从哪里开始。 """ 还要求他们输入他们的出生日期。在您的表单中使用三个下拉列表,以允许用户选择他们的出生日期的月份、日期和年份。应该为月份和这些列表中的每一个使用一个数组将需要使用循环将值放入下拉列表。如果用户在出生日期访问您的网站,他们应该会看到“特殊消息”。"""
    【解决方案2】:

    首先,您的收音机不能有相同的 id。它们必须不同且名称相同。

    <p>
       <input name="action" id="add" type="radio" value="add" />Add<br />
       <input name="action" id="subtract" type="radio" value="subtract" />Subtract<br />
       <input name="action" id="multiply" type="radio" value="multiply" />Multiply<br />
       <input name="action" id="divide" type="radio" value="divide" />Divide<br />
    </p>  
    

    那么您并没有验证用户通过复选框选择的功能。您正在遍历所有ifelse,而不过滤选择了哪个函数。

    我要做的是使用 Switch Case 语句来过滤选择了哪种计算方法,然后比较猜测是否正确。比如:

    switch $answer {
                    case "add":
                    //Check if guess is right and echo
                    break;
                    case "substract":
                    //Check if guess is right and echo
                    break;
                    case "multiply":
                    //Check if guess is right and echo
                    break; 
                    case "divide":
                    //Check if guess is right and echo
                    break;
                  };
    

    如果不允许使用 Switch Case 语句,则应首先检查标记了哪个答案,然后判断是否正确:

    if($answer == "add"){
       if($sum == $guess){
          echo "Congrats";
       } else {
          echo "error";
       }
    } else if($answer == "substract"){
       if($difference == $guess){
          echo "Congrats";
       } else {
          echo "error";
       }
    } else if($answer == "multiply"){
       if($product == $guess){
          echo "Congrats";
       } else {
          echo "error";
       }
    } else if($answer == "divide"){
       if($quotient == $guess){
          echo "Congrats";
       } else {
          echo "error";
       }
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 2021-07-03
      • 2013-07-21
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 2012-04-19
      • 2014-05-31
      • 2015-09-13
      相关资源
      最近更新 更多