2013-03-06 140 views
-2

如何将此程序转换为仅使用php而不使用JavaScript的计算?我使用echo“”;我应该用一个如果计算($选择)将Javascript转换为PHP

<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> 

回答

2

PHP无法执行客户端操作这一计划正在执行它,因此你不能将此转换为PHP是对JavaScript的替代品;因此任何尝试都是无用的。

编辑

但是你可以发布你的价值观的PHP页面,做服务器端处理和显示结果下页。

+0

为什么不是ajax?计算将在php中完成,事件将由JS处理。额外的http请求这种情况将是矫枉过正,但嘿,这就是OP问 – 2013-03-06 06:16:24

+0

@DamienPirsy这是真的,但是当OP甚至不想在那里的JavaScript,那么AJAX是没有问题,我假设。由于OP说'只有PHP的计算在那里,而不使用JavaScript' – 2013-03-06 06:20:10

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> 
0

代码略有变化。只是试图复制“与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>