我应该如何使用AJAX执行我的PHP代码?

时间:2013-03-31 18:51:18

标签: php javascript ajax

我已经在JavaScript中编写了条件,如果它是向下箭头,那么我想更新数据库,而目前它不属于javascript条件并在服务器级别更新数据库。任何帮助将不胜感激。

代码:

<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>

1 个答案:

答案 0 :(得分:0)

你应该使用ajax执行PHP代码的方法是将服务器端代码移动到一个单独的文件中,你在ajax-request中调用apon,url。

一个例子是使用jQuery。

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

此文档位于here

您可以在数据中发送执行PHP代码所需的信息。然后,$_POST$_GET会根据您选择的类型访问这些内容。

如果您不打算使用jQuery,可以在这里查看:Getting Started with AJAX - Updating Form via PHP

Here are some good practices using ajax