Fetch records count using PHP and MongoDB - mongodb

I have an HTML page with a department selectbox. when end user selects any department I have no show him the no of employs in that department. The employee collect
<select multiple="" class="chosen-select choice" id="form-field-select-4" data-placeholder="Choose a Department..." onchange="show_employee_count(this.value)">
<option value="Management">Senior Management</option>
<option value="Legal">Legal</option>
<option value="Operations">Operations</option>
<option value="Administration">Administration</option>
<option value="R&D">R&D</option>
<option value="Sales & Marketing">Sales & Marketing</option>
<option value="Finance">Finance</option>
<option value="Technology">Technology</option>
<option value="Customer Service">Customer Service</option>
<option value="Pro Services">Pro Services</option>
<option value="HR">HR</option>
</select>
The count of employee to show in this container div
<div id="emp_count"></div>

I have tried this as :
HTML
<select name='department' id='department'>
<option value="Management">Senior Management</option>
<option value="Legal">Legal</option>
<option value="Operations">Operations</option>
<option value="Administration">Administration</option>
<option value="R&D">R&D</option>
<option value="Sales & Marketing">Sales & Marketing</option>
<option value="Finance">Finance</option>
<option value="Technology">Technology</option>
<option value="Customer Service">Customer Service</option>
<option value="Pro Services">Pro Services</option>
<option value="HR">HR</option>
</select>
<div id="leads_coount"></div>
JS Code
<script type="text/javascript">
$(function(){
$("#department").change(function(){
var selectedDepartment = $(this).find(":selected").val();
console.log("the department you selected: " + selectedDepartment);
$.ajax({
type: "POST",
url: "ajax.php",
cache: false,
data: { method: "get_lead_count", department: selectedDepartment }
})
.done(function( leads ) {
$( "#leads_coount" ).html(leads);
}).fail(function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
});
});
});
</script>
ajax.php
<?php require_once('DataAccessBean.class.php');
if($_POST['method'] == 'get_lead_count') {
// select a collection
$collection = new MongoCollection($db, 'employs');
// find the collections with department
$clause = array('Department' => $_POST['department']);
$cursor = $collection->find($clause)->count();
echo 'There are '.$cursor.' employees available in this department';
//echo $_POST['department'];
}
Is there a better way to do?

Related

testing library userEvent.selectOptions does not select option

I have a React component which renders a combobox (another external component) with an option. When deployed the component does work as expected.
<select class="country-select"
id="nationalityCode" name="nationalityCode" required="">
<option value="" disabled="" hidden="">Choose</option>
<optgroup label="Most used">
<option value="0052">Belgium</option>
<option value="0055">German Citizien</option>
<option value="0057">French</option>
<option value="0001">Dutch</option>
</optgroup>
<optgroup label="Alle Nationalities">
<option value="0052">Belgium</option>
<option value="0055">German Citizien</option>
<option value="0057">French</option>
<option value="0001">Dutch</option>
<option value="0050">Albanese</option>
<option value="0223">American Citizen</option>
</optgroup>
</select>
When using testing library userEvent.selectOptions, the select of an option is successfull. Only the expect fails with
Error: expect(received).toBe(expected) // Object.is equality
Expected: true
Received: false
When debugging the selection state for all options it shows that the initial selected option is selected.
if (changeValue.nationaliteit) {
const selectOption = screen.getAllByRole('option', { name: 'French' })[0];
await userEvent.selectOptions(
screen.getByRole('combobox', { name: 'Nationaliteit' }),
selectOption
);
expect(
(screen.getAllByRole('option', { name: 'French' })[0] as HTMLOptionElement).selected
).toBe(true);
}
Using
"#testing-library/user-event": "^14.4.3",
"#testing-library/jest-dom": "^5.16.5",
"#testing-library/react": "^12.1.5",

Add optgroup dynamically to an existing select

I have an existing select with 5 options and a default blank value. I want to add an optgroup to the first 3 values and another optgroup to the last two options.
<select id="cityDropdown">
<option selected="selected" value="" label=" "> </option>
<option value="1000">Tokyo</option>
<option value="1001">Kyoto</option>
<option value="1002">Osaka</option>
<option value="1003">Incheon</option>
<option value="1004">Busan</option>
</select>
I want my select to look like this
<select id="cityDropdown">
<optgroup label="Japan">
<option value="1000">Tokyo</option>
<option value="1001">Kyoto</option>
<option value="1002">Osaka</option>
</optgroup>
<optgroup label="SoKorea">
<option value="1003">Incheon</option>
<option value="1004">Busan</option>
</optgroup>
</select>
Below is my existing code which no longer works since the values are no longer even.
$(document).ready(function() {
var select = $('#cityDropdown');
var opt1, opt2;
$('option', select).each(function(i) {
if (i % 2 === 0) {
opt1 = $(this);
} else {
opt2 = $(this);
var optgroup = $('<optgroup/>');
optgroup.attr('label', label);
opt2.add(opt1).wrapAll(optgroup);
}
});
});

Custom data-attributes from select_tag not submitting - Rails 4

I have a simple search form_tag in a Rails 4 app.
One of the options is a select_tag:
<%= select_tag "plottable[plottable_id]", options_for_select(Plottable.all.map{ |plottable| [ plottable, plottable.id, { 'plottbale-type' => plottable.class } ] }), prompt: 'Select one option'%>
The HTML is generated properly, resulting in:
<select name="plottable[plottable_id]" id="plottable_plottable_id">
<option value="">Select one option</option>
<option plottable-type="Pool" value="3">Pool1</option>
<option plottable-type="Pool" value="4">Pool2</option>
<option plottable-type="Asset" value="33">Asset1</option>
<option plottable-type="Asset" value="34">Asset2</option>
</select>
When submitting the form the plottable-type does not get submitted. Here are the params:
Parameters: {"utf8"=>"✓", "from_time"=>"", "to_time"=>"", "plottable"=>{"plottable_id"=>"35"}, "commit"=>"Search"}
Thanks for your suggestions.
Just try this:
<select name="plottable[plottable_id]" id="plottable_plottable_id">
<option value="">Select one option</option>
<option plottable-type="Pool" value="3">Pool1</option>
<option plottable-type="Pool" value="4">Pool2</option>
<option plottable-type="Asset" value="33">Asset1</option>
<option plottable-type="Asset" value="34">Asset2</option>
</select>
<input type="hidden" name=plottable[type] value="", id="plottable_plottable_type">
$('#plottable_plottable_id').on('change', function(){
var type = $('option:selected', this).attr('plottable-type');
$('#plottable_plottable_type').val(type);
})
you can add hidden field tag by rails code as well
<%= hidden_field_tag "plottable[plottable_type]"%>

Display textbox after selection of option

I'm making a form at the moment and I was wondering how I could get the script to show a textbox after I have selected options "other" or "option 3".
HTML:
<select name="choice" onchange="if(this.selectedIndex==5)
{this.form['other'].style.visibility='visible'}else
{this.form['other'].style.visibility='hidden'};">
<option value="" selected="selected">Select...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="other">Other</option>
</select>
<input type="textbox" name="other" style="visibility:hidden; margin-left:3px"/>
Javascript:
window.addEvent('domready', function() {
$('hearabout').addEvent('change', function() {
if ($('hearabout').value == 'Rekening' ) {
$('other_input').setStyle('display', 'block');
} else {
$('other_input').setStyle('display', 'none');
}
});
if ($('hearabout').value == 'Rekening' ) {
$('other_input').setStyle('display', 'block');
} else {
$('other_input').setStyle('display', 'none');
}
});
How do I change "this.selectedIndex==5" so that it takes both option 3 and 5?
Thanks
Add ID for ur select <select id='someth'>
Add an event for changing the selected value:
$('#someth').on('change', function(){
if (('#someth').val() == "other")
{
$('#YourInputID').css('display','block');
}
});
Dont miss to add display:none; in the input style

Auto Refresh Div Content Using jQuery and AJAX passing variables

I am trying to setup a page where the users can select 1 criteria from a select menu and then select the second from another.
I would then like these variables to pass through my auto updating div using ajax so that they are used in the .php that is refreshed.
The select menu is working fine but how would I pass the values through ajax and then make sure it remembered them for the refresh.
FORM
<select id="employee_user">
<option value="">--</option>
<option value="333">Test User</option>
<option value="111">Testing Testing</option>
</select>
<select id="they" onchange="showUser(this.value, employee_user.value)">
<option value="">--</option>
<option value="20120801" class="333" title="Test User">20120801</option>
<option value="20110801" class="333" title="Test User">20110801</option>
<option value="20100801" class="333" title="Test User">20100801</option>
<option value="20120801" class="111" title="Testing Testing">20120801</option>
<option value="20110801" class="111" title="Testing Testing">20110801</option>
</select>
</form>
AUTO REFRESHING DIV
<script>
(function($)
{
$(document).ready(function()
{
$.ajaxSetup(
{
cache: false,
beforeSend: function() {
$('#updatingdiv').hide();
$('#loading').show();
},
complete: function() {
$('#loading').hide();
$('#updatingdiv').show();
},
success: function() {
$('#loading').hide();
$('#updatingdiv').show();
}
});
var $container = $("#updatingdiv");
$container.load("getholidaylog.php");
var refreshId = setInterval(function()
{
$container.load('getholidaylog.php');
}, 9000);
});
})(jQuery);
</script>
<div id="updatingdiv"></div>
<img src="loading.gif" id="loading" alt="loading" style="display:none;" />
and then getholidaylog.php would:
$year = $_GET["year"];
$username = $_GET["username"];
and use for the database query.
EDIT
$j(document).ready(function() {
$j("#year_select").change(function (){
$.ajaxSetup(
{
cache: false,
beforeSend: function() {
$('#updatingdiv').hide();
$('#loading').show();
},
complete: function() {
$('#loading').hide();
$('#updatingdiv').show();
},
success: function() {
$('#loading').hide();
$('#updatingdiv').show();
}
});
var $container = $("#updatingdiv");
var user_select= $j('#user_select').val();
var year_select= $j('#year_select').val();
$container.load('getholidaylog.php',{username:user_select,year:year_select});
var refreshId = setInterval(function()
{
$container.load('getholidaylog.php');
}, 9000);
});
})(jQuery);
**
Following code passing the values to php page successfully
<script type="text/javascript">
$(document).ready(function(){
$("#they").change(function () {
var firstVal = $("#employee_user").val();
var secVal = $("#they").val();
$.ajax({
type: "POST",
data: "year="+ firstVal +"&username="+ secVal,
url: "getholidaylog.php",
success: function(msg){
$("#updatingdiv").html(msg);
}
});
});
});
</script>
but instead of loading the getholidaylog.php into div load the content of respective page into the div
DO Something like this..
Add two hidden fields in your html like this:
-------
<input type="hidden" id="userselect" />
<input type="hidden" id="yearselect" />
</form>
Now Change your jQuery code like this..
Modify your onchange function of year_select like this and put it separately . Dont include ajax setup in this.
$j("#year_select").change(function (){
$('#userselect').val($('#employee_user').val());
$('#yearselect').val($('#they').val());
});
Now take the value from hidden field.change these lines.
var user_select= $j('#userselect').val();
var year_select= $j('#yearselect').val();
So your Final hmtl markup would look like this:
<select id="employee_user">
<option value="">--</option>
<option value="333">Test User</option>
<option value="111">Testing Testing</option>
</select>
<select id="they" onchange="showUser(this.value, employee_user.value)">
<option value="">--</option>
<option value="20120801" class="333" title="Test User">20120801</option>
<option value="20110801" class="333" title="Test User">20110801</option>
<option value="20100801" class="333" title="Test User">20100801</option>
<option value="20120801" class="111" title="Testing Testing">20120801</option>
<option value="20110801" class="111" title="Testing Testing">20110801</option>
</select>
<select id="employee_user">
<option value="">--</option>
<option value="333">Test User</option>
<option value="111">Testing Testing</option>
</select>
<select id="they" onchange="showUser(this.value, employee_user.value)">
<option value="">--</option>
<option value="20120801" class="333" title="Test User">20120801</option>
<option value="20110801" class="333" title="Test User">20110801</option>
<option value="20100801" class="333" title="Test User">20100801</option>
<option value="20120801" class="111" title="Testing Testing">20120801</option>
<option value="20110801" class="111" title="Testing Testing">20110801</option>
</select>
<input type="hidden" id="userselect" />
<input type="hidden" id="yearselect" />
</form>
and your code will look like this:
$(function(){
$("#year_select").change(function (){
$('#userselect').val($('#employee_user').val());
$('#yearselect').val($('#they').val());
});
$.ajaxSetup(
{
cache: false,
beforeSend: function() {
$('#updatingdiv').hide();
$('#loading').show();
var user_select= $j('#userselect').val();
var year_select= $j('#yearselect').val();
},
complete: function() {
$('#loading').hide();
$('#updatingdiv').show();
},
success: function() {
$('#loading').hide();
$('#updatingdiv').show();
}
});
$container.load('getholidaylog.php',{username:user_select,year:year_select});
var refreshId = setInterval(function()
{
$container.load('getholidaylog.php');
}, 9000);
})
The low level $.ajax() method contains a variable called data through which you can pass data to the server. So if you want the send data in this case, you can do something like:
$.ajax({
data: {year:1999,username:'Codded'}
});
Same goes for load, the second parameter there is for passing data, so you can do
$("#divId").load('foo.php',{username:'Codded',year:1999});
u can use the data attribute of the ajax function to pass the selected value like this example :
$.ajax({
data: {'name': 'Dan'} // name is the name of the variable and Dan is the value in your case if the selected value
});
Check out your id of select is same as given below because it does not matches with the select box you have coded
var user_select= $j('#user_select').val();
var year_select= $j('#year_select').val();