Hi, can someone tell why or how I can perform a search using text from a drop down box -

I can search successfully using a textbox and bring back the correct search results -

But when I am using a dropdown box my search seems not to work -

what I am trying to do is search my products table by the given country name.

The problem is I think i am missing something out about the id's ?? but im not totally sure where I am going wrong.

I have a few searches like this one, so if I can get this working, I will be able to get the others working by following the same format.

Hoping someone can point me in the right direct or help me fix this -

here is the form i am using -

<form id="country" name="country" method="post" action="index.php">
   	     <select name="country" id="country" class="tb12" title="Search country">				
             <option value="England">England</option>
             <option value="USA">USA</option>    
          </select>  
            <input name="country" type="submit" id="country" class="tb11" value="Search country" title="Search by country" />
            <input type="hidden" name="country" value="by_country" />
        </form>

and here is my search

if ($_POST['country'] == "by_country") {
$queryString = "WHERE country LIKE '%$_POST[country]%' AND activated='1' ORDER BY id DESC";
    $queryMsg = "Displaying all products by $_POST[country]";

Dani AI

Generated

Short version: the form is sending the wrong value because multiple controls use the same name/id. and spotted the core problem — give each control a unique name (and unique id) so the server receives the selected country separately from any marker or control values.

Useful checks and quick debugging:

  • View the HTML (View Source) and confirm there are no duplicate id attributes; ids must be unique in the DOM.
  • Dump the incoming POST payload (for example with print_r($_POST) or var_dump($_POST)) to see exactly which keys and values arrive when you submit the form.
  • If two inputs share a name, you will not reliably get both values in PHP; only one value ends up associated with that key.

Practical fixes:

  • Rename the hidden field to something like search_mode (or remove it and detect submission by isset on the select field).
  • Give the select a distinct name such as selected_country.
  • Do not give the submit button the same name as other inputs (it can generally be left unnamed unless you need its value).
  • Use parameterized queries to avoid SQL injection and to safely pass the selected value to the database. Example using PDO:
// assume $pdo is a PDO connection
if (isset($_POST['search_mode']) && $_POST['search_mode'] === 'country_search') {
    $country = $_POST['selected_country'] ?? '';
    $stmt = $pdo->prepare('SELECT * FROM products WHERE country = :country AND activated = 1 ORDER BY id DESC');
    $stmt->execute([':country' => $country]);
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

Extra tips: validate the posted country against a whitelist or use numeric country IDs for lookups, index the column used in WHERE for performance, and avoid leading wildcards in LIKE if you want the query to use an index.

Recommended Answers

All 2 Replies

The select has the same name as your hidden input... That's what's messing it up.

your drop down and submit button has the same name, thats causing ambiguity

<select name="country" id="country" class="tb12" title="Search country">				
             <option value="England">England</option>
             <option value="USA">USA</option>    
          </select>

and

<input name="country" type="submit" id="country" class="tb11" value="Search country" title="Search by country" />
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.