【发布时间】:2015-04-08 09:50:50
【问题描述】:
我有一个通过单击按钮运行 MySQL 查询的页面。查询结果显示大约 6 列左右,大约 100 行。
我希望用户能够根据单击列标题来处理查询结果。几乎将它们发送到具有相同查询的同一页面,只是采取了。
这就是我目前所拥有的,都在同一个脚本中。
HTML 表格(在 PHP 中回显)
echo "<th id='frame_data_table'><a onClick='innerChange()' href='#'>Command</a></th>";
Javascript 函数
function innerChange() {
document.getElementById("innerTest").value = "Changed it.";
location.reload(true);
}
HTML 输入标签和 php 表单处理
<?php echo "-->" . $_POST['commandSort'] . "<--"; ?>
<form method="post">
<input type="text" id="innerTest" name="commandSort" value="command" />
</form>
当我这样做时,当页面重新加载时,echo $_POST['commandSort'] 没有任何显示。既不显示原始值,也不显示更改后的值。
真的,我只需要$_POST['commandSort'] 来获取更改后的输入框的值,然后我可以实现将我想要的任何值放入我的查询中,以便按照我的意愿对其进行排序。
提前致谢!
-安东尼
================================================ ====================
更新:解决方案
使用document.getElementById['mainForm'].submit() 解决这个问题(归功于我选择作为答案的一位用户)。这是我所拥有的。
HTML 表单
<html>
<!--...some html code here...-->
<form method="post" id="mainForm" action="example.com/results.php">
<!--...more code inside of the form here...-->
<input type="checkbox" name="command" value="command" id="command"/><p>Command</p>
<!--...more code inside of the form here...-->
<input type="hidden" name="hiddenSort" id="hiddenSort" />
<!--...more code inside of the form here...-->
</form>
<!--...some more html code here...-->
</html>
带有 Javascript 的 PHP example.com/results.php 页面
<?php
/*...more php code here...*/
<if (isset($_POST['command'])) {
?><th id="frame_data_table"><a href="javascript:sortFunction('command');">Command</a></th><?php
}
/*...more php code that displays the query result (while(...)) here...*/
?>
Javascript 函数
<script>
function sortFunction(x) {
var sortVar = x;
document.getElementById("hiddenSort").value = x;
document.getElementById("mainForm").submit();
}
</script>
解释:获取并显示初始查询结果后,如果用户单击“命令”列链接,document.getElementById['mainform'].submit() 会重新提交与之前运行的相同的表单,同时将变量传递给javascript函数x。该变量决定了查询的处理方式,因为真正需要做的就是在 PHP 中用"ORDER BY " . $_POST['commandSort'] 填充一个变量。如果您遇到类似问题,请随时给我发消息以获得进一步的解释。
感谢所有回复的人。
-安东尼
【问题讨论】:
-
难道您不需要提交表单而不是刷新页面吗?给表单一个 id 然后代替 location.reload 使用
document.getElementById("myForm").submit();
标签: javascript php html post reload