-1

我已经用 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>
4

1 回答 1

0

您应该使用 ajax 执行 PHP 代码的方式是将服务器端代码移动到一个单独的文件中,您在 ajax 请求中调用 apon,即 url。

一个例子是使用 jQuery。

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

该文档位于此处

您发送在数据中执行 PHP 代码所需的信息。然后根据您选择的类型$_POST$_GET根据您选择的类型访问它们。

如果您不打算使用 jQuery,可以查看此处:AJAX 入门 - 通过 PHP 更新表单

下面是一些使用 ajax 的好习惯

于 2013-04-01T02:11:45.213 回答