执行java脚本并在我的php查询中使用结果

Executing java-script and using the result in my php query

本文关键字:查询 结果 php 我的 java 脚本 执行      更新时间:2023-09-26

我有一个执行计算的java脚本函数。我想在我的 php 代码中使用该计算的答案。

document.write(Fixed((PoissonTerm( X, Y )),8,4))

值 X 和 Y 都来自我的 php 代码中的变量,所以

<?php
$valueofx;
$valueofy;
?>

在理想的世界中,我想看起来像这样

<?php
$thejavascriptvalue = document.write(Fixed((PoissonTerm( $valueofx, $valueofy )),8,4))
?>

我知道这不可能做到,我需要拉取和使用 5 个不同的值。无论如何我可以解决它吗?我不介意刷新页面或从另一个页面抓取它,只要我可以在我的 php 代码中使用 5 个值。

我需要运行 javascript 10 次才能重定向,例如

 document.write(Fixed((PoissonTerm(0.1, 0 )),8,4))
   document.write(Fixed((PoissonTerm( 8, 2 )),8,4))
   document.write(Fixed((PoissonTerm( 6, 3 )),8,4))

下面如果 JavaScript

function Fixed( s, wid, dec ) {
   // many combinations of possibilities
   // maybe prepare for upcoming truncate
   var z = 1
   if (dec > 0) {
      z /= Math.pow( 10, dec );
      if (s < -z)  s -= 0.5 * z;
      else
         if (s > z)  s += 0.5 * z;
         else
            s = 0;
      }
   // assure a string
   s = "" + s;
   // chop neg, if any
   var neg = 0;
   if (s.charAt(0) == "-") {
      neg = 2;
      s = s.substring( 1, s.length );
      }
   // chop exponent, if any
   var exp = "";
   var e = s.lastIndexOf( "E" );
   if (e < 0)  e = s.lastIndexOf( "e" );
   if (e > -1) {
      exp = s.substring( e, s.length );
      s = s.substring( 0, e );
      }
   // if dec > 0 assure "."; dp == index of "."
   var dp = s.indexOf( ".", 0 );
   if (dp == -1) {
      dp = s.length;
      if (dec > 0) {
         s += ".";
         dp = s.length - 1;
         }
      }
   // assure leading digit
   if (dp == 0) {
      s = '0' + s;
      dp = 1;
      }
   // not enough dec pl?  add 0's
   while ((dec > 0) && ((s.length - dp - 1) < dec))
      s += "0";
   // too many dec pl?  take a substring
   var places = s.length - dp - 1;
   if (places > dec)
      if (dec == 0)
         s = s.substring( 0, dp );
      else
         s = s.substring( 0, dp + dec + 1 );
   // recover exponent, if any
   s += exp;
   // recover neg, if any
   if (neg > 0)
      s = "-" + s;
   // if not enough width, add spaces IN FRONT
   //    too many places?  tough!
   while (s.length < wid)
      s = " " + s;
   return s
   }
function Prb( x ) {
   if (x < 0)  x = 0;
   else
      if (x > 1)  x = 1;
   return x;
   }
function PosV( x ) {
   if (x < 0)  x = -x;
   return x;
   }

// FACTORIALS
function Fact( x ) {
   // x factorial
   var  t=1;
   while (x > 1)
      t *= x--;
   return t;
   }
function LnFact( x ) {
   // ln(x!) by Stirling's formula
   //   see Knuth I: 111
   if (x <= 1)  x = 1;
   if (x < 12)
      return Math.log( Fact(Math.round(x)) );
   else {
      var invx = 1 / x;
      var invx2 = invx * invx;
      var invx3 = invx2 * invx;
      var invx5 = invx3 * invx2;
      var invx7 = invx5 * invx2;
      var sum = ((x + 0.5) * Math.log(x)) - x;
      sum += Math.log(2*Math.PI) / 2;
      sum += (invx / 12) - (invx3 / 360);
      sum += (invx5 / 1260) - (invx7 / 1680);
      return sum;
      }
   }
// POISSON
function PoissonPD( u, k ) {
   // Peizer & Pratt 1968, JASA 63: 1416-1456
   var s = k + (1/2);
   var d1 = k + (2/3) - u;
   var d2 = d1 + 0.02/(k+1);
   var z = (1 + g(s/u)) / u;
   z = d2 * Math.sqrt(z);
   z = NormalP( z );
   return z;
   }
function PoissonTerm( u, k ) {
   // by logs
   return  Math.exp( (k * Math.log(u)) - u - LnFact(k) );
   }
function PoissonP( u, k ) {
   // term-by-term summation
   if (k >= 20) return  PoissonPD( u, k );
   else {
      var  sum = 0.0, j = 0;
      while (j <= k)
         sum += PoissonTerm( u, j++ );
      if (sum > 1)  sum = 1;
      return  sum;
      }
   }
function DoPoi( aform ) {
   var u = PosV(parseFloat(aform.u.value));
   aform.u.value = Fixed(u,10,4);
   var k = PosV(parseInt(aform.k.value));
   aform.k.value = Fixed(k,8,0);
   aform.tnk.value = Fixed(PoissonTerm( u, k ),8,4);
   var t = PoissonP( u, k );
   aform.puk.value = Fixed(t,8,4);
   aform.quk.value = Fixed(1-t,8,4);
   }
这是

非常通用的。 您将不得不根据需要对其进行修改。 但这将为您提供基本概念:

<form name="thisform" action="phpPage.php" method="POST">
X: <input type="text" name="val_x" id="val_x" value="40" /><br />
Y: <input type="text" name="val_y" id="val_y" value="60" /><br />
<input type="button" onclick="sendForm();" value="send form"/>
</form>

JavaScript:

function sendForm(){
  //Choose one of these methods:        
  //This will generate a string that you can use as a location.
  //use $_GET in PHP to retrieve the values
  var valofX = document.getElementById("val_x").value;
    var valofy = document.getElementById("val_y").value;
    generateURL = 'phpPage.php?val_x=' + valofX;
    generateURL += '&val_y=' + valofy;
    document.location = generateURL;

    //This will submit the form.
  //use $_POST in PHP to retrieve the values
    document.getElementById("thisform").submit();
}

提交表单或发送位置后,您需要在 PHP 中获取值:

$val_x = $_POST['val_x'];
$val_y = $_POST['val_y'];
//OR
$val_x = $_GET['val_x'];
$val_y = $_GET['val_y'];

您将使用 $_GET 或 $_POST,具体取决于值的发送方式。