简体   繁体   English

避免使用php重新加载页面或重置选择下拉列表

[英]avoid reloading page or resetting select dropdowns with php

I'm new to PHP programming and working with backend. 我是PHP编程和后端处理的新手。 I know what I am doing, but having trouble finding a proper solution online which matches my case. 我知道自己在做什么,但是在网上找不到适合我情况的合适解决方案时遇到了麻烦。
Basically, I have a form (2 text fields, 3 dropdowns) that filters the data that I am getting from SQL Server (that works). 基本上,我有一个表单(2个文本字段,3个下拉列表),用于过滤从SQL Server(有效)获取的数据。 However, when I press the "filter" button and I get the desired results, the page reloads and resets the form. 但是,当我按下“过滤器”按钮并获得所需的结果时,页面将重新加载并重置表格。 Now I have the filtered records but I don't know what filters I used to get that. 现在,我有了过滤的记录,但是我不知道我曾经使用过什么过滤器。
I could insert a <?php ... ?> if else, but the thing is I already am using php to get the dropdown values. 如果可以的话,我可以插入<?php ... ?> ,但是问题是我已经在使用php来获取下拉列表值。 Don't think I can put php inside php. 不要以为我可以将php放到php中。 By including if-else I mean this: 通过包括if-else,我的意思是:
echo '<option value="' . $row['status'] . '"' . if($prod_status == $row['status']): echo ' selected="selected"' . '>' . $row['status'] . '</option>';
Here's the code: 这是代码:

<label for="prod_status_filter">Prod Status</label>
<select class="form-control" id="prod_status_filter" name="prod_status_filter">
<?php
    $query_status = 'select DISTINCT status FROM analytics.laborrecords ORDER BY status ASC';
    $result = sqlsrv_query($conn, $query_status);
    while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
    {
        echo '<option value="' . $row['status'] . '">' . $row['status'] . '</option>';
    }
?>
</select>

This is one of the dropdowns. 这是下拉菜单之一。

I want: 我想要:
1. Either stop the page from reloading so the filter dropdowns remain the same. 1.停止重新加载页面,以使过滤器下拉列表保持不变。 (AJAX won't work, I have 3 dropdowns with a lot of options and it's doesn't seem like a good practice to make so many getElementById lines) (AJAX无法正常工作,我有3个下拉菜单,其中包含很多选项,制作很多getElementById行似乎不是一种好习惯)
2. Let the page reload but keep the values as I set until I want to reset them, with a "reset" button. 2.让页面重新加载,但是使用“重置”按钮将值保留为我设置的值,直到要重置它们为止。

You have to populate the form using the submitted form data. 您必须使用提交的表单数据填充表单。 It should be either in $_POST or $_GET . 它应该在$_POST$_GET

Check if the value of $_POST['prod_status_filter'] matches the value in $row['status'] and if it does, echo selected. 检查$_POST['prod_status_filter']的值是否与$row['status']中的值匹配,如果匹配,则选择回显。

Be sure to sanitize the form data. 确保清理表单数据。 Never trust user input. 永远不要相信用户输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM