Jump to content

Recommended Posts

im trying to convert a javascript code to php can GURUS help me and tell me am i on right track

 

below are the codes

 

JAVASCRIPT

<script language="JavaScript1.2">
 function calculatePrices(){
        var spotPrice = 1.0 * getValue("spotPrice");
        var callStrikePrice = 1.0 * getValue("callStrikePrice");
        var callRate = 1.0 * getValue("callRate");
        var putStrikePrice = 1.0 * getValue("putStrikePrice");
        var putRate = 1.0 * getValue("putRate");

        var actualCallRate = Math.ceil(100.0 * ((((callStrikePrice - spotPrice) - (callStrikePrice - (callStrikePrice+putStrikePrice)/2.0))/(callStrikePrice - spotPrice)) + 1) * callRate);
        var actualPutRate = Math.ceil(100.0 * (1 - ((((callStrikePrice+putStrikePrice)/2.0 - putStrikePrice) - (spotPrice - putStrikePrice))/(spotPrice - putStrikePrice))) * putRate);

        var buyPercent = Math.ceil(10000.0 * actualCallRate/(actualCallRate + actualPutRate));
        var sellPercent = Math.ceil(10000.0 * actualPutRate/(actualCallRate + actualPutRate));

        var result = "The chances of Nifty Future going up is " + buyPercent/100.0 + " % and going down is " + sellPercent/100.0 + " %";
        if(buyPercent==sellPercent){
            result = result + "<br/><h1 style='color:yellow'>TREND IS NEUTRAL</h1>";
        }else if(buyPercent>sellPercent){
            result = result + "<br/><h1 style='color:green'>TREND IS UP</h1>";
        }else{
            result = result + "<br/><h1 style='color:red'>TREND IS DOWN</h1>";
        }
        document.getElementById("result").innerHTML = result;
    }

    function populateCallPutStrikePrice(){
        var spotPrice = 1.0 * getValue("spotPrice");
        if(getValue("spotPrice")!=null && getValue("spotPrice")!=""){
            document.getElementById("userInput").style.display="block";
        }else{
            document.getElementById("userInput").style.display="none";
        }
        if(spotPrice%100>=0 && spotPrice%100<40){
            setValue("callStrikePrice",Math.floor(spotPrice/100)*100 + 100 + "");
            setValue("putStrikePrice",Math.floor(spotPrice/100)*100 - 100 + "");
        }else if(spotPrice%100 >=40 && spotPrice%100<=60){
            setValue("callStrikePrice",Math.floor(spotPrice/100)*100 + 100 + "");
            setValue("putStrikePrice",Math.floor(spotPrice/100)*100 + "");
        }else{
            setValue("callStrikePrice",Math.floor(spotPrice/100)*100 + 200 + "");
            setValue("putStrikePrice",Math.floor(spotPrice/100)*100 + "");
        }
    }

    function getValue(id){
        return document.getElementById(id).value;
    }

    function setValue(id,value){
        document.getElementById(id).value = value;
    }
 </script>

MY CONVERSION TO PHP OF THE ABOVE JAVASCRIPT

<?php
function CalculatePrices()
{
$spotPrice=1.0* $_POST['spotPrice'];
$callStrikePrice=1.0* $_POST['spotPrice'];
echo"$callStrikePrice";
$callRate=1.0* $_POST['spotPrice'];
$putStrikePrice=1.0* $_POST['spotPrice'];
$putRate=1.0* $_POST['spotPrice'];
$actualCallRate=ceil(100.0 * ((((callStrikePrice - spotPrice) - (callStrikePrice - (callStrikePrice+putStrikePrice)/2.0))/(callStrikePrice - spotPrice)) + 1) * callRate);
$actualPutRate=ceil(100.0 * (1 - ((((callStrikePrice+putStrikePrice)/2.0 - putStrikePrice) - (spotPrice - putStrikePrice))/(spotPrice - putStrikePrice))) * putRate);
$buyPercent =ceil(10000.0 * actualCallRate/(actualCallRate + actualPutRate));
$sellPercent = ceil(10000.0 * actualPutRate/(actualCallRate + actualPutRate));
$buyPer=$buyPercent/100.0;
$sellPer=$sellPercent/100.0;
echo"The chances of Nifty Future going up is $buyPer percent and going down is  $sellPer percent";

if(buyPercent==sellPercent){
            echo"TREND IS NEUTRAL";
        }else if(buyPercent>sellPercent){
            echo"<br/><h1 style='color:green'>TREND IS UP</h1>";
        }else{
            echo"<br/><h1 style='color:red'>TREND IS DOWN</h1>";
        }
        
}

IM STUCK WITH

 

document.getElementById("result").innerHTML = result; 

document.getElementById("userInput").style.display="block";

document.getElementById("userInput").style.display="none";

 

how do i tackle these in php

Link to comment
https://forums.phpfreaks.com/topic/281210-javascript-to-php/
Share on other sites

you cannot convert those to php, because those are DOM specific. php does have a DOM class that works similar, if you have a string or html file or xml file to work with, but this isn't really the same as grabbing or altering something from a live DOM on a page. Same thing with your getValue and setValue functions. You're going to have to refactor your code to leave those in javascript.

 

If you absolutely must convert it 100% to php, your only option is to have the full page reload whenever the code is executed, and have it output updated html code each time.

Link to comment
https://forums.phpfreaks.com/topic/281210-javascript-to-php/#findComment-1445198
Share on other sites

Well, here's my two cents. You would never rely upon JavaScript to do calculations of price because a user can override those calculations. But, it's fine to have something on the client side to provide the user a quick feedback on the price. But, you absolutely should have PHP code that will be used to determine the actual price to be used. So, it makes sense to me that you would have the same logic in PHP. However, your function is still relying upon other POST values to determine the price - which is a bad process and could result in users 'hacking' the data to reduce the price. The only information the form should need to pass is the id of the items in the cart and the quantities. You would then get the price, rates, etc. from server-side data/code.

 

But, the real question is what are you going to do with the price when the PHP page receives it. Do you want to display a page with the order information and a confirmation of the final price? If so, just pass the relevant values to your function and return the calculated price and output that price wherever you want on the page. There is no need to reference element by their ID, since your PHP code will be generating those elements and you can simply include the value as part of that output.

 

I'm also seeing some oddities in your code. For example, you are multiplying all the submitted values by 1? You should be using floatval() instead. Plus, you are using the same POST vaule for multiple variables - that seems to be a waste.

Link to comment
https://forums.phpfreaks.com/topic/281210-javascript-to-php/#findComment-1445202
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.