-2

如何在不使用 JavaScript 的情况下将该程序转换为仅包含计算的 php?我正在使用回声“”;我应该使用 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>
4

3 回答 3

2

PHP无法执行该程序正在执行的客户端操作,因此您无法将其转换为 PHP 以替代 JavaScript;因此,任何尝试都将是无用的。

编辑

但是,您可以将值发布到 PHP 页面并进行服务器端处理并在下一页上显示结果。

于 2013-03-06T06:13:31.460 回答
0

代码略有改动。只是试图复制“with 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>
于 2013-08-10T04:00:12.407 回答
0

你想把它完全转换成 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>
于 2013-03-06T06:26:26.263 回答