使用带有PHP,MYSQL和AJAX的复选框

时间:2015-03-24 14:37:47

标签: javascript php jquery mysql ajax

这基于以下示例:

http://www.w3schools.com/php/php_ajax_database.asp

区别在于:

  • 使用复选框而不是选择选项
  • 选择多个复选框会影响检查结果,如果选择2和3,则将填充2和3的结果
  • 现在尝试使用多个复选框 只有一个,但理想情况下我想要多个,所以只是 现在有两个而不是一个会很好。

以下是主页:

<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();

                $.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="100">One<br>
            <input onchange="showUser();" name="person[]" type="checkbox" value="200">Two<br>
            <input onchange="showUser();" name="person[]" type="checkbox" value="300">Three<br>
            <input onchange="showUser();" name="person[]" type="checkbox" value="400">Four<br>
            <input onchange="showUser();" name="person[]" type="checkbox" value="500">Five<br>
        </form>
        <br>
        <div id="txtHint"><b>Person info will be listed here...</b></div>

    </body>
</html>

以下是getuser:

<!DOCTYPE html>
<html>
    <head>
        <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }

        table, td, th {
            border: 1px solid black;
            padding: 5px;
        }

        th {text-align: left;}
        </style>
    </head>
    <body>

    <?php

        $person = '';
        $person = isset($_POST['person']) ? $_POST['person'] : '';
        $allIds = '';

        if($person != ''){
            foreach($person as $personid){
                $allIds .= $personid.',';
            }
            $personid = rtrim($allIds,',');
        }

        include("includes/db.php"); 

        global $con;

        $sql            = "SELECT * FROM courses WHERE course_duration IN ( = '" . $personid . "') LIMIT 10";
        $result         = mysqli_query($con,$sql);
        $sqlCount       = "SELECT * FROM courses WHERE course_duration IN ( = '" . $personid . "')";
        $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>  
                        <span class='clearfix borda'></span>

                </article>";
        }

        echo "</table>";

        mysqli_close($con);

    ?>

    </body>
</html>

我的问题在于以下几行:

$sql= "SELECT * FROM courses WHERE course_duration IN ( = '" . $personid . "') LIMIT 10";
$result = mysqli_query($con,$sql);
$sqlCount = "SELECT * FROM courses WHERE course_duration IN ( = '" . $personid . "')";
$get_crs_count = mysqli_query($con, $sqlCount);

我收到以下错误:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\test\getuser.php on line 42
Number of courses available: 
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\test\getuser.php on line 44

我正在努力解决它,我不知道问题的核心在哪里。我知道例如当我将行更改为:&#34; SELECT * FROM courses WHERE course_duration =&#39;&#34;。$ personid。&#34;&#39;&#34 ;;它的工作原理,但问题是我必须使用IN,否则我将无法组合多个复选框,如果我选择复选框2和3,只有最后一次单击的结果和只能选择一个的结果将显示

0 个答案:

没有答案