问题
I have a page that runs a MySQL query off of a button click. The query result displays about 6 or so columns with about 100 rows.
I would like the user to be able to resort the query results based on clicking the column headers. Pretty much sending them to the same page with the same query, just resorted.
So here is what I have so far, all in the same script.
HTML table (echoed in PHP)
echo "<th id='frame_data_table'><a onClick='innerChange()' href='#'>Command</a></th>";
Javascript function
function innerChange() {
document.getElementById("innerTest").value = "Changed it.";
location.reload(true);
}
HTML input tag and php form handling
<?php echo "-->" . $_POST['commandSort'] . "<--"; ?>
<form method="post">
<input type="text" id="innerTest" name="commandSort" value="command" />
</form>
When I do this, when the page reloads nothing shows up for echo $_POST['commandSort']
. Neither the original value nor the changed value is displayed.
Really, all I need is the $_POST['commandSort']
to grab the value of the changed input box, and then I can implement placing whatever value I want to into my query to order it however I want.
Thanks in advance!
-Anthony
==================================================================
UPDATE: Solution
The solution to this (credit goes to one of the users who I selected as the answer for) was by using document.getElementById['mainForm'].submit()
. Here is what I have.
HTML Form
<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>
PHP example.com/results.php Page with Javascript
<?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 Function
<script>
function sortFunction(x) {
var sortVar = x;
document.getElementById("hiddenSort").value = x;
document.getElementById("mainForm").submit();
}
</script>
Explanation: After the initial query result is fetched and displayed, if the user clicks on the "command" column link, the document.getElementById['mainform'].submit()
resubmits the same form as was previously run, while at the same time passing the variable through the javascript function x. The variable dictates how the query will be resorted, as all that really needs to be done is populate a variable with "ORDER BY " . $_POST['commandSort']
in PHP. Feel free to message me for a further explanation if you are encountering a similar problem.
Thanks for all who responded.
-Anthony
回答1:
$_POST['commandSort']
is null because you don't submit the form, try instead of
location.reload(true);
with
document.forms["form1"].submit();
renaming
<form method="post" id="form1">
.
回答2:
Are you sure you want to use JavaScript for this? It can be done with HTML and PHP.
eg.
<tr>
<th><a href="myfile.php?sort=blah1">Header Text 1</a></th>
<th><a href="myfile.php?sort=blah2">Header Text 2</a></th>
..
</tr>
then in PHP you can retrieve that value with:
$sort = $_GET['sort'];
回答3:
on click of button you can do:
window.location.href = window.location.href.replace( /[\?#].*|$/, "?q=mysort" );
and get sort option as $sort= $_GET['q'];
来源:https://stackoverflow.com/questions/28396518/passing-variable-php-javascript-location-reload-and-html-input