duanjie3267 2012-06-27 15:44 采纳率: 0%
浏览 64
已采纳

在哪里放置第二个jQuery.ajax函数和isset()检查POST变量?

This is a page using a single jQuery.ajax posting to the same page and is working fine.

<?php 
if (!isset($_POST['varA'])) {
?>
<html>
    <head>
        <script type="text/javascript">
            $(document).ready(bingo);

            function bingo()
            {
                jQuery.ajax({
                    type: "POST",
                    data:  {varA: "123"},
                    success: function(data)
                    {
                        $("#test").html(data);
                    }
                });
            }
        </script>
    </head>
    <body>
        <span id="test"></span>
    </body>
</html>
<?php
}
else 
{
    print_r($_POST['varA']);
}
?>

Situation: I have another variable that I wish to set using another separate jQuery.ajax. However, my page content duplicated itself when I put them altogether like this.

Bad Example (depicting what I'm trying to do)

<?php 
    if (!isset($_POST['varA'])) {
    ?>
    <html>
        <head>
            <script type="text/javascript">
                $(document).ready(bingo);

                function bingo()
                {
                    jQuery.ajax({
                        type: "POST",
                        data:  {varA: "123"},
                        success: function(data)
                        {
                            $("#test").html(data);
                        }
                    });
                }

                function buttony() //added 2nd independent jQuery.ajax function
                {
                    jQuery.ajax({
                        type: "POST",
                        data: {varB: "456"},
                        success: function(data)
                        {
                            $("#test2").html(data);
                        }
                    });
                }
            </script>
        </head>
        <body>
            <span id="test"></span>
            <span id="test2"</span> //added 2nd span
            <input type="button" value="Clicky" onclick="buttony()"/> //added
        </body>
    </html>
    <?php
    }
    else 
    {
        print_r($_POST['varA']);
    }

    if(isset($_POST['varB'])) //added isset() checking
    {
        print_r($_POST['varB']);
        echo 'varB Okay';
    }
    else
    {
        echo 'varB Not';
    }
?>

Additional information: Posting of variables for both jQuery.ajax of "Bad Example" are working fine.


Note: Pictures below are not from the codes above, just an example of what I meant by "content duplicated itself".

Before

Before

After

After


Question: May I know where should I put my 2nd jQuery.ajax function and isset() checking of the POST variable, in such a way that both jQuery.ajax functions will work correctly and won't conflict with each other?

  • 写回答

2条回答 默认 最新

  • doutan1637 2012-06-28 17:19
    关注

    I used window.location to replace the use of 2nd jQuery.ajax to avoid complication.

    Sample example

    Inside JavaScript function

    window.location = "samePage.php?varB=abc";
    

    Checking of POST variable varB

    if(isset($_REQUEST['varB'])
    {
        $getValue = $_REQUEST['varB'];
    }
    

    I'm leaving the question open for the community to tackle on how to do proper nesting of using 2 or more jQuery.ajax in the same page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计