0

I have a java-script function that carries out a calculation. I would like to use the answer to that calculation in my php code.

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

Both values X and Y come from variables within my php code so

<?php
$valueofx;
$valueofy;
?>

In the ideal world I would like to to look like this

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

I know this can't be done and i have 5 different values i need to pull and use. Is there anyway I can work around it? I dont mind refreshing the page or grabbing it from another page as long as i can have 5 values to use in my php code.

I would need to run the javascript 10 times before redirecting like

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

below if the 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);
   }
7
  • 1
    You can use an AJAX call to send those values to a PHP page, have that page process and do whatever, and JavaScript will intercept the result. Commented Feb 4, 2016 at 14:23
  • 1
    You can also sent the values to PHP by generating a new URL with those variables sent along as $_GET parameters. Or, you can create a form with those values and submit that to PHP as $_POST. Commented Feb 4, 2016 at 14:24
  • Or you can just rewrite the equivalent functions in php, if they dont actually rely on client interaction Commented Feb 4, 2016 at 14:26
  • @Steve- the math is complex and i have had to have someone write it up for me. Commented Feb 4, 2016 at 14:28
  • @durbnpoisn - would you be able to provide an example? - if you need to see javascript code i can add it Commented Feb 4, 2016 at 14:28

1 Answer 1

1

This is very generic. You're going to have to modify this to your needs. But this will give you the basic idea:

<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();
}

Once the form is submitted, or the location is sent, you'll need to grab the values in PHP:

$val_x = $_POST['val_x'];
$val_y = $_POST['val_y'];

//OR

$val_x = $_GET['val_x'];
$val_y = $_GET['val_y'];

You would use $_GET or $_POST depending on how the values are sent.

Sign up to request clarification or add additional context in comments.

7 Comments

Is there away to execute sendForm() withing the php script rather then a onclick function as i want to automate the script
sendForm() is JavaScript, not PHP. So no. But JavaScript can trigger it whenever you want. It's just a function call. I attached it to a submit button. But you can call it however you wish.
In the end, this is the simplest method of passing your JS variables, however they are generated, to a PHP page for processing.
Great - q last question if I triiger the script by sendForm(); outside of a form - what would i need to replace document.getElementById("thisform").submit(); with ot go to the generateURL
If you're going with the generated URL, you don't need to submit the form. Just use the line where it sets the location. Comment out that last line.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.