北城已荒凉 2016-02-10 14:18 采纳率: 0%
浏览 26

POST Ajax请求

I'm working on an old project that wasn't developed by me at first. I need to make an Ajax request so that the values contained in the fields (more on that later) be sent to a php script which will then return their values into the correct td. Here is the JavaScript/jQuery code.

$(function () 
{   

    $('form').on('submit', function (e) 
    {
        e.preventDefault();
        $.ajax
        ({
            type: 'post',
            url: 'envoi_dispo_semaine.php',
            data: $('form').serialize(),
            success: function () 
            {
                alert('Le planning a été mis à jour.');
            }
        });
    });

    jQuery(document).ready(function date()
    {
        Date.prototype.getWeek = function() {
            var onejan = new Date(this.getFullYear(),0,1);
            var today = new Date(this.getFullYear(),this.getMonth(),this.getDate());
            var dayOfYear = ((today - onejan +1)/86400000);
            return Math.ceil(dayOfYear/7)
        };

        var today = new Date();
        var t = today.getWeek();
    })

    jQuery(document).ready(function()
    {

        jDispo  = {};
        jCharge = {};
        jSolde  = {};
        var d = 0;
        var c = 0;
        var s = 0;

        jQuery('.DISPO').each(function()
        {
            jDispo[d] = jQuery(this).val();
            d++;
        });

        jQuery(".CHARGE").change(function()
        {
            var totalCharge = 0;
            if(jQuery(".CHARGE").length > 0)
            {
                jQuery(".CHARGE").each(function() 
                {
                    jCharge[c] = jQuery(this).val();
                    c++;
                    totalCharge = totalCharge + jQuery(this).val(); 
                });

            }
            jQuery('.SOLDE').each(function()
            {
                jSolde[s] = jQuery(this).val();
                $.ajax(
                {
                    type:'post',
                    url:'check_charge.php',
                    data:{charge : jCharge[s],solde : jSolde[s],dispo : jDispo[s],action:"update_site"},
                    success: function() 
                    {
                        $('jSolde[s]').empty();
                        $('jSolde[s]').append();
                        $('.ajax').html($('.ajax input').val());
                        $('.ajax').removeClass('ajax');
                    }
                });
                s++;
            });
        });
    });

    $(document).ready(function()
    {
        if ($("#tab_projets table tbody tr:eq(2) td:contains('-')").length) 
        {
            $("#tab_projets table tbody tr:eq(2) td:contains('-')").css('background', '#CCFF00');
            $("#tab_projets table tbody tr:eq(2) td:contains('-')").css('font-color', 'black');
        }
        if ($("#tab_projets table tbody tr:eq(5) td:contains('-')").length) 
        {
            $("#tab_projets table tbody tr:eq(5) td:contains('-')").css('background', '#CCFF00');
            $("#tab_projets table tbody tr:eq(5) td:contains('-')").css('font-color', 'black');
        }
        if ($("#tab_projets table tbody tr:eq(8) td:contains('-')").length) 
        {
            $("#tab_projets table tbody tr:eq(8) td:contains('-')").css('background', '#CCFF00');
            $("#tab_projets table tbody tr:eq(8) td:contains('-')").css('font-color', 'black');
        }
    });
});

And here is check_charges.php:

<?php
   include('connexion_db.php');

   $charge   = $_POST['charge'];
   $dispo    = $_POST['dispo'];
   $solde    = $_POST['solde']; //I'll need this one later on.

$res = $dispo - $charge;
echo $res;
?>

I also have some php code that allows me to generate a table (it's in the same file as the javascript):

<thead>
            <?php
                echo "  <td colspan=2>Semaine n°</td>
                        <td>Retard</td>";
                for ($i=$numerosemaine; $i <= $numerosemaine + $longueurAff; $i++) 
                {
                    echo "<form action=\"envoi_dispo_semaine.php\" method=\"post\">
                            <td>
                                <input type=\"hidden\" name=\"semaine_id\" value=\"".$i."\" />".$i."</td>";
                }

            ?>
            </thead>
            <tbody>
            <?php 

                foreach($users as &$myUser)
                {
                        echo "  <tr class=".$myUser.">
                                    <td width=66% rowspan=3><input type=\"hidden\" name=\"login\" value=\"".$myUser."\" onblur=\"updateCharge\"/>".$myUser."</td>
                                    <td width=34%>Disponibilité</td>
                                    <td rowspan=3></td>
                            ";
                                    for ($i=$numerosemaine; $i <= $numerosemaine + $longueurAff; $i++) 
                                    {
                                        $req = "
                                                SELECT Nb_max_jours FROM Dispo_par_semaine WHERE login = '".$myUser."' AND semaine_id = ".$i;
                                        $query = requete_is_plancharges($req);
                                        $row = mysql_fetch_row($query);
                                        $affichageDispo = $row[0];
                                        if ($affichageDispo == "") 
                                        {
                                            $affichageDispo = 3;
                                        }
                                        echo "  
                                                <td>
                                                    <input class=\"DISPO\" type=\"number\" name=\"disponibilite[]\" value=".$affichageDispo." min=\"0\" max=\"5\" step=\"0.5\" class=\"input\"/>
                                                </td>
                                            ";
                                    }
                                    echo"
                                </tr>
                                <tr class=".$myUser.">
                                    <td width=34%>Charge</td>";
                                    for ($i=$numerosemaine; $i <= $numerosemaine + $longueurAff; $i++) 
                                    { 
                                        $reqTache = "
                                            SELECT tache_id 
                                            FROM Tache 
                                            WHERE ebi_id = ".$ebi."
                                             AND demande_id = ".$demande."
                                             AND action_id = ".$action;
                                        $resultatTache_id = requete_is_plancharges($reqTache);
                                        $maTache = mysql_fetch_object($resultatTache_id);

                                        $req_Charge = "
                                        SELECT COUNT(charge) as charge_tache
                                        FROM Charge_par_tache 
                                        WHERE tache_id   =".$maTache->tache_id.
                                        " AND semaine_id =".$i.
                                        " AND login      = '".$myUser."'";

                                        $resultat_requete_Charge = mysql_fetch_object(requete_is_plancharges($req_Charge));

                                        if ($resultat_requete_Charge->charge_tache > 0) 
                                        {
                                            $req = "
                                                SELECT Charge_par_tache.charge 
                                                FROM Charge_par_tache, Tache 
                                                WHERE   Charge_par_tache.tache_id = Tache.tache_id
                                                    AND Tache.ebi_id = ".$ebi." 
                                                    AND Tache.demande_id = ".$demande." 
                                                    AND Tache.action_id = ".$action."
                                                    AND Charge_par_tache.login = '".$myUser."' 
                                                    AND Charge_par_tache.semaine_id = ".$i;

                                            $Charge = mysql_fetch_object(requete_is_plancharges($req));



                                        } else
                                        {
                                            $Charge->charge = "";
                                        }
                                        echo "  <input type = \"hidden\" name = \"tache_id\" value=".$maTache->tache_id.">
                                                    <td class=\"CHARGE\">";
                                                        $query = requete_is_plancharges($req);
                                                        $row = mysql_fetch_array($query);
                                                        $affichageCharge = $row[0];
                                                        echo " <input class=\"CHARGE\" type=\"number\" name=\"charge[]\" value=".$Charge->charge." min=\"0\" step=\"0.5\"/>
                                                    </td>";
                                    }
                                    echo"
                                </tr>
                                <tr class=".$myUser.">
                                    <td width=34%>Solde</td>";
                                    for ($i=$numerosemaine; $i <= $numerosemaine + $longueurAff; $i++) 
                                    {
                                        $req1 = "
                                                SELECT charge FROM Charge_par_tache WHERE login = '".$myUser."' AND semaine_id = ".$i;
                                        $req2 = "
                                                SELECT Nb_max_jours FROM Dispo_par_semaine WHERE login = '".$myUser."' AND semaine_id = ".$i;
                                        $query1 = requete_is_plancharges($req1);
                                        $row1 = mysql_fetch_row($query1);
                                        $query2 = requete_is_plancharges($req2);
                                        $row2 = mysql_fetch_row($query2);
                                        $solde=$row2[0]-$row1[0];
                                        echo "<td class=\"SOLDE\"><input type=\"hidden\" class=\"SOLDE\" value=".$solde."/> ".$solde."</td>";
                                    }
                                        ?>
                                </tr>
                                        <?php
                }
            ?>

            </tbody>
        </table>
            <p><input type="submit" name="submit" value="Mise à jour"></p>
        </form> 

The problem is that I can't seem to retrieve $res. I'm just starting Ajax so I really don't know what to do, and couldn't find the answer on the Internet as I use a js array to store my values.

  • 写回答

1条回答 默认 最新

  • weixin_33674976 2016-02-10 15:59
    关注

    If I understand your problem you want to get the response value of "check_charges.php", that it is the $res value, isn't it? The value will be returned in the first parameter of success function of your ajax.

    Your code:

    jQuery('.SOLDE').each(function()
    {
        jSolde[s] = jQuery(this).val();
        $.ajax(
        {
            type:'post',
            url:'check_charge.php',
            data:{charge : jCharge[s],solde : jSolde[s],dispo : jDispo[s],action:"update_site"},
            success: function(data) 
            {
                // Store where you want the data value
                alert('res value: ' + data);
                $('jSolde[s]').empty();
                $('jSolde[s]').append();
                $('.ajax').html($('.ajax input').val());
                $('.ajax').removeClass('ajax');
            }
        });
        s++;
    });
    

    I hope I have helped you.

    评论

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 python进程启动打包问题
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题