dongxizhe9755 2013-03-31 18:51
浏览 28
已采纳

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

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>
  • 写回答

1条回答 默认 最新

  • dongtiao0279 2013-04-01 02:11
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题