0

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

<?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.

I then want to get the value of an item in the drop down list. without resubmitting the form.

4

2 回答 2

2

在后续请求将其发回给您之前,您不会从选择框中获得该值。

你需要有条件的逻辑说

  • 用户是第一次获得该页面吗?
    • 渲染选择
  • 用户是否将页面提交给我,并填写了表格?
    • 使用权$_POST
于 2013-06-12T19:39:23.177 回答
2

无论您如何生成下拉列表,在您创建整个页面,将其发送给用户,然后在脚本的不同化身中接收用户的响应之前,它都不会有值。

因此,您需要代码来创建页面,以及读取读者响应的代码。你两个都做吗?

于 2013-06-12T19:39:45.390 回答