0

hi I am really novice in programming and doing some PHP development to support a personal commitment.

I have written a javascript to do some calculations and return the answer. I want to access this value from my php and print it.

following are the code components.

<?php include 'dbconnection.php'; ?>
<?php

  $Kg=0.00;
 $Rate=0.00;  $MinusBags=0.00; $Amount = 0.00;
 $KgIntoRate=0.00;
//$Amount = $KgIntoRate - $MinusBags;



//execute the SQL query and return records
$result = mysql_query("SELECT EmployeeName FROM employees");
$result2 = mysql_query("SELECT SRate FROM scraprates where SYear=YEAR(CURRENT_DATE()) AND SMonth=MONTHNAME(CURRENT_DATE())");
$temp1 = mysql_fetch_array($result2);
$Rate = $temp1['SRate'];
//$Rate=mysql_fetch_array($result2);


//fetch the data from the database 
try{
while ($row = mysql_fetch_array($result)) {
    echo "<tr><td>".$row{'EmployeeName'}."</td>";
    echo "<td><input type='text' name='Kg' id='Kg' onChange='CalcKgIntoRate();'/></td>";
    echo "<td>".$Rate."</td>";


//  $KgIntoRate = $_GET['php_KgIntoRate'];
    //echo "<td>".$_GET['php_KgIntoRate']."</td>";
    echo "<td>".$KgIntoRate."</td>";

    echo "<td><input type='text' name='MinusBags'/></td>";
    echo "<td>".$Amount."</td></tr>";
}
}
catch(Exception $e)
{echo "error".$e;}
?>

<script type="text/javascript">
//$Kg= $('#Kg').val();
function CalcKgIntoRate(){
var Kg=document.getElementById('Kg').value;
var php_rate = "<?php echo $Rate; ?>";
var php_KgIntoRate = "<?php echo $KgIntoRate; ?>";
//document.write(Kg);
php_KgIntoRate=Kg*php_rate;
return php_KgIntoRate;
}
</script>

<?php
//function CalcKgIntoRate(){
//$KgIntoRate = $Kg * $Rate;
//echo "<td>".$KgIntoRate."</td>";
//}


//close the connection
mysql_close($dbhandle);
?>

what i want to do is this. Names and Rate are coming from two database tables. I want to calculate KgIntoRate based on the Kg's entered (on change event of text field) and show the value in the Kg * Rate field.

I read that I need to use Ajax. but don't know how to do it. Appreciate some help with the code.

5
  • 1
    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. Commented Jun 27, 2013 at 8:32
  • Do you want to stay on the same PHP page ? I mean do you mind if we get the values to the php variable in the next page where the JAVASCRIPT will post ? Commented Jun 27, 2013 at 8:33
  • if you want to stay on the same page we have to use AJAX ... other wise we can just create a form using JAVASCRIPT and post it to another PHP page where we can happily accept it into php Variable Commented Jun 27, 2013 at 8:34
  • Why don't you do the calculation in PHP? Or why you don't print the value in JS? Commented Jun 27, 2013 at 8:41
  • i need to stay on the same page.. let me explain a bit more. I am generating a table in php with the following columns. 1. Employee Name --> pulled from database table called 'employees' 2. Kg --> a text field to input the amount of Kg's 3. Rate for the month --> pulled from database table called 'scraprates' 4. Kg * Rate --> Here I want to multiply the Kg amount entered by the user with the rate and display in the column (there is no text box for this). I want this calculation to happen for the Onchange event of the Kg text box Commented Jun 27, 2013 at 10:24

2 Answers 2

1

I think in your example what your missing is just passing the value back to the Input field.

   function CalcKgIntoRate(){
var Kg=document.getElementById('Kg').value;
var php_rate = "<?php echo $Rate; ?>";
var php_KgIntoRate = "<?php echo $KgIntoRate; ?>";
//document.write(Kg);
php_KgIntoRate=Kg*php_rate;
//return php_KgIntoRate;

// here! instead of returning the value, set it as the value of your Input
document.getElementById('Kg').value = php_KgIntoRate;}

or you could simply make the calculations in php....

$result1 = $Rate * $KgIntoRate;
echo "<tr><td>".$row{'EmployeeName'}."</td>";
echo "<td><input type='text' name='Kg' id='Kg' value='".$result1."'/></td>";
echo "<td>".$Rate."</td>";
Sign up to request clarification or add additional context in comments.

4 Comments

let me explain a bit more. I am generating a table in php with the following columns. 1. Employee Name --> pulled from database table called 'employees' 2. Kg --> a text field to input the amount of Kg's 3. Rate for the month --> pulled from database table called 'scraprates' 4. Kg * Rate --> Here I want to multiply the Kg amount entered by the user with the rate and display in the column (there is no text box for this). I want this calculation to happen for the Onchange event of the Kg text box
haaa ok now i understand so you need to update the rate column, assign an Id to that column and then update its innerHTMl echo "<td id="rateColumn">".$Rate."</td>"; in the js funtion add this document.getElementById('rateColumn').innerHTML= php_KgIntoRate;
if you need to update any Db values then you would need some ajax...or submit a FORM
hmm.. i may need to save this table at the end.. I will think about that when i get to that point :-)
1

You can do it wihtout ajax if your calculation is handled in javascript:

Check if it helps you.

e.g. Changes are in PHP code:

$i = 0; // define outside the loop

echo "<td><input type='text' name='Kg' id='Kg".$i."' onChange='CalcKgIntoRate(".$i.");'/></td>";
echo "<td id='rate".$i."'>".$KgIntoRate."</td>";
echo "<td id='kg_into_rate".$i."'>".$Amount."</td></tr>";
$i++;

JS Code:

function CalcKgIntoRate(i) {
    var kg = document.getElementById('Kg'+i).value;
    var rate = document.getElementById('rate'+i).innerHTML;
    var kg_into_rate = document.getElementById('kg_into_rate'+i);

    // here is your calculation login
    // suppose you store the calculation in an another variable
    var calVal = parseFloat(kg*eval(rate));
    kg_into_rate.innerHTML = calVal;
}

2 Comments

thanks for the response.. but it didn't work.. let me explain a bit more. I am generating a table in php with the following columns. 1. Employee Name --> pulled from database table called 'employees' 2. Kg --> a text field to input the amount of Kg's 3. Rate for the month --> pulled from database table called 'scraprates' 4. Kg * Rate --> Here I want to multiply the Kg amount entered by the user with the rate and display in the column (there is no text box for this). I want this calculation to happen for the Onchange event of the Kg text box
code is updated. now check it will work. i have checked on my machine.

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.