-1

I've written the conditions down in JavaScript and if it's a down arrow then I want to update the database whereas currently it's not falling into the javascript condition and updating the database at server level. Any help will be appreciated.

Code:

<script language="javascript">
    $(document).ready(function() {

        totalgames=<?= $totalgames ?>;

        //scoress = 0;
        previous_s=0;

        for(xx=totalgames; xx>=1; xx--){

            score_ele = 'scores'+xx;
            tdid_ele = 'tdid'+xx;
            var tdd = document.getElementById(tdid_ele);
            var scoress = document.getElementById(score_ele);

            if(previous_s == 0){
                tdd.innerHTML = "<img src='images/bullet.png'/>";
            }else{
                if(parseFloat(previous_s) > parseFloat(scoress.value)){
                    tdd.innerHTML = "<img src='images/arrow_up.png'/>";
                }else if(parseFloat(previous_s) < parseFloat(scoress.value)){
                    tdd.innerHTML = "<img src='images/arrow_down.png'/>";
                    <?php
                        //Selecting from table teams
                        $sql_sel_amnt = "Select * from teams where t_name='".$t_name."'";
                        $result_sel = $db1->getResult($sql_sel_amnt);
                        $row_sel = mysql_fetch_assoc($result_sel);

                        //Selecting from table profitnloss
                        $sql_pnl = "Select * from profitnloss where t_name='".$t_name."' and username='".$abc."'";
                        $result_pnl = $db1->getResult($sql_pnl);
                        $row_pnl = mysql_fetch_assoc($result_pnl);

                        $transact_money = $row_pnl['pnl_amount'];
                        $pnl_results = $row_pnl['pnl'];

                        $profit = 0;
                        $loss = 0;

                        $transact_money = explode("|", $transact_money);
                        $pnl_results = explode("|", $pnl_results);
                        for($i=0; $i<count($transact_money); $i++){
                            if($pnl_results[$i]=='P'){
                                $profit = $profit + $transact_money[$i];
                            }else{
                                $loss = $loss + $transact_money[$i];
                            }//end if
                        }//end for..
                        $money_results_total = $profit - $loss;

                        $pnl_date = date("d-m-Y H:i:s");
                        $pnl_amount = $row_sel['c_amount'];//total amount lost
                        $t_amount = $money_results_total + $row_pnl['t_amount'] + $pnl_amount;

                        $noofplayers = mysql_num_rows($result_sel)-1;//total no of players
                        $company_share = 17;//charity percentage
                        $company_share_amnt = $company_share*$pnl_amount/100;
                        $pnl_amount_remaining = $pnl_amount - $company_share_amnt;
                        $charity = substr($row_sel['charity'], 0, 2);//charity percentage
                        $charity_amount = $charity*$pnl_amount_remaining/100;

                        $sharing_amount = $pnl_amount-$charity_amount-$company_share_amnt;
                        $pnl_profit = round($sharing_amount/$noofplayers, 2);

                        echo "noofplayers=> ".$noofplayers.", company_share=> ".$company_share.", company_share_amnt=> ".$company_share_amnt.", charity=> ".$charity."%, charity_amount=> ".$charity_amount.", sharing_amount=> ".$sharing_amount.", pnl_profit=> ".$pnl_profit;

                        $sql_updt_loss = "UPDATE profitnloss SET game_date = '".$serial_date."', pnl_date = CONCAT(pnl_date, '$pnl_date|'), pnl_amount = CONCAT(pnl_amount, '$pnl_amount|'), pnl = CONCAT(pnl, 'Loss|'), t_amount='".$t_amount."' where username='".$abc."' and t_name='".$t_name."'";
                        //echo $updt_pnl;
                        //$result_loss = $db1->getResult($sql_updt_loss);

                        $sql_updt_profit = "UPDATE profitnloss SET pnl_date = CONCAT(pnl_date, '$pnl_date|'), pnl_amount = CONCAT(pnl_amount, '$pnl_profit|'), pnl = CONCAT(pnl, 'Profit|') where username not like'".$abc."' and t_name='".$t_name."'";
                        //echo $updt_pnl;
                        //$result_profit = $db1->getResult($sql_updt_profit);
                    ?>
                }else if(parseFloat(previous_s) == parseFloat(scoress.value)){
                    tdd.innerHTML = "<img src='images/bullet.png'/>";
                }//end if
            }//end if 0..
            previous_s = document.getElementById(score_ele).value;

        }//end for

    })//emd document ready..
</script>
3
  • 4
    you are confusing clientsided with serversided programming. Commented Mar 31, 2013 at 19:27
  • thats why i want to write this code in AJAX... but I'm beginner in AJAX so any help will be really appreciated. Commented Mar 31, 2013 at 19:58
  • you should send the browser js code, this js sends an ajax request to a php script in your server, then gets the response and process it Commented Mar 31, 2013 at 21:09

1 Answer 1

0

The way you should execute you PHP-code using ajax is by moving the server side code to a separate file which you call apon in the ajax-request, the url.

One example is using jQuery.

$.ajax({
    type: "POST",
    url: "some.php",
    data: { name: "John", location: "Boston" }
}).done(function( msg ) {
    alert( "Data Saved: " + msg );
});

This documentation is located here

You send the information needed to execute the PHP-code in the data. These are then accessed by $_POST or $_GET depending on which type you chose.

If you're not intending to use jQuery, you can look here: Getting Started with AJAX - Updating Form via PHP.

Here are some good practices using ajax

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

1 Comment

thanx alot Quinee and Evan, that really helped alot my code is working super fine. really appreciated

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.