自动提交表单和MYSQL重新查询

时间:2013-03-15 12:02:12

标签: php jquery mysql ajax

我一直在使用此代码在我的网站上显示一些推荐。

到目前为止这是代码:

// Select testimonials from database 
$q = "SELECT testimonial, city_name, name, web_address, image_name, membership_type
        FROM testimonials 
        INNER JOIN city ON city.city_id = testimonials.city_id
        ORDER BY date_added DESC LIMIT $start, $display";

$r = mysqli_query($dbc, $q); 

if ( $records >= 1 ) {

    while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC)) {
        $testimonial = $row['testimonial'];
        //echo $testimonial;
        $mytestimonial = nl2br($testimonial);
        $city               = $row['city_name'];
        $name               = $row['name'];
        $url                = $row['web_address'];
        $imageName      = $row['image_name'];
        $type               = $row['membership_type'];

        echo '<div class="testimonial-row">
                    <div class="testimonial-image">
                        <img src="'.UPLOAD_DIR.$imageName.'" />
                    </div>
                    <div class="testimonial">
                        <h2>'.$name.'</h2>
                        <h3>';
                        if($type==1){
                            echo 'A teacher';
                        }elseif($type==2){
                            echo 'An Institute';
                        }elseif($type==3){
                            echo 'A Student';
                        }
                        echo " from <strong>$city</strong></h3>
                        <blockquote>$mytestimonial</blockquote>
                        <p class='user-url'><a href=''>$url</a></p>
                    </div>                  
                </div>";    
    }
} else {
    echo "There is no any testimonial to display at this time. Please try again later.";
}

运行上面的代码后,我可以显示所有推荐信。我有3种不同的推荐,目前在我的页面中一起显示所有类型的推荐。

现在我要使用一个选择框来解决方案,根据其类型过滤和显示它们。

这是我的选择框:

<select class="select" name="type">
    <option value="1">Tutor</option>
    <option value="2">Institute</option>
    <option value="3">Student</option>
</select>

从选择框中选择一个选项时,我的过滤应该通过选择所选类型的原始查询来进行。我得到了一个jquery show / hide函数的解决方案,但它与期望的结果不匹配。

注意:我无法使用此选择框的提交按钮。这就是为什么我在寻找Ajax或Jquery的解决方案。

谁能告诉我是否有可能?

谢谢。

2 个答案:

答案 0 :(得分:1)

这可能是解决方案...... 像这样传递你的价值:

                    <select name="jumpMenu" class="sort-by-select" id="jumpMenu" onchange="MM_jumpMenu('parent',this,0)">
                      <option value=""> Select type </option>
                      <option value ="yourpage?type=1">Tutor</option>
                      <option value ="yourpage?type=2">Institute</option>
                      <option value ="yourpage?type=3">Student</option>

                    </select>

 <script type="text/javascript">
function MM_jumpMenu(targ,selObj,restore) //v3.0
{ 
    eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
    if (restore) selObj.selectedIndex=0;
}
</script> 

之后获得这样的价值..

 if(isset($_GET['type']) && $_GET['type'] != "")
 {
$Q = $_GET['type'];
  }

将此值放在Query

 SELECT testimonial, city_name, name, web_address, image_name, membership_type
    FROM testimonials 
    INNER JOIN city ON city.city_id = testimonials.city_id where membership_type='$Q'
    ORDER BY date_added DESC LIMIT $start, $display

答案 1 :(得分:1)

以下是代码示例代码,可以让您了解我向您推荐的内容..

$(document).ready(function(){


 $('#type').change(function(){
   var data = $("#type").val();
$.ajax({

         url : "yourdomain/yourfilterpage.php",
         data: {'type':data},
         type : 'POST' ,
         success : function(data){ $('#divtoshowdata').html(data)}
     });
 })

})

 <select class="select" name="type" id='type'>
 <option value="1">Tutor</option>
<option value="2">Institute</option> 
<option value="3">Student</option>

yourfilterpage.php上的

获取过滤结果