PHP / HTML提交后记住所选值

时间:2019-03-04 07:27:01

标签: php html mysqli

我知道对此有很多问题,但是我从这里尝试了很多,由于某种原因,它们对我没有用。

我从mysqli中填充列表。

from tkinter import *
import xlwt


# designing window for vmdetails

def Vmdetails():
    global vmdetails_screen
    for i in range(1,N+1):
        t=i
        numb=str(t)
        vmdetails_screen = Toplevel(numofvm_screen)
        vmdetails_screen.title("VM_Details_"+ numb)
        vmdetails_screen.geometry("400x950")

        global cluster_name
        global vm_template
        global clustername_entry



        # Set text variables
        cluster_name = StringVar()
        vm_template = StringVar()


        # Set label for user's instruction
        Label(vmdetails_screen, text="Please enter details below", bg="blue").pack()
        Label(vmdetails_screen, text="").pack()

        Cluster_Name_lable = Label(vmdetails_screen, text="Cluster_Name "+ numb +"* ")
        Cluster_Name_lable.pack()

        # Set textvariables entry

        clustername_entry = Entry(vmdetails_screen, textvariable=cluster_name)
        clustername_entry.pack()

        Button(vmdetails_screen, text="Submit", width=10, height=1, command=check).pack()


def check():
    # Retrieve the value from the entry and store it to a variable
    global var
    if cluster_name.get() == '':
        invalid_value()
    else:
     write_to_xls()

def invalid_value():
    global invalid_value_screen
    invalid_value_screen = Toplevel(vmdetails_screen)
    invalid_value_screen.title("Invalid Entry")
    invalid_value_screen.geometry("250x100")
    Label(invalid_value_screen, text="Enter valid values for all the required fields").pack()
    Button(invalid_value_screen, text="OK", command=delete_invalid_value).pack()


def delete_invalid_value():
    invalid_value_screen.destroy()


# designing window to provide the number of vm's to be created

def numofvm():

    global numofvm_screen
    numofvm_screen = Toplevel(main_screen)
    numofvm_screen.title("VM_NUM")
    numofvm_screen.geometry("300x250")

    global vmnumber
    global vmnumber_entry
    vmnumber = StringVar()

    Label(numofvm_screen, text="Please enter the Number of VM's you wishes to create ", bg="blue").pack()
    Label(numofvm_screen, text="").pack()

    vmnumber_lable = Label(numofvm_screen, text="Number of VM's to be created * ")
    vmnumber_lable.pack()

    vmnumber_entry = Entry(numofvm_screen, textvariable=vmnumber)
    vmnumber_entry.pack()

    Button(numofvm_screen, text="Submit", width=10, height=1, command=screen_duplicate).pack()


def screen_duplicate():
    global N
    N = int(vmnumber.get())
    if N > 0  :
      Vmdetails()

#exporting the user inputs into an excel sheet

def write_to_xls():
    # create new workbook
    wb = xlwt.Workbook()

    for i in range(1,N+1):
        t=i
        numb=str(t)
    # add sheet using given name
        ws = wb.add_sheet("VM_"+numb+"_DETAILS")

    # write text to cell
        ws.write(0, 0, "Cluster_Name")
        ws.write(1, 0,cluster_name.get())


    # save to given file name
    wb.save('my_file.xls')


def main_account_screen():
    global main_screen
    main_screen = Tk()
    main_screen.geometry("300x250")
    main_screen.title("Welcome")
    Label(text="Select Your Choice", bg="blue", width="300", height="2", font=("Calibri", 13)).pack()
    Label(text="").pack()
    Button(text="Enter", height="2", width="30", command=numofvm).pack()
    Label(text="").pack()
    main_screen.mainloop()


main_account_screen()

如何获取它,以便所选的对象在提交后保持选中状态?

3 个答案:

答案 0 :(得分:1)

获取已选择的值,并将其传递给$selected_product 并检查您要添加的每个选项。 这应该可以解决您的情况

   <select multiple="multiple" name="formCountries[]" size="5" id="keuzeproduct">
            <option value="keuze1" disabled selected="selected[]" multiple="multiple">Selecteer Product</option>
            <?php
                $selected_product = $your_value_here; //get the selected product from the database on load
                $is_selected  = "";
                $sql = "SELECT DISTINCT Id, Product FROM metingen group by Product order by Product";
                $resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
                   while( $rows = mysqli_fetch_assoc($resultset) ) {
                      if($selected_product == $rows["Product"]){
                         $is_selected = "selected";
                      }
                    ?>
            <option value="<?php echo $rows["Product"];?>" <?php echo $is_selected; ?>><?php echo $rows["Product"]; ?></option>
                  <?php } ?>
        </select>

答案 1 :(得分:1)

使用功能将选项生成包装起来:

function makeOption($value)
{
    echo "<option value=\"$value\"";
    if(isset($_POST['formCountries']) && in_array($value, $_POST['formCountries']))
        echo ' selected="selected"';
    echo ">$value</option>";
}

在while循环中使用它:

while( $rows = mysqli_fetch_assoc($resultset) )
    makeOption($rows["Product"]);

答案 2 :(得分:1)

对于选项脚本,您可以这样做:

<option value="<?php echo $rows["Product"];?>" <?=$rows['Product']=='//your val'?"selected":""?>><?php echo $rows["Product"]; ?></option>
                  <?php } ?>