![](/img/trans.png)
[英]How to load a new php page in same window, using jQuery, and at the same time post data to it
[英]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.