简体   繁体   English

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

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

I am building a website that will identify parts based on 10 different measurements. 我正在建立一个网站,该网站将基于10种不同的测量来识别零件。 I am wanting my my onchange event from my first drop down box to do two things. 我希望我的第一个下拉框中的onchange事件能够做两件事。 First, I need it to post my selection to a php variable on the next page. 首先,我需要将其选择发布到下一页的php变量中。 second, I want the function to load the next page which will give me another drop down list that only shows the options that also have the same measurement as the first list. 第二,我希望函数加载下一页,这将给我另一个下拉列表,该下拉列表仅显示与第一个列表具有相同度量的选项。 I am basically building 10 pages that just keep adding on to my sql statement that generates my drop down list. 我基本上是在构建10个页面,这些页面只会不断添加到生成下拉列表的sql语句中。 I am just not sure how to send the jquery post to a php variable, and also how to load a new page. 我只是不确定如何将jquery帖子发送到php变量,以及如何加载新页面。 I am new to programming, so I am trying to keep this not too complicated. 我是编程的新手,所以我试图使其保持不太复杂。 here is the basics of my code. 这是我代码的基础。

<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>

this is basically what the rest of the pages are 这基本上就是其余页面

<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>

I think what you are looking for is 我想你要找的是

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

assuming your select is inside a form as <form id="form1" action="secondPage.php" method="post"> 假设您的选择位于表单内,例如<form id="form1" action="secondPage.php" method="post">

and in the second page you catch the value using $_POST['selectName'] 然后在第二页中使用$_POST['selectName']捕获值

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

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