douyan8772 2017-05-11 19:49
浏览 52

PHP - 我想在单击type =“reset”按钮时将值设置为false。 我怎样才能做到这一点?

I have been trying to learn PHP for the past week now.
I have created a form which acts like a calculator in which two values are either added up, subtracted, multiplied or divided.

$cal = new calculator();
if(isset($_POST['submit']))
{   
    $result = $cal->getresult($_POST['n1'],$_POST['n2'],$_POST['op']);
}

if(isset($_POST['reset'])){   
$result = false;
}

?>

<form method="post">
<table align="center">
    <tr>
        <td><b><?php echo $result; ?></b></td>
    </tr>
    <tr>
        <td>Zahl 1 </td>
        <td><input type="number" name="n1"></td>
    </tr>

    <tr>
        <td>Zahl 2 </td>
        <td><input type="number" name="n2"></td>
    </tr>

    <tr>
        <td>Select Oprator</td>
        <td><select name="op">
            <option value="+">+</option>
            <option value="-">-</option>
            <option value="*">*</option>
            <option value="/">/</option>
        </select></td>
    </tr>

    <tr>
        <td></td>
        <td><input type="submit" name="submit" value="Berechnen"></td>
    </tr>

    <tr>
        <td></td>
        <td><input type="reset" name="reset" value="Reset"></td>
    </tr>

</table>
</form> 

Beforehand I define the calculator-class but it is not relevant for this question.
So what I want to do is when I click the

<td><input type="reset" name="reset" value="Reset"></td>

button i want the variable $result to be defined as empty, which I want to achieve by

if(isset($_POST['reset'])){   
$result = false;
}

In effect, php should not show the result on top of the document, because

<tr>
        <td><b><?php echo $result; ?></b></td>
    </tr>

is empty?

Unfortunately, that doesn't happen. Does anyone know how to fix this?

  • 写回答

1条回答 默认 最新

  • doujie2356 2017-05-11 19:53
    关注

    The reset button won't submit your form, which is what needs to happen in order for your PHP to run. You can, however, use multiple submit buttons in a form and name them differently. Try this:

    <input type="submit" name="reset" value="Reset">
    

    This will trigger the form to submit, but will come across as "reset" in your POST data, and should subsequently trigger your logic to clear values.


    EDIT:

    The $result variable is dead once your page is processed server side. By time it shows up in your browser, that part of the request is over and the variable doesn't exist, so clicking a "reset" button doesn't really matter. It sounds like you want to clear the form and also clear the result display. In that case, continue to use the reset button as you were.

    And then add some javascript to clear out the result. For that to work, you need to add an ID or class to the element holding it:

    <tr>
        <td>
           <b id="result">
              <?php echo $result; ?>
           </b>
        </td>
    </tr>
    

    Then add a click handler, either in a JS file, in <script> tags, or right on the element itself:

    <input type="reset" name="reset" value="Reset" onClick="document.getElementById('result').innerHTML='0';">
    

    That would set it to "0".

    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端