Javascript到PHP源代码转换

时间:2015-06-01 11:37:25

标签: javascript php jquery

我想将以下客户端javascript代码转换为服务器端PHP以保护某些代码。下面是我希望在PHP中使用的示例代码(并且不在每行使用echo,因为除了打开和关闭PHP标记之外,这不会隐藏任何内容!)

function calc() {
    var aa = document.a.lsofa.value * 40.77;
    var bb = document.a.ssofa.value * 29.26;
    var z90 = "Text here.";
    var ctt = aa + bb;
    ctt = parseInt(ctt);
    tot = ctt;
    if (tot < 1) {
        var rslt = "Please enter relevant quantities in the form above.";
    }
    else {
        var rslt = "Complete.  We would require " + ctt.toString() + z95 + "";
    }

    document.a.answer.value = rslt

此外,表单的onclick事件需要更改(我希望将转换后的代码保留在同一页面上,只需在表单提交时调用此函数)。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

你的意思是这样的:

header('Content-type: application/json'); 

$aa  = $_GET["lsofa"] * 40.77;
$bb  = $_GET["ssofa"] * 29.26;
$z90 = "Text here.";
$ctt = aa+bb;
$ctt = intval(ctt);
$tot = $ctt; // not useful

if ($tot<1) {
  echo json_encode(array('result' => "Please enter relevant quantities in the form above."));
} else {
  echo json_encode(array('result' => "Complete.  We would require " . $ctt . $z95));
}

调用
 $(function() {
   $("#myForm").on("submit",function(e) {
     e.preventDefault(); // stop submission
     $.get("calc.php",$(this).serialize(),function(data) {
       alert(data.result);
     });
   });  
 });