从下拉菜单中进行选择后,我试图立即提交表单。提交表单后,我想基于下拉菜单中的选择向MySQL数据库发送查询,并显示检索到的文本。
目前,对于我下面的内容,没有任何显示,不会引发任何错误。 JS提交事件处理程序可以工作,但是在页面重新加载后,不会显示新文本。
非常感谢您的帮助。
提交表单的JS:
// Set all fetch requests as an object by default
$dbo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$admin = 'ADMIN';
$adminLive = 'ADMIN-live';
$adminType = 'ADMIN_US_Type';
$customusDomestic = 'US_domestic';
$adminChina = 'China_admin';
$adminIndia = 'India_admin';
$adminCustom = 'ADMIN_custom';
$customusaustinCulver = 'US_Austin_Culver';
$customuschinaTokyo = 'US_China_Tokyo';
$customcorkuaeBayarea = 'Cork_UAE_BayArea';
$customchinashanghaiBeijing = 'China_Shanghai_Beijing';
$adminselfMoves = 'ADMIN_selfmoves';
$adminmilanVienna = 'ADMIN-milan-vienna';
$custombayareaSeattle = 'Bayarea_Seattle_admin';
// Prepare the query
$sql = 'SELECT DISTINCT usergroup
FROM plus_signup
WHERE usergroup != ?
AND usergroup != ?
AND usergroup != ?
AND usergroup != ?
AND usergroup != ?
AND usergroup != ?
AND usergroup != ?
AND usergroup != ?
AND usergroup != ?
AND usergroup != ?
AND usergroup != ?
AND usergroup != ?
AND usergroup != ?
AND usergroup != ?';
$stmt = $dbo->prepare($sql);
// Execute the query
$stmt->execute([$admin,$adminLive,$adminType,$customusDomestic,$adminChina,$adminIndia,$adminCustom,$customusaustinCulver,$customuschinaTokyo,$customcorkuaeBayarea,$customchinashanghaiBeijing,$adminselfMoves,$adminmilanVienna,$custombayareaSeattle]);
$usergroups = $stmt->fetchAll();
foreach($usergroups as $locations) {
// This works fine but my output is adding an extra br in some occurances of the loop
echo $locations->usergroup . "<br/>";
}
提交表单后运行的PHP:
$(".platformSelectDropDown").change(function() {
$('.platformSelectForm').submit();
});
用于查询和返回数据的PHP函数:
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$platform = $_POST['platformSelectDropDown'];
$description = call_data($tableName, $platform)['Description'];
$application = call_data($tableName, $platform)['Application'];
}
表格:
function call_data($tableName, $col, $platformName) {
include('connection.php');
$sql = 'SELECT * FROM $tableName WHERE platform_name = $platformName';
try {
return $db->query($sql);
}
catch (Exception $e) {
echo "Error! " . $e->getMessage() . "<br/>";
return array();
}
}
答案 0 :(得分:2)
我相信下面的代码可以实现您想要的功能,并且在安全性和功能上有所改进。但是,请注意,从您的代码来看,尚不清楚设置$tableName
的位置,因此我只是将其硬编码为测试表。我将php和html混合在一起,因为它使我更容易解决问题,并且我认为它将使您更容易遵循我的解决方案。如果您愿意,没有理由可以将其拆分回去并实现php部分的功能,类似于您的原始方法。检查一下:
<html>
<body>
<form class="platformSelectForm" id="platformSelectForm" method="post">
<?php
// Get which dropdown option is selected, if any, so can keep selected on page reload
if(!isset($_POST['platformSelectDropDown'])) {
// Not postback, default to first option ("Select One")
$p0Select = ' selected';
$p1Select = '';
$p2Select = '';
} else {
// Is postback
// Set variables for query below
$tableName = 'tbl_platforms_1';
$platformName = $_POST['platformSelectDropDown'];
// set dropdown selection to whatever was select at form submission
if($platformName == 'Platform_1') {
$p1Select = ' selected';
} elseif ($platformName == 'Platform_2') {
$p2Select = ' selected';
} else {
$p0select = ' selected';
}
}
?>
<select name="platformSelectDropDown" class="platformSelectDropDown" onchange="document.getElementById('platformSelectForm').submit()">
<option value="Select_One"<?php echo $p0Select; ?>>Select One</option>
<option value="Platform_1"<?php echo $p1Select; ?>>Platform 1</option>
<option value="Platform_2"<?php echo $p2Select; ?>>Platform 2</option>
</select>
<?php
// If dropdown value is set and does not equal "Select_One"
if(isset($_POST['platformSelectDropDown'])&& $_POST['platformSelectDropDown'] != 'Select_One') {
?>
<ul class="indent">
<?php
try {
// Set database parameters
// Replace these values with appropriate values for your database
// (okay to use an include like you did originally)
$dbhost = 'your_database_host';
$dbname = 'your_database_name';
$dbuser = 'your_database_user';
$dbpass = 'your_database_user_password';
// Create PDO
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Prepare SQL statement and bind parameters
$stmt = $conn->prepare("SELECT * FROM $tableName WHERE platform_name = :platformName");
$stmt->bindValue(':platformName', $platformName, PDO::PARAM_STR);
// Execute statement and return results in an associative array (e.g., field_name -> value)
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Close Connection
$conn = null;
// For each row that was returned, output results
for ($i = 0; $i < count($results); $i++) {
echo '<li>' .$results[$i]['Description'] .'</li>';
echo '<li>' .$results[$i]['Application'] .'</li>';
}
} catch (Exception $e) {
echo '<li>Error! ' .$e->getMessage() . '</li>';
}
?>
</ul>
<?php
};
?>
</form>
</body>
</html>
我用来设置测试的代码:
DROP TABLE IF EXISTS tbl_platforms_1;
CREATE TABLE IF NOT EXISTS tbl_platforms_1 (
id int AUTO_INCREMENT NOT NULL,
platform_name varchar(20),
Description varchar(20),
Application varchar(20),
PRIMARY KEY (id)
);
INSERT INTO
tbl_platforms_1
(platform_name, Description, Application)
VALUES
('Platform_1', 'Description 1', 'Application 1'),
('Platform_2', 'Description 2', 'Application 2');
如果这可以解决您的问题,请记住标记为已回答,这样每个人都将知道您不再需要帮助(因此,我花了很多时间提出此解决方案:-),我将得到奖励。如果这样做不能解决您的问题,请提供尽可能多的详细信息,说明当前结果与您期望的结果有何不同,我将尝试对其进行修改以满足您的需求。谢谢!