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> </p>
<p> </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"/>
It appears that I solved the problem by simply changing the label
tags to p tags instead. I guess that got around the conflict.
Related
I have one dropdown with a foreach loop which will pass to model using post method.
<div class="form-group" ">
<select class="form-control" id="rel" name="rl[][rel]" >
<option></option>
<?php
foreach ($relationship as $row) {
?>
<option value="<?php echo $row->relID; ?>"><?php echo $row->relCat; ?></option>
<?php
}
?>
</select>
</div>
and in the model it is getting the proper values using post method. As
$rel = $_POST['rel'];
Here the problem is when the user select one option ,I want to get two values like
"<?php echo $row->relID; ?>" and "<?php echo $row->relCatID; ?>"
like
$rel = $_POST['rel']; //this for the $row ->relID
$relcat = $_POST['relcat'];//this for the $row ->relCatID
I want to get both from one selection without adding any visble dropdown or element..
Try bellow code with ajax call
In Javascript code get value
var name = $('#rel option:selected').attr('data-cat_id');
var id = $('#rel').val();
<div class="form-group" >
<select class="form-control" id="rel" name="rl[][rel]" >
<option></option>
<?php foreach ($relationship as $row) { ?>
<option value="<?php echo $row->relID; ?>" data-cat_id="<?php echo $row->relCatID; ?>"><?php echo $row->relCat; ?></option>
<?php } ?>
</select>
</div>
I want to keep the selected values in select box after search .This is a vehicle search filter page and Page is coded using php and connected with database.
Database have 2 columns for both options that is driver age for 21-24 and driver_age for 25+ as some vehicles also available for both age of drivers
code below
<script>
$('#driver').change(function() {
if($('#driver option:selected').val() == '21-24') {
$('#driver').attr('name','driverage');
}
if($('#driver option:selected').val() == '25+') {
$('#driver').attr('name','driver_age');
}
});
</script>
<form id="myform" method="GET" action="">
<div class="fields">
<p>Vehicle Type</p>
<select class="car" name="car_type" id="car_type">
<option value="0">Select Vehicle</option>
<?php if(count($vehicleType) > 0 ){
foreach($vehicleType as $vt){ ?>
<option value="<?php echo $vt ?>" <?=isset($_GET['car_type'])&&$_GET['car_type']==$vt?'selected':'';?> ><?php echo $vt ?></option>
<?php }
} ?>
</select>
</div>
<div class="fields">
<p>Age of Youngest Driver</p>
<select class="half" id="driver" name="">
<option value="21-24">21-24</option>
<option value="25+">25+</option>
</select>
</div>
<input type="hidden" name="search" />
<div class="fields" style="text-align:center">
<input type="submit" value="Search" class="mitsub" />
</div>
</form>
Thanks and please help
You can add a conditional statement inside your foreach() that adds selected attribute to option. This will ensure that the selected value is selected in the list of options.
<?php
if(count($vehicleType) > 0 ){
foreach($vehicleType as $vt){
echo "<option value='$vt'";
if(isset($_GET['car_type']) && $_GET['car_type'] == $vt){
echo " selected";
}
echo ">$vt</option>\r\n";
}
}
?>
You can do this all in one, you just have to make sure the logic is correct.
echo "<option value='$vt'" . ((isset($_GET['car_type'] && $_GET['car_type'] == $vt) ? " selected" : "") . ">$vt</option>\r\n";
I just find it easier to work with a full if statement.
I have three multiple select menus. I am trying to select the options returned from a database query. It is not setting any options. I console log the arrays returned by php and they are displayed as:["5233", "7148"]["5233", "5437", "5316"]["7029", "7852", "5525"].
I am not concerned about the form submission at this point. I only want to display the values returned from the db query as selected. I am sure there is something I am missing but can't seem to figure it out. I appreciate all the help I get. Thank you in advance!!
First are my 3 database arrays that are created:
Second is my html:
Third is my javascript/jQuery:
I have three multiple select menus. I am trying to select the options returned from a database query. It is not setting any options. I console log the arrays returned by php and they are displayed as:["5233", "7148"]["5233", "5437", "5316"]["7029", "7852", "5525"].
I am not concerned about the form submission at this point. I only want to display the values returned from the db query as selected. I am sure there is something I am missing but can't seem to figure it out. I appreciate all the help I get. Thank you in advance!!
First are my 3 database arrays that are created:
Second is my html:
Third is my javascript/jQuery:
<?
$huntNum = $hunts[$i][$fm_hunt_fields['__id']];
$cookRole = 'Cook';
$cookVols = get_volunteersByHunt($huntNum, $cookRole);
foreach ($cookVols as $cVols) {
//create new cook array that only contains ID of person
$exCooks[] = $cVols['_id_acct'];
}
$guideRole = 'Hunt Guide';
$hgVols = get_volunteersByHunt($huntNum, $guideRole);
foreach ($hgVols as $hVols) {
$exHg[] = $hVols['_id_acct'];
}
$supportRole = 'General Support';
$gsVols = get_volunteersByHunt($huntNum, $supportRole);
foreach ($gsVols as $gVols) {
$exGs[] = $gVols['_id_acct'];
}
?>
<script>
var existing_cooks = <?= json_encode($exCooks); ?>
var existing_hgs = <?= json_encode($exHg); ?>
var existing_gss = <?= json_encode($exGs); ?>
</script>
<form action="" name="vol-update" id="vol-update" class="vol- update" method="post">
<input type="hidden" id="id_hunt" name="id_hunt" value="<?= $huntNum; ?>" />
<input type="hidden" name="action2" id="action2" value="1" />
<table class="tyhp-account">
<tr>
<td>
<div class="floatL cont_lft_side">
<label for="cooks"><strong>Cooks</strong></label>
<select name="cook[]" class="form-control vCooks" multiple="multiple">
<?php
foreach ($cooksArray as $cook) {
?>
<option
<?
if (in_array($cook['ID'], $exCooks)) {
echo "selected='selected'";
}
?>
value="<?= $cook['ID']; ?>" >
<?= $cook['name_last'] . $cook['name_first']; ?>
</option>
<?
}
?>
</select>
</div>
</td>
<td>
<div class="floatL cont_lft_side">
<label for="hunt_guides"><strong>Hunt Guides</strong></label>
<select name="huntGuide[]" class="form-control vHg" multiple="multiple">
<?php
foreach ($guidesArray as $guide) {
?>
<option
<?
if (in_array($guide['ID'], $exHg)) {
echo "selected='selected'";
}
?>
value="<?= $guide['ID']; ?>" >
<?= $guide['name_last'] . $guide['name_first']; ?>
</option>
<?
}
?>
</select>
</div>
</td>
<?php
$allVols = getAllVolunteers();
?>
<td>
<div class="floatL cont_lft_side">
<label for="supp"><strong>General Support</strong></label>
<select name="gsupport[]" class="form-control vSupp" multiple="multiple">
<?php
foreach ($allVols as $allVol) {
?>
<option
<?
if (in_array($allVol['__id'], $exGs)) {
echo "selected='selected'";
}
?>
value="<?= $allVol['ID']; ?>" >
<?= $allVol['name_last'] . $allVol['name_first']; ?>
</option>
<?
}
?>
</select>
</div>
</td>
</tr>
<tr>
<td> </td>
<td style="text-align:center">
<input type="submit" name="action" id="upVol" class="btn btn-primary" value="Update Volunteers">
</td>
<td> </td>
</tr>
</table>
</form>
//Volunteer information for hunts
<script>
var cooks = existing_cooks;
var hunt_guides = existing_hgs;
var gen_support = existing_gss;
console.log(cooks);
console.log(hunt_guides);
console.log(gen_support);
//Cooks multiSelect
$(".vCooks").val(cooks);
$('.vCooks').multiselect({
columns: 2,
placeholder: 'Select Cooks'
});
//Hunt Guide multi-select
$(".vHg").val(hunt_guides);
$('.vHg').multiselect({
columns: 2,
placeholder: 'Select Hunt Guides'
});
//General Support multi-select
$(".vSupp").val(gen_support);
$('.vSupp').multiselect({
columns: 2,
placeholder: 'Select General Support'
});
return false;
</script>
Im trying to retrieve the value from a select box after the selected index has been changed. I keep getting an undefined index variable.
The form reloads the page so that I can update a table elsewhere on the page. The options are filled from the results of an SQL query.
The select box code.
<form action="" method="post">
<label>Select School</label>
<select class="form-control" name="schoolSelect" onchange="this.form.submit()">
<?php
foreach ($faculty as $key) { ?>
<option value="<?php echo $key['1']; ?>"><?php echo $key['1']; ?></option>
<?php } ?>
</select>
</form>
PHP used to retrive value
if (isset($_POST['schoolSelect'])){
$selectedSchool = $_POST['schoolSelect'];
$result = executeUserSelect($sqlUserBySchool, $db, $_POST['schoolSelect']);
}
EDIT
var dump =
array (size=1)
'schoolSelect' => string 'Plymouth Business School' (length=24)
Select box text = Plymouth Business School
Thanks in advance
Tony
<body>
<?php
if (isset($_POST['schoolSelect'])){
$selectedSchool = $_POST['schoolSelect'];
echo $selectedSchool;
}
else {
?>
<form action="" method="post">
<label>Select School</label>
<select class="form-control" name="schoolSelect" onchange="this.form.submit()">
<?php
foreach ($faculty as $key) { ?>
<option value="<?php echo $key['1']; ?>"><?php echo $key['1']; ?></option>
<?php } ?>
</select>
</form>
<?php } ?>
</body>
I have some manufactures in my store and I want to modify the search so that you can choose the manufacture from a drop-down menu :)
I wish to change the search box into a drop-down menu in opencart. Is that possible? If yes, please tell me how.
Thank you !
Ok, I don't know why you are doing this, but here is one way...
I am using header search for this example, Opencart 1.5.4.1
Edit catalog/controller/commmon/header.php. After line 96:
$this->load->model('catalog/product');
add this:
// - - Manufacturers Dropdown Data Start - - - - - - - -
// load manufacturer model
$this->load->model('catalog/manufacturer');
//get manufacturers data
$manufacturers = $this->model_catalog_manufacturer->getManufacturers();
//populate data array for use in view
$this->data['manufacturers'] = array();
foreach ($manufacturers as $manufacturer){
$this->data['manufacturers'][] = array(
'id' => $manufacturer['manufacturer_id'],
'name' => $manufacturer['name']
);
}
// - - Manufacturers Dropdown Data End - - - - - - - - -
Now, in catalog/view/theme/xxx/template/common/header.tpl find search DIV, comment out the search input (lines 55-62):
<div id="search">
<!-- <div class="button-search"></div>
<?php if ($filter_name) { ?>
<input type="text" name="filter_name" value="<?php echo $filter_name; ?>" />
<?php } else { ?>
<input type="text" name="filter_name" value="<?php echo $text_search; ?>" onclick="this.value = '';" onkeydown="this.style.color = '#000000';" />
<?php } ?> -->
...
Add your select element:
<div id="search">
<!-- <div class="button-search"></div>
<?php if ($filter_name) { ?>
<input type="text" name="filter_name" value="<?php echo $filter_name; ?>" />
<?php } else { ?>
<input type="text" name="filter_name" value="<?php echo $text_search; ?>" onclick="this.value = '';" onkeydown="this.style.color = '#000000';" />
<?php } ?> -->
<select name='manuf_dropdown' ONCHANGE="location = this.options[this.selectedIndex].value;">
<option value=''></option>
<?php foreach ($manufacturers as $manufacturer) {
if (isset($_GET['manufacturer_id']) && $_GET['manufacturer_id'] == $manufacturer['id']){
?>
<option selected="selected" value="./?route=product/manufacturer/info&manufacturer_id=<?php echo $manufacturer['id'] ?>"><?php echo $manufacturer['name'] ?></option>
<?php } else { ?>
<option value="./?route=product/manufacturer/info&manufacturer_id=<?php echo $manufacturer['id'] ?>"><?php echo $manufacturer['name'] ?></option>
<?php }} ?>
</select>
</div>
Let me know if you need any explanation of the code.