使用下拉列表修改php变量以生成不同数量的SQL结果
问题描述:
I have a form that queries a SQL database with a stored procedure and generates TOP 100 results. I would like to put a dropdown box next to the search button to choose how many results to display.
I imagine doing this by running a different stored procedure for each amount (i.e. TOP 100, TOP 200, TOP 300 etc.) so I want to change the $query variable being executed by the 'Search' button to run each of these stored procs.
Can PHP do this or do I need javascript/AJAX?
Example of my code:
echo '<input class="btn btn-primary btn-lg active" type="submit" name="search" value="Search"> <br><br>';
$query = "EXEC dbo.sp_HeaderSearch100";
Any help would be greatly appreciated! Thanks
答
You can do this with the basic POST.
Your markup would look like:
<input type="text" name="amount" /> <!-- or select/option with the same name, etc. -->
<input type="submit" class="..." name="search" />
Then your backend would process it simply as:
// Use an array of available "top" procs
// Otherwise use a default (e.g. 100)
$top = in_array($_POST['amount'], array(100, 200, 300)) ? $_POST['amount'] : 100;
$query = "EXEC dbo.sp_HeaderSearch$top"