...

View Full Version : Basic js-to-php conversion



mindlessLemming
06-11-2004, 02:56 PM
G'day :)
I've found myself flooded by new additions from a client merely a week before beta testing was meant to start ... Contracts,eh? Won't forget next time ;)
Anyway, there is a purpose to this, and that is this:

One of the features requested by the client is a js grants calculator that they use on their intranet. Leaving this as js just ain't gonna happen on my watch, but I'm knee deep in administration sections, access level permissions and other such mumbo. I fear that small things like this calculator might bite me in the butt on the last day and I'm asking if there is a kind soul out their who could take ten minutes of their time to convert this js function to php :o


function calculate(y, e, s){
var rate = [0.4, 0.2, 0.1, 0.075, 0.05];
if(y<3 && e>=20000){var grant = (e-15000)/2;}
else if(e>=20000){
var ge = (e-15000)/2;
var gs = s*rate[y-3];
if(ge<gs){var grant = ge;}
else{var grant = gs;}
}
else{var grant = 0; alert("Applicants need a minimum of $20,000 in eligible expenditure to be entitled to claim.");}

if(grant<0 || isNaN(grant)){grant=0; alert("Check your input and try again.");}
document.gui.grant.value=grant;
}

Thanks in advance if anyone decides to help me out :D:thumbsup:

Andrew.

carl_mcdade
06-11-2004, 03:45 PM
I need some test data to do a proper conversion. But try this:



<?php

function calculate($y, $e, $s){
$rate = array(0.4, 0.2, 0.1, 0.075, 0.05);

if($y<3 && $e>=20000){
$grant = ($e-15000)/2;
}
elseif($e>=20000){
$ge = ($e-15000)/2;
$gs = $s*$rate[$y-3];
if($ge<$gs){
$grant = $ge;
}
else{
$grant = $gs;
}
}
else{
$grant = 0; ?>
<script>alert("Applicants need a minimum of \$20\,000 in eligible expenditure to be entitled to claim.");</script>
<?php }

if($grant < 0 || $grant === false ){
$grant = 0; ?>
<script>alert("Check your input and try again.")</script>";
<?php }
return $grant;
}


print calculate(6,600,7000);
?>

mindlessLemming
06-11-2004, 03:52 PM
:thumbsup: :D :thumbsup:
That's perfect Carl, thankyou so much.
If you ever want a second brain to go over a crazy css bug or similar please feel free to PM me, I owe you one :)

I never cease to be impressed by the helpfullness and friendly attitude of this community.

carl_mcdade
06-11-2004, 03:58 PM
I forgot to escape a couple of slashes in the js so try the latest edit if you get a js error.

mindlessLemming
06-11-2004, 04:36 PM
That's cool, I'm going to replace those two script alerts with text printed to the screen. Just to remove all functionality from the hands of js.
Thanks again :thumbsup:

mindlessLemming
06-12-2004, 03:02 AM
I've got it tweaked now, but it's not returning any values :confused:
I think it might be my if statement that's freakin' it out:


<?php //check if already submitted, display result.
if (isset($_GET['exp'])) {
$y = $_GET['year'];
$e = $_GET['exp'];
$s = $_GET['sales'];
print "<p>".calculate($y, $e, $s)."</p>";
}
?>


And here's the whole script:



<?php
$selfPost = $_SERVER['PHP_SELF'];

function calculate($y, $e, $s){
$rate = array(0.4, 0.2, 0.1, 0.075, 0.05);
if($y<3 && $e>=20000){
$grant = ($e-15000)/2;
}
elseif($e>=20000){
$ge = ($e-15000)/2;
$gs = $s*$rate[$y-3];
if($ge<$gs){
$grant = $ge;
$ret = "You are elligible for up to \$".number_format($grant).".";
}
else{
$grant = $gs;
$ret = "You are elligible for up to \$".number_format($grant).".";
}
}
else{
$grant = 0;
$ret = "Applicants need a minimum of \$20\,000 in eligible expenditure to be entitled to claim.";
}
if($grant<0 ){
$grant=0;
$ret = "Check your input and try again";
}
return $ret;
}


//print calculate(6,60000,700000);
?>
<form action="<?php echo $selfPost; ?>" name="calc" method="get">
<fieldset>
<legend>EMDG Grant Calculator</legend>
<?php //check if already submitted, display result.
if (isset($_GET['exp'])) {
$y = $_GET['year'];
$e = $_GET['exp'];
$s = $_GET['sales'];
print "<p>".calculate($y, $e, $s)."</p>";
}
?>
<label for="exp">Total Eligible Expenditure:</label> <br />
<input type="text" name="exp" id="exp" /> <br />
<label for="sales">Total Export Sales:</label> <br />
<input type="text" name="sales" id="sa;es" /> <br />
<label for="year">Select Grant Year</label> <br />
<select name="year" id="year">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
</select>
<input type="submit" />
</fieldset>
</form>


Any ideas?

carl_mcdade
06-12-2004, 07:12 AM
Uhh, you for got the values in your HTML, php needs them. I think the javascript collected the text.



<select name="year" id="year">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>

mindlessLemming
06-12-2004, 07:14 AM
I was surprised that the original form didn't have values, although the query string still sends the correct number, so that isn't the problem...
Thanks anyway.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum