繁体   English   中英

jQuery加载新页面并发布on change事件中的数据

[英]jquery to load a new page and post data from an on change event

我正在建立一个网站,该网站将基于10种不同的测量来识别零件。 我希望我的第一个下拉框中的onchange事件能够做两件事。 首先,我需要将其选择发布到下一页的php变量中。 第二,我希望函数加载下一页,这将给我另一个下拉列表,该下拉列表仅显示与第一个列表具有相同度量的选项。 我基本上是在构建10个页面,这些页面只会不断添加到生成下拉列表的sql语句中。 我只是不确定如何将jquery帖子发送到php变量,以及如何加载新页面。 我是编程的新手,所以我试图使其保持不太复杂。 这是我代码的基础。

<html>
    <head>
        <script type = "text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>    
        <script type='text/javascript'>
        function get() {
                var lengthdata = $('#filter').serialize();
                $.post('spline.php', lengthdata,
                function(output){
                        $('#list').html(output);
                        });
                }           
        </script>   
    </head>     
    <body>
        <div id="id1"></div>
        <?php
        //database login and connection.
        $dbhost = "localhost";
        $dbuser = "root";
        $dbpass = "password";
        $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("could not connect!");
        $select_db = mysql_select_db('camdb') or die('could not select camdb database!!');
        echo "<style type='text/css'>";
        echo "td {padding: 10px;}";
        echo "</style>";
        echo "<form name='filter' id='filter'><table><tr>";

        echo"<div id='lengthsel'>";
        $query = "SELECT DISTINCT Length FROM camTable;";
        $result = mysql_query($query);

        echo"<td>Cam Length" . "<br/>";
        echo"<select name=\"Length\" id='Length' onchange='get()'>/n";
        echo"<option value=''>Select</option>";
        while ($row = mysql_fetch_array($result)) {
            echo "<option value='" . $row['Length'] . "'>" . $row['Length'] . "</option>";
        }
        echo "</select></td>";
        echo"</tr></table></form>";
        echo"</div>";
        ?>
        <div id="list"></div>
    </body>
</html>

这基本上就是其余页面

<html>
    <head>
        <script type = "text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>    
        <script type='text/javascript'>
        function get() {
                var splinedata = $('#filter').serialize();
                $.post('spider.php', splinedata, 
                function(output){
                        $('#list').html(output);
                        });
                }           
        </script>   
    </head>     
    <body>
        <?php
        $dbhost = "localhost";
        $dbuser = "root";
        $dbpass = "password";
        $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("could not connect!");

        $select_db = mysql_select_db('camdb') or die('could not select camdb database!!');

        $sql = "SELECT * FROM camtable WHERE ";
        if ($_REQUEST['Length'] != "") {
            $sql.='length="' . mysql_real_escape_string($_REQUEST['Length']) . '";';
        }

        //$sql.="ORDER BY length, spline, spider, support, head, nose, grov1";
        echo $sql . "<br/>";

        $result = mysql_query($sql);

        $sql = "SELECT * FROM camtable WHERE ";
        if ($_REQUEST['Length'] != "") {
            $sql.='length="' . mysql_real_escape_string($_REQUEST['Length']) . '";';
        }
        $result = mysql_query($sql);

        echo"<td>Spline" . "<br/>";
        echo"<select name=\"spline\"id='spline' onchange='get()'>/n";
        echo"<option value=''>Select</option>";
        while ($row = mysql_fetch_array($result)) {
            echo "<option value='" . row['spline'] . "'>" . $row['spline'] . "</option>";
        }
        echo "</select></td>";
        ?>
    </body>
</html>

我想你要找的是

$(document).ready(function(){
    $('select').change(function(){ $('#form1')[0].submit();});
});

假设您的选择位于表单内,例如<form id="form1" action="secondPage.php" method="post">

然后在第二页中使用$_POST['selectName']捕获值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM