【问题标题】:combine two ajax call - storing multiple post variables in url结合两个 ajax 调用 - 在 url 中存储多个 post 变量
【发布时间】:2015-03-25 15:21:44
【问题描述】:

我想将这两个 ajax 调用结合起来,因为我希望能够将两个 post 变量存储在 url 中,因为在特定时间只能存储其中一个:

 <script>
            function showUser() {
                var selectedPerson = $('#testform').serialize();
                                                    document.getElementById("txtHint").innerHTML = "<div class='loading-indication'><img src='ajax-loader.gif' /> &nbsp; Please wait... Loading New Courses...</div>";


                $.ajax({
                    url:'getuser.php',
                    type:'post',
                    data:selectedPerson,
                    success:function(res){
                        $('#txtHint').html(res);
                    }
                })
            }
        </script>

        <script>
            function showUser2() {
                var selectedPerson = $('#testform2').serialize();
                                                    document.getElementById("txtHint").innerHTML = "<div class='loading-indication'><img src='ajax-loader.gif' /> &nbsp; Please wait... Loading New Courses...</div>";


                $.ajax({
                    url:'getuser.php',
                    type:'post',
                    data:selectedPerson,
                    success:function(res){
                        $('#txtHint').html(res);
                    }
                })
            }
        </script>

下面是php部分:

$person  = isset($_POST['person']) ? $_POST['person'] : '';
$allIds = '';
if($person!='')
{
    $allIds = implode(',', $person);
}


$person2  = isset($_POST['person2']) ? $_POST['person2'] : '';
$allIds2 = '';
if($person2!='')
{
    $allIds2 = implode(',', $person2);
}

如果需要任何说明,请告诉我

更新:

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script>
            function showUser() {
                var selectedPerson = $('#testform').serialize();
                                                    document.getElementById("txtHint").innerHTML = "<div class='loading-indication'><img src='ajax-loader.gif' /> &nbsp; Please wait... Loading New Courses...</div>";


                $.ajax({
                    url:'getuser.php',
                    type:'post',
                    data:selectedPerson,
                    success:function(res){
                        $('#txtHint').html(res);
                    }
                })
            }
        </script>

        <script>
            function showUser2() {
                var selectedPerson = $('#testform2').serialize();
                                                    document.getElementById("txtHint").innerHTML = "<div class='loading-indication'><img src='ajax-loader.gif' /> &nbsp; Please wait... Loading New Courses...</div>";


                $.ajax({
                    url:'getuser.php',
                    type:'post',
                    data:selectedPerson,
                    success:function(res){
                        $('#txtHint').html(res);
                    }
                })
            }
        </script>
    </head>
    <body>

        <form id="testform">
            <input onchange="showUser();" name="person[]" type="checkbox" value="1">One<br>
                        <input onchange="showUser();" name="person[]" type="checkbox" value="2">Two<br>
                                                <input onchange="showUser();" name="person[]" type="checkbox" value="3">Three<br>



        </form>

        <br>
        <div id="txtHint"><b>Person info will be listed here...</b></div>

    </body>
</html>

更新

PHP 完整代码

<!DOCTYPE html>
<html>
<head>

</head>
<body>

<?php
$person  = isset($_POST['person']) ? $_POST['person'] : '';
$allIds = '';
if($person!='')
{
    $allIds = implode(',', $person);
}


$person2  = isset($_POST['person2']) ? $_POST['person2'] : '';
$allIds2 = '';
if($person2!='')
{
    $allIds2 = implode(',', $person2);
}





include("includes/db.php"); 



global $con;


$sql= "SELECT * FROM courses WHERE (course_duration IN ('" . $allIds . "')) LIMIT 10";
$result = mysqli_query($con,$sql);
$sqlCount = "SELECT * FROM courses WHERE (course_duration IN ('" . $allIds . "'))";
$get_crs_count = mysqli_query($con, $sqlCount);
$count_rows = mysqli_num_rows($get_crs_count);
echo '<p style="margin-bottom: 8px;margin-top: 8px;font-size:18px;"><b>Number of courses available: </b>' . $count_rows ;
while($row_crs = mysqli_fetch_array($result)){ 

    $crs_id = $row_crs['course_id'];
        $crs_cat = $row_crs['course_cat'];

    $crs_provider = $row_crs['course_provider'];

    $crs_title = $row_crs['course_title'];
        $crs_price = $row_crs['course_price'];
          $crs_city= $row_crs['course_city'];
                      $crs_category= $row_crs['course_cat1'];

          $crs_date= $row_crs['course_date1'];


$crs_sdesc= $row_crs['course_sdesc'];
$crs_shortdesc = mb_strimwidth("$crs_sdesc",0,140,"...");
        $crs_image = $row_crs['course_image'];
        $provider_image = $row_crs['provider_image'];


  echo " <article class='search-result row'><center>
      <div class='col-xs-12 col-sm-12 col-md-3' id='thumbnailContainer'>
        <a href='#' title='Lorem ipsum' class='thumbnail' id='resultThumbnail'><img src='$provider_image' /></a>
     <a href='searchPage.php?crs_price=$crs_price' style='color:black;'>  <button id='resultprice'><span id='resultpriceText'>$ $crs_price</span></button></a>
      </div>
      <div class='col-xs-12 col-sm-12 col-md-2'>
        <ul class='meta-search' id='listDesign'>
        <a href='searchPage.php?crs_date=$crs_date' style='color:white;'>   <li><button id='resultInfo'><i class='fa fa-calendar fa-1x'><span id='iconText'>  $crs_date</span></i></button></li></a>
         <a href='searchPage.php?crs_category=$crs_category' style='color:white;'>   <li><button id='resultInfo2'><i class='fa fa fa-tags fa-1x'><span id='iconText'> $crs_category</span></i></button></li></a>
<a href='searchPage.php?crs_provider=$crs_provider' style='color:white;'><li><button id='resultInfo'><i class='fa fa-graduation-cap fa-1x'><span id='iconText'>  $crs_provider</span></i></button></li></a>
<a href='searchPage.php?city=$crs_city' style='color:white;'><li><button id='resultInfo'><i class='fa fa-map-marker fa-1x'><span id='iconText'> $crs_city</span></i></button></li></a>
        </ul>
      </div></center>
      <div class='col-xs-12 col-sm-12 col-md-7 excerpet'>
        <h3 id='resultHeading'><a href='coursePage.php?crs_id=$crs_id' id='headingLinking'><b>$crs_title</b></a></h3>

        <div id='courseshortDescription'>
$crs_shortdesc
 <center><a href='coursePage.php?crs_id=$crs_id' style='color:white;'><button class='btn btn-danger' id='findoutBtn'>Find Out More</button></a> </center>
        </div>  

</div>

      <span class='clearfix borda'></span>
    </article>";
}

mysqli_close($con);
?>
</body>
</html>

课程:

【问题讨论】:

  • 如何调用showUsershowUser2
  • 感谢您的回复。我在发布整个 html 页面的初始帖子下添加了一个更新,基本上我正在尝试实现这个w3schools.com/php/php_ajax_database.asp,但使用多个复选框而不是单个选择选项'
  • 那么您必须使用会话将第一次调用的参数存储在服务器端。然而,这将是一个非常脆弱的解决方案。你能不能把第一个请求的参数和你要发送的参数一起发送?
  • 您好,感谢您的询问。我知道它很脆弱,但我想使用会话,因为它可以让我更轻松地检索其他页面上的变量。我是这方面的新手,我将如何通过会话解决这个问题
  • 为了您的礼貌,我还发布了整个 php 页面

标签: php jquery html mysql ajax


【解决方案1】:

其实serialize()可以接受这两种形式:

        function showUser() {
            var datastring = $('#testform, #testform2').serialize();
            document.getElementById("txtHint").innerHTML = "<div class='loading-indication'><img src='ajax-loader.gif' /> &nbsp; Please wait... Loading New Courses...</div>";

            $.ajax({
                url: 'getuser.php',
                type: 'post',
                data: datastring,
                success: function (res) {
                    $('#txtHint').html(res);
                }
            })
        }

【讨论】:

  • 感谢您一如既往的高兴。所以我只使用表单名称会有所不同,但与每个输入关联的功能将保持不变。 php页面会有什么变化吗?
  • 我已经实施了你的建议,它有效,但我现在遇到的问题是我可以通过两个单独的帖子来控制变量,因为第一种形式是课程持续时间,第二种形式是用于子类别。现在似乎两种形式都引用同一个变量,在这种情况下,我如何知道哪个变量与哪个形式相关联,这在使用 sql 以显示结果时很有用 $sql= "SELECT * FROM courses WHERE (course_duration IN ('" . $allIds . "') OR course_SUBC1 IN ('" . ? . "')) LIMIT 10";
  • 您是想只向您的 php 页面发送两个输入还是来自两个单独表单的所有输入?
  • 那么,您的复选框应该提交表单吗?
  • 您是否赞同使用 php 和 mysql 执行此操作的想法,或者您是否愿意接受其他后端存储解决方案?
猜你喜欢
  • 2012-11-08
  • 2014-03-20
  • 2019-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多