6

I have a search form below that has two select input types and one text input field where the user can search by keywords. For some reason, whenever I click on keywords text box, the focus changes immediately back to the first select box drop down for Category and I so I can't type anything in the keywords text box. Can anyone see why this is happening?

$categories_list = array();
$ratings_list = array();

try {
$query = $dbh->query("SELECT category, genre, rating FROM posts WHERE 1
GROUP BY category, genre, rating");
$query->setFetchMode(PDO::FETCH_ASSOC);
$iterator = new IteratorIterator($query);
foreach ($iterator as $row) {
    if (empty($row['category']) == false && !in_array($row['category'],
$categories_list))
        $categories_list[] = $row['category'];
    if (empty($row['rating']) == false && !in_array($row['rating'],
$ratings_list))
        $ratings_list[] = $row['rating'];
}
} catch (Exception $e) {
echo '<p>', $e->getMessage(), '</p>';
}
?>


<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>Search Completed Stories</h2>
<form name="search" method="get" 
action="http://example.com/searchB/">
<label>
    Category:
    <select name="category">
        <?php foreach($categories_list as $category) : ?>
            <option><?php echo $category; ?></option>
        <?php endforeach; ?>
    </select>
</label>
<label>
    Rating:
    <select name="rating">
            <option value="">Any</option>
        <?php foreach($ratings_list as $rating) : ?>
            <option><?php echo $rating; ?></option>
        <?php endforeach; ?>
    </select>
</label>
<label>
    Keywords:
    <input type="text" name="keywords">
</label>
<input type="hidden" name="active" value="0">
<input type="submit"/>

8
  • Changing focus is useally caused by some type of Javascript, yet I don't see any JS in your code. As far as I know, PHP cannot reset the focus after the page is already loaded.
    – icecub
    Commented Jul 4, 2015 at 1:03
  • I guess that might be it. You are right that I am not using any JS anywhere on the page.
    – JA4677
    Commented Jul 4, 2015 at 1:05
  • Oddly it works fine on a development page. But when I place this code on to another page that has another list of results, then I get this focus issue with the keywords input text box
    – JA4677
    Commented Jul 4, 2015 at 1:10
  • Well there's nothing in your code above that will cause a change of focus. So something else must be going on. You can try providing additional code or, if there's no other option, provide a link where this script is running. I might be able to see more if I actually see it happening.
    – icecub
    Commented Jul 4, 2015 at 1:12
  • Thanks for the offer but it appears to be the wordpress template causing the issue. I used the exact same code on another wordpress template and it works fine.
    – JA4677
    Commented Jul 4, 2015 at 1:20

1 Answer 1

0

It appears that I solved the problem by simply changing the label tags to p tags instead. I guess that got around the conflict.

Not the answer you're looking for? Browse other questions tagged or ask your own question.