我在将从动态下拉列表中选择的变量传递给PHP文件时遇到问题。我希望PHP选择db表中与变量匹配的所有行。这是迄今为止的代码:
select.php
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("select#type").attr("disabled","disabled");
$("select#category").change(function(){
$("select#type").attr("disabled","disabled");
$("select#type").html("<option>wait...</option>"); var id = $("select#category option:selected").attr('value');
$.post("select_type.php", {id:id}, function(data){
$("select#type").removeAttr("disabled");
$("select#type").html(data);
});
});
$("form#select_form").submit(function(){
var cat = $("select#category option:selected").attr('value');
var type = $("select#type option:selected").attr('value');
if(cat>0 && type>0)
{
var result = $("select#type option:selected").html();
$("#result").html('your choice: '+result);
$.ajax({
type: 'POST',
url: 'display.php',
data: {'result': myval},
});
}
else
{
$("#result").html("you must choose two options!");
}
return false;
});
});
</script>
</head>
<body>
<?php include "select.class.php"; ?>
<form id="select_form">
Choose a category:<br />
<select id="category">
<?php echo $opt->ShowCategory(); ?>
</select>
<br /><br />
Choose a type:<br />
<select id="type">
<option value="0">choose...</option>
</select>
<br /><br />
<input type="submit" value="confirm" />
</form>
<div id="result"></div>
<?php include "display.php"; ?>
<div id="result2"></div>
</body>
</html>
这是我想要传递给变量的display.php。此文件将从db中选择条件,然后在select.php中打印结果。
<?php
class DisplayResults
{
protected $conn;
public function __construct()
{
$this->DbConnect();
}
protected function DbConnect()
{
include "db_config.php";
$this->conn = mysql_connect($host,$user,$password) OR die("Unable to connect to the database");
mysql_select_db($db,$this->conn) OR die("can not select the database $db");
return TRUE;
}
public function ShowResults()
{
$myval = $_POST['result'];
$sql = "SELECT * FROM specialities WHERE 'myval'=sp_name";
$res = mysql_query($sql,$this->conn);
echo "<table border='1'>";
echo "<tr><th>id</th><th>Code</th></tr>";
while($row = mysql_fetch_array($res))
{
while($row = mysql_fetch_array($result)){
echo "<tr><td>";
echo $row['sp_name'];
echo "</td><td>";
echo $row['sp_code'];
echo "</td></tr>";
}
echo "</table>";
//}
}
return $category;
}
}
$res = new DisplayResults();
?>
我真的很感激任何帮助。如果我能提供更多详细信息,请通知我。
链接到数据库图表:http://imgur.com/YZ0SuVw
第一个下拉列表来自专业表,第二个来自专业表。我想要做的是显示作业表中与下拉框中所选专业相匹配的所有行。这将需要将来自下拉列表的变量(结果)的结果转换为作业表中的spec_code。不确定如何做到这一点。谢谢!
答案 0 :(得分:0)
因为我们不知道它是在SQL查询中使用的类别或类型字段,所以无法正确回答您的问题。
下面是一些概念代码,它应该指向正确的方向。
<强> HTML / JS 强>
cat
和type
变量是否大于0时,您应首先将值解析为整数,因为默认情况下将帖子值作为文本发送。 myval
值无法破译,请说明您的意图。$("select#type")
)可以存储在var $type = $("select#type")
之类的变量中,然后像$type.attr('disabled', 'disabled')
一样引用。我不是100%肯定jQuery如何缓存结果,但理论上它应该需要更少的jQuery处理,它也会减少duplicated code。<强> PHP 强>
isset()
或!empty()
设置了超变量索引,否则将显示警告(或通知,无法调用)。因此,而不是$myvar = $_POST['myfield']
做$myvar = isset( $_POST['myfield'] ) ? $_POST['myfield'] : "";
sp_name
列是否属于类别或类型字段。我已将两者都包括在内。'myval'=sp_name
做sp_name='myval'
。showResults()
。showResults()
。另请注意,它会返回一个值,因此我们还必须将其打印到屏幕上。<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("select#type").attr("disabled","disabled");
$("select#category").change(function(){
$("select#type").attr("disabled","disabled");
$("select#type").html("<option>wait...</option>"); var id = $("select#category option:selected").attr('value');
$.post("select_type.php", {id:id}, function(data){
$("select#type").removeAttr("disabled");
$("select#type").html(data);
});
});
$("form#select_form").submit(function(){
// Fetch values and parse them to integers
var cat = parseInt( $("#category").val(), 10 );
var type = parseInt( $("#type").val(), 10 );
if(cat>0 && type>0)
{
$("#result").html('your choice: '+type);
// Prepare data
var data = {
category: cat,
type: type
}
$.ajax({
type: 'POST',
url: 'display.php',
data: data
});
}
else
{
$("#result").html("you must choose two options!");
}
return false;
});
});
</script>
</head>
<body>
<?php include "select.class.php"; ?>
<form id="select_form" action="">
Choose a category:<br />
<select id="category">
<?php echo $opt->ShowCategory(); ?>
</select>
<br /><br />
Choose a type:<br />
<select id="type">
<option value="0">choose...</option>
</select>
<br /><br />
<input type="submit" value="confirm" />
</form>
<div id="result"></div>
<?php include "display.php"; ?>
<div id="result2"></div>
</body>
</html>
<?php
class DisplayResults
{
protected $conn;
public function __construct()
{
$this->DbConnect();
}
protected function DbConnect()
{
include "db_config.php";
$this->conn = mysql_connect($host,$user,$password) OR die("Unable to connect to the database");
mysql_select_db($db,$this->conn) OR die("can not select the database $db");
return TRUE;
}
public function ShowResults()
{
// Fetch values from POST and escape/cast the values to prevent SQL injection
$category = (int) ( isset( $_POST['category'] ) ? $_POST['category'] : 0 );
$type = (int) ( isset( $_POST['type'] ) ? $_POST['type'] : 0 );
// Use the values in your SQL query
//
// PLEASE CHANGE THE COLUMN NAMES TO MATCH YOUR SOLUTION
//==================
$sql = "SELECT * FROM specialities WHERE category='". $category ."' and type='". $type ."' ";
$res = mysql_query($sql,$this->conn);
echo "<table border='1'>";
echo "<tr><th>id</th><th>Code</th></tr>";
while($row = mysql_fetch_array($res))
{
echo "<tr><td>";
echo $row['sp_name'];
echo "</td><td>";
echo $row['sp_code'];
echo "</td></tr>";
//}
}
echo "</table>";
return $category;
}
}
$res = new DisplayResults();
// Get and print results
echo $res->showResults();
?>