Board index   FAQ   Search  
Register  Login
Board index php forum :: HTML JavaScript

PHP + Javascript Calculate & Save Each array text boxes

Javascript coding ..

Moderators: macek, egami, gesf

PHP + Javascript Calculate & Save Each array text boxes

Postby borgyborg » Tue Jan 24, 2012 8:15 pm

I want to calculate & save each array textboxes i created. my Codes are:

Code: Select all
<?php
        $sql="SELECT * FROM records WHERE is_active = 'Yes'";   
        $result=mysql_query($sql,$connection) or die(mysql_error());
        while ($row=mysql_fetch_array($result)) {
            $id = $row["id"];
            $default_salary = $row["default_salary"];
            $num_rows = mysql_num_rows($result);


    echo "<tr>";
    echo "<td>";

    echo "<input type=\"text\" class=\"validate[required,custom[number],maxSize[5],minSize[3]] text-input\" id=\"default_salary[$id]\" size=\"4\" name=\"default_salary[$id]\" value=\"$default_salary\" >";
    echo "</td>";
    echo "<td width=\"\" align=\"left\" border=\"0\">";
    echo "<input type=\"text\" class=\"validate[required,custom[number],maxSize[5],minSize[1]] text-input\" id=\"additional_salary[$id]\" size=\"4\" name=\"additional_salary[$id]\" value=\"0\">";
    echo "</td>";
    echo "</tr>";} ?>


         <INPUT TYPE="text" class="validate[required,custom[calculate],maxSize[10],minSize[1]] text-input" NAME="answer" id="answer" Value="" size="10" readonly="readonly">


                 <input type="button" name="calc" value="Calculate" onclick="return calculateSum();" />
                 <script>             


                 function calculateSum() {

                    var sum = 0;
                    $("input[type='text'][name='default_salary[]']").each(function(){

                        if(!isNaN(this.value) && this.value.length!=0) {
                            sum += parseFloat(this.value);
                        }

                    });
                    $("input[type='text'][name='additional_salary[]']").each(function(){

                        if(!isNaN(this.value) && this.value.length!=0) {
                            sum += parseFloat(this.value);
                        }

                    });
                    //.toFixed() method will roundoff the final sum to 2 decimal places
                    $("#answer").val(sum.toFixed());
                        }
                    </script>


As you can see, I have two different array text boxes. 1. default_salary[$id] 2. additional_salary[$id]

I also have this function calculateSum(). Which will calculate the default_salary[$id] & additional_salary[$id].

Now, my question is.

why if I remove the $id in the "default_salary[$id]" it calculates but doesn't save and if I retain the "$id" it doesn't calculate but it saves. What is wrong in my codes?
My insert to MySql Codes are below:

Code: Select all
if(isset($_POST['user'])){
    while(list($key, $val) = each($_POST['user'])){
          $default_salary = $_POST['default_salary'][$key];
          $additional_salary = $_POST['additional_salary'][$key];

                 $sql="insert into record
       (Fixed_Income_Amount, Variable_Income_Amount) values
       ('$default_salary','$additional_salary')";
       $result=mysql_query($sql,$connection) or die(mysql_error());
       }
     }


Please help. Thanks in advanced.
borgyborg
New php-forum User
New php-forum User
 
Posts: 5
Joined: Tue Jan 24, 2012 2:55 am

Re: PHP + Javascript Calculate & Save Each array text boxes

Postby borgyborg » Mon Jan 30, 2012 10:16 pm

Please help. :(
borgyborg
New php-forum User
New php-forum User
 
Posts: 5
Joined: Tue Jan 24, 2012 2:55 am


Return to JavaScript

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.