简体   繁体   English

如何获取通过echo创建的下拉列表的选定值?

[英]How can I get the selected value of a dropdown list created via echo?

I have been using the following to get the value of the selected item in a dropdown list on my pages. 我一直在使用以下方法在页面的下拉列表中获取所选项目的值。

 $field = $_POST['dropdownlistname'];

However when I create a dropdown list through echoing this doesn't work. 但是,当我通过回显创建下拉列表时,此操作将无效。 The following is my form. 以下是我的表格。

<form name="searchForm" action="newCustomerSearchform.php" method="post">
   <label><span></span> <input type="text" name="searchDB" /></label>
 <button type="submit" name="btnSearch" value="Search"  id="btnSearch" onclick="this.form.action">Search</button></label>

<?php 
   echo  '<select name="customers">';
     foreach ($_SESSION['names'] as $option => $value) {
                 echo  '<option value='.$value['ID'].'>'.$value['First_Name'].' '.$value['Surname'].'</option>';
}
                  echo  '</select>';


$test = $_POST['customers'];
echo $test;
</form>

Once the form is submitted the following query is run on newCustomerSearch.php 提交表单后,将在newCustomerSearch.php上运行以下查询

<?php
include 'newCustomer.php';
connect('final');


    $searchtext = $_POST['searchDB']; 
    $searchtext = htmlspecialchars($searchtext); // stop HTML charkacters
    $searchtext = mysql_real_escape_string($searchtext); //stop SQL injection
    $query = "SELECT * FROM customer WHERE First_Name LIKE '%$searchtext%'";
    $data = mysql_query($query) or die(mysql_error());


    $Customers = array();
    $colNames = array();
    while($row = mysql_fetch_assoc($data)){// puts data from database into array, loops until no more

            $Customers[] = $row;

        }
         $anymatches = mysql_num_rows($data); //checks if the querys returned any results
                if ($anymatches != 0) {
                     $_SESSION['names']=$Customers;
                                 $colNames = array_keys(reset($Customers));




    }

            if ($anymatches == 0) 
                    { 
                        echo "Sorry, but we can not find an entry to match your query<br><br>"; 
                    } 




header("location: newCustomer.php");
?>

The newCustomer page then reloads with the rendered drop down list contaning all of the rows returned by the query. 然后,newCustomer页面将重新加载渲染的下拉列表,其中包含查询返回的所有行。

I then want to get the value of an item in the drop down list. 然后,我想在下拉列表中获取项目的值。 without resubmitting the form. 无需重新提交表格。

You won't have the value from the select box until a subsequent request POSTs it back to you. 在后续请求将其回传给您之前,您将没有选择框中的值。

You need conditional logic that says 您需要条件逻辑说

  • Is the user getting the page for the first time? 用户是第一次获取页面吗?
    • Render the select 渲染选择
  • Is the user submitting the page back to me, with the form filled out? 用户是否在填写表格后将页面提交给我?
    • Access $_POST 存取$_POST

Regardless of how you generate the drop-down, it won't have a value until you create the entire page, send it to the user, and then receive the user's response in a different incarnation of your script. 无论您如何生成下拉菜单,在创建整个页面,将其发送给用户,然后以另一种形式化脚本接收用户的响应之前,下拉菜单都没有值。

So you need code to create the page, and code to read the reader's response. 因此,您需要代码来创建页面,并需要代码来阅读读者的回复。 Are you doing both? 你们都在做吗?

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

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