jquery加载新页面并从on change事件发布数据

时间:2013-07-01 03:15:53

标签: php javascript ajax variables

我正在建立一个网站,根据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>

1 个答案:

答案 0 :(得分:0)

我认为你在寻找的是

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

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

并在第二页中使用$_POST['selectName']

捕获值