duanleiliu7373 2015-11-05 16:13
浏览 44

如何从ajax中的<select>执行3个过滤器

im a fairly newbie into javascript and php (just got less than two weeks max)however im trying to learn because i really enjoy this,anyway, right now im trying to do a proyect for the company where i work for ( not that we will use it, but as an incentive).

the proyect is about making a query in sql that would return a table, but using the parameters given in three drop down's now those three 's must pick three conditionals. ie

 $sql ="SELECT * FROM catalogo WHERE escuelas='$e' and grado='$g' and seccion='$p'";

so far i have wrote ( and to be honest copy/paste a lot from different sources) the basics, but im stuck trying to a query.. anyway here is my code for the index.php

<script>
        function showescuelas(str) {
            if (str == "") {
                document.getElementById("txtHint").innerHTML = "";
                return;
            } else { 
                if (window.XMLHttpRequest) {
                    // code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                } else {
                    // code for IE6, IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange = function() {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                    }
                }
                xmlhttp.open("GET","ajaxdatospersonales.php?e="+str, true);
               //xmlhttp.open("GET","ajaxdatospersonales.php?e="+document.getElementById("esc").value+"&gra="+document.getElementById("gra").value+"&sec="+document.getElementById("sec").value,,true);

                xmlhttp.send();
            }
        }
        </script>
        </header>
        <?php include_once("menu.php"); ?>
        <div class="container">
           <h1> Agregar apartado </h1>
        <form id="form" method="post" action="procesadordatospersonales.php" >


          <!-- down below is my first filter, it feeds from a table named "escuelas"-->
            <p> <label for="escuelas">Escuela:</label>
               <select name="escuelas"  id="esc" onchange="showescuelas(this.value)">
                echo "<option>  </option>";
                <?php
                    $con = mysql_connect('localhost', 'root', 'pass') or die(mysql_error());
                    mysql_select_db('apartados');

                    $sql = "SELECT escuelas FROM escuelas";
                    $data = mysql_query($sql,$con);

                    while($category = mysql_fetch_array($data)){
                        $catname = $category['escuelas'];
                        $valueid = $category['escuelas'];
                      echo '<option value="'.$valueid.'">'.$catname.'</option>';
                    }
                ?>

                </select>

                <br />

            </p>





          <!-- down below is my second filter, it feeds from a table named "grados"-->

            <p> <label for="grado">grado:</label>
               <select name="grado"  id="gra" onChange="showgrados(this.value)">
                echo "<option>  </option>";
                <?php
                    $con = mysql_connect('localhost', 'root', 'pass') or die(mysql_error());
                    mysql_select_db('apartados');

                    $sql = "SELECT grado FROM grado";
                    $data = mysql_query($sql,$con);

                    while($category = mysql_fetch_array($data)){
                        $catname = $category['grado'];
                        $valueid = $category['grado'];
                      echo '<option value="'.$valueid.'">'.$catname.'</option>';
                    }
                ?>

                </select>

                <br />




            </p>
         <!-- yep you guess it this is the last  filter, it feeds from a table named "grados"-->

            <p> <label for="seccion" >seccion:</label>
               <select name="seccion" id="sec" onChange="showescuelas(this.value)">
                echo "<option>  </option>";
                <?php
                    $con = mysql_connect('localhost', 'root', 'pass') or die(mysql_error());
                    mysql_select_db('apartados');

                    $sql = "SELECT seccion FROM seccion";
                    $data = mysql_query($sql,$con);

                    while($category = mysql_fetch_array($data)){
                        $catname = $category['seccion'];
                        $valueid = $category['seccion'];
                      echo '<option value="'.$valueid.'">'.$catname.'</option>';
                    }
                ?>

                </select>

                <br />




            </p>

        </div>
        </div>

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

            <input type="submit" value="Agregar">
        </form>

and this is my "ajaxdatospersonales.php"

<?php
                $e=$_GET["e"];
                $g=$_GET["g"];
                $s=$_GET["s"];


                $con = mysqli_connect('localhost','root','pass','apartados');
                if (!$con)
                  {
                  die('Could not connect: ' . mysqli_error($con));
                  }
                mysqli_select_db($con);



                //i made this array just to check if the variables 
                $test= array("$e", "$g", "$s"); 
                echo "esta es la escuela " . $test[0] . " y este el grado " . $test[1] . " y la seccion " . $test[2] . ".";



                $sql ="SELECT * FROM catalogo WHERE escuelas='$e' and grado='$g' and seccion='$p'";


                $result = mysqli_query($con, $sql);
                $ii=1;
                echo "<table border='1'>
                <tr>
                <th>agregar</th>
                <th>isbn</th>
                <th>nombre</th>
                <th>autor</th>
                <th>editorial</th>
                </tr>";

                while($row = mysqli_fetch_array($result))
                  {
                  echo "<tr>";
                  echo "<td> <input type='checkbox' name= isbn".$ii." value=".$row['isbn']. " </td>";
                  echo "<td>" . $row['isbn'] . "</td>";
                  echo "<td>" . $row['nombre'] . "</td>";
                  echo "<td>" . $row['autor'] . "</td>";
                  echo "<td>" . $row['editorial'] . "</td>";
                  echo "</tr>";
                  $ii++;
                  }
                echo "</table>";

                mysqli_close($con);
                ?>

now for some reaso it just dosent work.... i dont know whats going on.. thanks.

now if you decide to help me please, try to be as newbie frendly as possible. also i already know about sql injection. but this is a proyect for fun

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行