나는 PHP를 처음 사용합니다. 제출 버튼을 누른 후 필드가 명확 해지는 것을 막으려 고합니다. 필요할 때 필드를 지울 수있는 재설정 버튼을 만들고 싶습니다. 여기에 코드가 이해되기를 바랍니다. 어떻게 해결했는지 설명해주세요. 감사합니다.
<?php
$result = "";
class calculator
{
var $a;
var $b;
function checkopration($oprator)
{
switch($oprator)
{
case '+':
return $this->a + $this->b;
break;
case '-':
return $this->a - $this->b;
break;
case '*':
return $this->a * $this->b;
break;
case '/':
return $this->a / $this->b;
break;
default:
return "Sorry No command found";
}
}
function getresult($a, $b, $c)
{
$this->a = $a;
$this->b = $b;
return $this->checkopration($c);
}
}
$cal = new calculator();
if(isset($_POST['submit']))
{
$result = $cal->getresult($_POST['n1'],$_POST['n2'],$_POST['op']);
}
?>
<form method="post">
<table align="center">
<tr>
<td><strong><?php echo $result; ?><strong></td>
</tr>
<tr>
<td>Enter 1st Number</td>
<td><input type="text" name="n1"></td>
</tr>
<tr>
<td>Enter 2nd Number</td>
<td><input type="text" 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=" = "></td>
</tr>
</table>
</form>
코드를 약간 정리하고 클래스 이름을 지정한 모든 요소를 지우는 자바 스크립트 함수를 추가했습니다. clearOnReset
<form method="post">
<table align="center">
<tr>
<td><strong class="clearOnReset"><?php echo $result; ?></strong></td>
</tr>
<tr>
<td>Enter 1st Number</td>
<td><input type="text" name="n1" class="clearOnReset" value="<?php if(isset($_POST['n1'])){echo htmlspecialchars($_POST['n1']);}?>"></td>
</tr>
<tr>
<td>Enter 2nd Number</td>
<td><input type="text" name="n2" class="clearOnReset" value="<?php if(isset($_POST['n2'])){echo htmlspecialchars($_POST['n2']);}?>"></td>
</tr>
<tr>
<td>Select Oprator</td>
<td>
<select name="op">
<option value="+" <?php if(isset($_POST['op']) && $_POST['op']=="+") { echo "selected";}?>>+</option>
<option value="-" <?php if(isset($_POST['op']) && $_POST['op']=="-") { echo "selected";}?>>-</option>
<option value="*" <?php if(isset($_POST['op']) && $_POST['op']=="*") { echo "selected";}?>>*</option>
<option value="/" <?php if(isset($_POST['op']) && $_POST['op']=="/") { echo "selected";}?>>/</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value=" = ">
</td>
</tr>
</table>
</form>
<button onclick="resetCalculator()">reset calculator</button>
<script>
function resetCalculator(){
var fieldsToClear = document.getElementsByClassName("clearOnReset");
for(var i = 0; i < fieldsToClear.length; i++){
fieldsToClear[i].value="";
}
}
</script>
자바 스크립트 함수가하는 일은 클래스 이름 clearOnReset
을 가진 모든 요소를 찾은 다음 fieldsToClear
The line 이라는 배열에 배치하는 것입니다. 그 아래 줄은 해당 배열의 모든 요소를 통과하여 ""
본질적으로 아무것도 아닌 값을 설정합니다 .
PHP 코드 ive 추가는 특정 게시 요청 매개 변수가 설정되었는지 확인하고 설정되어 있으면 양식의 해당 위치에 배치합니다.
여전히 질문이 있거나 내 코드에 문제가 있음을 발견하면이 답변 아래에 의견을 작성하십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다