结合多个变量 - 使用POST

时间:2015-03-24 20:06:51

标签: javascript php jquery mysql ajax

我有以下函数,每个函数都会在url中添加一个变量,但问题是除非我将这两个变量合并到url中,否则它将无法正常工作。

我希望这两个帖子都作为变量添加到url中,而不是像当前那样添加到其中一个。

JS:

 <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:

 <?php
 $person = '';
        $person  = isset($_POST['person']) ? $_POST['person'] : '';
        $allIds = '';
        if($person!=''){
            foreach($person as $personid){
                $allIds .= $personid.',';
            }
            $personid = rtrim($allIds,',');
        }


 $person2 = '';
        $person2  = isset($_POST['person2']) ? $_POST['person2'] : '';
        $allIds2 = '';
        if($person2!=''){
            foreach($person2 as $personid2){
                $allIds2 .= $personid2.',';
            }
            $personid2 = rtrim($allIds2,',');
        }


include("includes/db.php"); 



global $con;


$sql= "SELECT * FROM courses WHERE (course_duration IN ('" . $personid . "')) OR (course_duration IN ('" . $personid2. "')) LIMIT 10";
$result = mysqli_query($con,$sql);
$sqlCount = "SELECT * FROM courses WHERE (course_duration IN ('" . $personid . "')) OR (course_subc1 IN ('" . $personid2. "')) OR (course_subc3 IN ('" . $personid2. "')) OR (course_subc1 IN ('" . $personid2. "')) OR (course_subc4 IN ('" . $personid2. "')) OR (course_subc5 IN ('" . $personid2. "'))";
$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);
?>

1 个答案:

答案 0 :(得分:0)

这是我的意思的一个例子:

JS:

<script>
        //Combine the two forms into one string of data
        //to send one ajax call instead of two
        var selectedPeople = "";
        function showUsers() {
            selectedPeople += $('#testform').serialize();
            selectedPeople += $('#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:selectedPeople,
                success:function(res){
                    $('#txtHint').html(res);
                }
            });
        }
    </script>

PHP:

 <?php
    //Globals and includes should go near the top
    //Look up PSR coding standards for php for good formatting
    include("includes/db.php"); 
    global $con;

    $person = '';
    $person  = isset($_POST['person']) ? $_POST['person'] : '';
    $allIds = '';
    if ($person!='') {
        foreach($person as $personid)
        {
            $allIds .= $personid.',';
        }
        $personid = rtrim($allIds,',');
    }


    $person2 = '';
    $person2  = isset($_POST['person2']) ? $_POST['person2'] : '';
    $allIds2 = '';
    if ($person2!='') {
        foreach($person2 as $personid2)
        {
            $allIds2 .= $personid2.',';
        }
        $personid2 = rtrim($allIds2,',');
    }

    //Okay to put strings on multiple lines for better reading
    $sql= "
        SELECT * FROM courses 
        WHERE (course_duration IN ('" . $personid . "')) 
        OR (course_duration IN ('" . $personid2. "')) 
        LIMIT 10";
    $result = mysqli_query($con,$sql);
    $sqlCount = "
        SELECT * FROM courses 
        WHERE (course_duration IN ('" . $personid . "')) 
        OR (course_subc1 IN ('" . $personid2. "')) 
        OR (course_subc3 IN ('" . $personid2. "')) 
        OR (course_subc1 IN ('" . $personid2. "')) 
        OR (course_subc4 IN ('" . $personid2. "')) 
        OR (course_subc5 IN ('" . $personid2. "'))";
    $get_crs_count = mysqli_query($con, $sqlCount);
    $count_rows = mysqli_num_rows($get_crs_count);
    //I use multiple lines for php html strings too 
    //so I don't get confused
    //Keep up using single quotes for html strings, good habit!
    echo '
        <p style="margin-bottom: 8px;margin-top: 8px;font-size:18px;">
            <b>Number of courses available: </b>' . $count_rows . '
        </p>';
    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'];
        //Being nit-picky, but you said you were novice so thought I'd help
        //<center></center> isn't used much anymore, use CSS to align
        // 'src="$variable"' works, but src="' . $variable . '" is much more reliable, and it's best to be consistent
        //Don't use <i></i> tags either, use <span>
        //Use CSS font-weight:bold; instead of <b></b> tags
        //Try to avoid inline-styling ( style="color:white;" )
        //Sometimes inline-styling is necessary, but use classes/ids if possible
        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);

如果一切都有道理,请告诉我。很抱歉在评论中含糊不清。不知道你的知识水平和我在工作大声笑。