如何将此程序转换为仅php与计算在那里不使用JavaScript?我使用echo " ";我应该使用If计算($choice)
吗?<html>
<head><title>hw4-5</title>
<style type='text/css'>
table{color:black;background-color:lightgray;border:6px solid black;border-width:4px;}
th{color:white;background-color:black;outline-style:solid;outline-width:2px;border:2px solid black;}
input.button{color:black;background-color:red;border:4px solid black;}
input#idname1,input#idname2,input#idname3{background-color:DEDEE6;border:4px solid black;}
</style>
<script type='text/javascript'>
function compute(choice)
{
var tbox1=document.getElementById('idname1');
var tbox2=document.getElementById('idname2');
var tbox3=document.getElementById('idname3');
if(choice==1){tbox2.value=parseInt(tbox1.value)*(tbox1.value);}
else if(choice==2){tbox3.value=parseInt(tbox1.value)*4;}
else if(choice==3){tbox2.value=parseInt(tbox1.value)*(tbox1.value);
tbox3.value=parseInt(tbox1.value)*4;}
else{tbox1.value=tbox2.value=tbox3.value='';}
}
</script>
</head>
<form>
<table>
<th colspan='2' >SQUARE PROBLEM</th>
<tr><td><label>Side: </label></td><td><input type='text' id='idname1' /></td></tr>
<tr><td><label>Area: </label></td><td><input type='text' id='idname2' /></td></tr>
<tr><td><label>Perimeter:</label></td><td><input type='text' id='idname3' /></td></tr>
<tr><td colspan='2' >
<input class='button' type='button' value='Area' onClick='compute(1)' />
<input class='button' type='button' value='Perimeter' onClick='compute(2)' />
<input class='button' type='button' value='Both' onClick='compute(3)' />
<input class='button' type='button' value='Clear' onClick='compute(4)' /></td></tr>
</table>
</form>
</html>
PHP不能执行客户端操作该程序正在执行,因此您不能将其转换为PHP作为JavaScript的替代品;因此,任何次数的尝试都将是无用的。
编辑
但是,你可以将你的值发布到PHP页面,并在服务器端进行处理,并在下一页显示结果。
你想把它完全转换成PHP吗?然后你可以这样做
<?php
if(isset($_POST) && $_POST['submitbtn']!=''){
$tbox1 = $_POST['idname1'];
$tbox2 = $_POST['idname2'];
$tbox3 = $_POST['idname3'];
if($_POST['submitbtn'] =='Area'){$tbox2 = $tbox1*$tbox1;}
if($_POST['submitbtn'] =='Perimeter'){$tbox3 = $tbox1*4;}
if($_POST['submitbtn'] =='Both'){$tbox2 = $tbox1*$tbox1;$tbox3 = $tbox1*4;}
}
?>
<form action="" method="post">
<table>
<tr><th colspan='2' >SQUARE PROBLEM</th></tr>
<tr><td><label>Side: </label></td><td><input type='text' id='idname1' name="idname1" value="<?php echo $tbox1?>" /></td></tr>
<tr><td><label>Area: </label></td><td><input type='text' id='idname2' name="idname2" value="<?php echo $tbox2?>" /></td></tr>
<tr><td><label>Perimeter:</label></td><td><input type='text' id='idname3' name="idname3" value="<?php echo $tbox3?>" /></td></tr>
<tr><td colspan='2'>
<input class='button' value='Area' name="submitbtn" type="submit" />
<input class='button' value='Perimeter' name="submitbtn" type="submit" />
<input class='button' value='Both' name="submitbtn" type="submit" />
<input class='button' value='Clear' name="clearbtn" type="reset" /></td></tr>
</table>
</form>
有细微变化的代码。只是试图复制"与js"的确切行为!
<html>
<head><title>hw4-5</title>
<style type='text/css'>
table{color:black;background-color:lightgray;border:6px solid black;border-width:4px;}
th{color:white;background-color:black;outline-style:solid;outline-width:2px;border:2px solid black;}
input.button{color:black;background-color:red;border:4px solid black;}
input#idname1,input#idname2,input#idname3{background-color:DEDEE6;border:4px solid black;}
</style>
<?php
$idname1 = "";
$answerA=0;
$answerP=0;
if (isset($_POST['butt1'])) {
$idname1 = $_POST['idname1'];
if($_POST['butt1']=="Area"){$answerA=$_POST['idname1'] * $_POST['idname1'];}
else if($_POST['butt1']=="Perimeter"){$answerP=$_POST['idname1']*4;}
else if($_POST['butt1']=="Both"){$answerA=$_POST['idname1'] * $_POST['idname1'];$answerP=$_POST['idname1']*4;}
else{$answerA = $answerP = $idname1="";}
}
?>
</head>
<form method="post">
<table>
<th colspan='2' >SQUARE PROBLEM</th>
<tr><td><label>Side: </label></td><td><input type='text' id='idname1' name="idname1" value="<?php echo($idname1); ?>"/></td></tr>
<tr><td><label>Area: </label></td><td><input type='text' id='idname2' name="idname2" value="<?php echo($answerA); ?>"/></td></tr>
<tr><td><label>Perimeter:</label></td><td><input type='text' id='idname3' name="idname3" value="<?php echo($answerP); ?>"/></td></tr>
<tr><td colspan='2' >
<input class='button' type='submit' value='Area' name="butt1" />
<input class='button' type='submit' value='Perimeter' name="butt1" />
<input class='button' type='submit' value='Both' name="butt1"/>
<input class='button' type='submit' value='Clear' name="butt1"/></td></tr>
</table>
</form>
</html>