On the frontend of my Laravel application I have a list of users (people looking for work) which I have filters to filter certain parameters of the users which appends the URL using the request().
My current filters are all anchors, so I am using the HREF attribute to update the url like so:
Filter By Full Time
Full Time
The above filter will append the url with mysite.io/users/?work_type=full_time and filter all users who work Full Time. I have this all working for all filters using anchor tags and href.
What I cannot work out is how to do a similar approach using a Select Menu to filter users by their city?
I have a list of cities in my database. They are dynamically added to the select menu and displaying on the page.
<select>
<option value="1">City Name 1</option>
<option value="2">City Name 2</option>
</select>
What I am trying to achieve is how 'OnChange' of the select menu I can pass the city ID to the Request query paramater as &city=2
You can add an event listener to the dropdown element and reload the page or anything else if you want. Let's see an example on reloading the page with the query param.
I have changed your select dropdown markup a little bit, adding an ID and a default option,
<select id="myFancyDropdown">
<option>Choose city</option>
<option value="1">City Name 1</option>
<option value="2">City Name 2</option>
</select>
Then in JS, do your thing when the dropdown is changed,
let elmSelect = document.getElementById('myFancyDropdown');
if (!!elmSelect) {
elmSelect.addEventListener('change', e => {
let choice = e.target.value;
if (!choice) return;
let url = new URL(window.location.href);
url.searchParams.set('city', choice);
// console.log(url);
window.location.href = url; // reloads the page
});
}
You could use a form to set the URL parameters.
<form id="myForm" method="get">
<button
id="workTypeButton"
type="button"
data-value="full_time"
onclick="setWorkTypeInput(this);"
>
Full Time
</button>
<select name="city">
<option value="1" {{ request('city') === '1' ? 'selected' : '' }}>City Name 1</option>
<option value="2" {{ request('city') === '2' ? 'selected' : '' }}>City Name 2</option>
</select>
<input id="workTypeInput" type="hidden" name="work_type" value="{{ request('work_type') }}">
</form>
Then add an event listener to submit the form if any input inside it changes. Instead of using a anchor to filter by full time, you could bind a button (workTypeButton) onclick event to a hidden input (workTypeInput) inside the form.
let myForm = document.getElementById('myForm');
let workTypeInput = document.getElementById('workTypeInput');
myForm.querySelectorAll('input, select, textarea')
.forEach(item => {
item.addEventListener('change', event => {
myForm.submit();
});
});
function setWorkTypeInput(event) {
let changeEvent = new Event('change');
workTypeInput.value = event.dataset.value;
workTypeInput.dispatchEvent(changeEvent);
}
If you use a radio or checkbox to set the work type. This way you would not need to define a onclick handler.
Related
I have php search filter page in which i used different fields..One of them is Age of youngest driver field which have 2 values like 21-24 and 25+ ..Basically i only want to keep the selected values in the select box even after search so user know what he searched for ..What happen now when i select 25+ from the field and click on search it goes back to 21-24 . But As page is connected top database so it picks values from database which i enter like the database screenshot below Please check and let me know if it have any solution
<select class="half" id="form_frame" name="" onsubmit="getData(this);"/>
<option id="value1" value="21-24" selected="selected">21-24</option>
<option id="value2" value="25+">25+</option>
</select>
When i slect all values from the field and selct 25+ from driver age field then url is
http://localhost/Vehicle2/?car_type=0&pickup=0&return=0&residence_two=International&driver_age=25%2B&passengers=No.+of+Passengers&search=
http://localhost/Vehicle2/?car_type=0&pickup=0&return=0&residence_two=International&driver_age=21-24&passengers=No.+of+Passengers&search=
I want driverage=21-24 instead of driver_age
Apply onchange event in javascript like as follows if you are using jquery
jQuery(function () {
jQuery("#form_frame").change(function () {
var id = jQuery(this).val();;
location.href = "http://localhost/php-page/option"+id+"="+id
})
})
Simple use change event handler for selectbox
$(document).on('change','#form_frame',function(){
//window.location.href="http://localhost/php-//page/option"+$(this).//val()+"="$(this).val();
console.log("http://localhost/phppage/option"+$(this).val()+"="+$(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="half" id="form_frame" name="">
<option value="10" selected="selected">10</option>
<option value="11">11</option>
</select>
On my expectation, I want to make an Add button on the bottom of option.
Here is my code:
<select class="form-control" name="id_sales" style="margin-bottom:15px;">
<option value="" selected disabled>Sales</option>
<?php
$result = mysqli_query($con,"SELECT * FROM sales");
while ($row = mysqli_fetch_array($result)) {
?>
<option value="<?= $row['id_sales']; ?>"><?= $row['id_sales']; ?> - <?= $row['nama_sales']; ?></option>
<?php } ?>
<a href="index.php?page=sales" class="btn btn-primary">
<span class="fa fa-plus"></span> Add new sales
</a>
</select>
How to make it works?
You can use Jquery fo this, As you can't put an anchor in option tag
Here is the solution for question,
<select class="form-control" name="id_sales" id="id_sales" style="margin-bottom:15px;">
<option value="" selected disabled>Sales</option>
<option value="" >sales 1</option>
<option value="" >sales 2</option>
<option value="" >sales 3</option>
<option value="add_sales" id="add_sales">Add new sales</option>
<script type="text/javascript">
$(document).on('change','#id_sales', function (){
var selected_value = $(this).val();
if(selected_value=="add_sales"){
window.location.replace("url where you want to go on clicking 'Add new sales'");
}
});
</script>
In HTML, a possible element inside a select can only be a options element. You can not put anything inside the select tag except this.
This also makes sense as select and options define a single element. They represent a single entity.
So, first of all you need to declare the anchor outside the select tag to be able to see the link.
And, Using HTML a data can only be submitted using a form element. There is no substitute of it.
So, you need to wrap your fields in a form element too or you should use ajax. But Ajax also has its own use case so shouldn't be use if not necessary.
To make this work first wrap your element inside a HTML form and change the anchor to a submit type input/button. And, define the script url in form's action tag. And, if you must use anchor to submit the form do it using JavaScript.
Hope this is what you asked.
I have the following form I want to use to filter through products on my website:
<div class="top-filter-select-container">
<form method="GET" action="" id="sort-filter-pick">
<select class="selectpicker" id="sort-filter">
<option value="popularity">Sort by Popularity</option>
<option value="ratings">Sort by Ratings</option>
<option value="newest">Sort by Newest</option>
<option value="lowest">Sort by Lowest Price</option>
</select>
</form>
</div>
I am trying to reload my page when an option is selected using jQuery form.submit() and retrieve the selected option to be used for filtering with a SQL query. I would eventually want to use this value along with other filtering values for a more complex filtering.
$(function(){
$('#sort-filter').on('change', function() {
var action = $(this).val();
$("#sort-filter-pick").attr("action", "?sort=" + action);
this.form.submit();
});
});
To test my code, I am just trying to echo isset($_GET['sort']) ? $_GET['sort'] : null;
The code works if I change form method to POST instead of GET but doesn't work with GET. On websites such as Amazon, the GET form method is used when applying filters from a select option but I also notice that the ?sort=... is added to the page URL after the form is submitted, which is not the case for me if I use GET. I was wondering what would be the right approach to do the same thing.
If you add a "name" to your form-elements you don't need to manually add the sort-criteria with jQuery.
HTML:
<div class="top-filter-select-container">
<form method="GET" action="" id="sort-filter-pick">
<select class="selectpicker" name="sort" id="sort-filter">
<option value="popularity">Sort by Popularity</option>
<option value="ratings">Sort by Ratings</option>
<option value="newest">Sort by Newest</option>
<option value="lowest">Sort by Lowest Price</option>
</select>
</form>
</div>
Javascript:
$(function(){
$('#sort-filter').on('change', function() {
this.form.submit();
});
});
Or you could just use location.href = url rather than posting the form.
I have a form which posts to a preset url variable, I'd like to add an extension to that url by using a dropdown selector:
<form class="my-form" method="POST" action="<?php echo $url; ?>">
<select name="script_to_follow">
<option value="script1.php">Report 1</option>
<option value="script2.php">Report 2</option>
</select>
Does anyone know how to add the value selectors as extenstions to the URL, for example if $URL= www.example.com and Report 1 was selected the resulting action would be to post to www.example.com/script1.php
You May have to do this using jQuery:
var url = 'www.example.com';
$(".script_to_follow").change(function(){
var selvalue = $(this).val();
url = url + '/' + selvalue;
window.location = url;
})
You can do it by using jQuery.
On Submit call a function:
var action = $('form.my-form').prop('action');
var selectedOption = $('select[name="script_to_follow"]').val();
$('form.my-form').prop('action', action+'/'+selectedOption;
You have two options here:
Use javascript to listen for change events on the <select> field, and change the action of the form based on what's been selected. This is what the other answers are suggesting.
To you avoid using javascript, instead of pointing the form towards different php scripts based on the selected value, have a single php script that switches based on the selected value. So:
<form class="my-form" method="POST" action="action.php">
<select name="script_to_follow">
<option value="a">Report 1</option>
<option value="b">Report 2</option>
</select>
And then in action.php do something like:
switch ($_GET["script_to_follow"]) {
case "a":
// contents of script1.php
case "b":
// contents of script2.php
}
simply try this
add id attribute to select box as
<select name="script_to_follow" id="script_to_follow">
<option value="script1.php">Report 1</option>
<option value="script2.php">Report 2</option>
</select>
now write some jquery codes to change action attribute with change in option
<script>
$('#script_to_follow').change(function(){ // onchange event
var url = $('#script_to_follow').val(); // get selected option
$('form.my-form').prop('action',url); // replace action attribute
});
</script>
Note : do not forget to include jquery script before form
<script
src="https://code.jquery.com/jquery-3.1.1.js"
integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA="
crossorigin="anonymous"></script>
I'm trying to create a drop-down list with four options such that if I select the 4th option, I want a text box created so that I can get the value typed in that box using "$_GET"
Something like this;
<select name="value">
<option value="value1">Option 1</option>
<option value="value2">Option 2</option>
<option value="value3">Option 3</option>
<!-- And a 4th one -->
</select>
And if the 4th one is selected, a box should appear like this;
<input type="text" name="firstname">
Edit;
My current code;
<script>
jQuery(function($){ //calling the code inside braces when document loads and passing jQuery object as '$' parameter;
$("select[name='sortby']").change(function(){ //binding an event that fires every time select value changes
var select = $(this); //caching select, which value was changed
if(select.val() === "byDefindex"){ //checking if we selected the right option
$("<input>").attr({type: "text", name: "defindex"}).appendTo(select.parent()); //creating new input element object, setting its value to "value4" and appending to select parent element or wherever you want it
}
});
});
</script>
<form action="<?php $_PHP_SELF ?>" method="GET">
Select:
<br />
<select name="sortby">
<option value="playHours">Play Hours</option>
<option value="lastLogin">Last Login</option>
<option value="byDefindex">By Defindex</option>
</select>
<br />
<input type="submit" />
</form>
If your 4th option is this:
<option value="value4">Option 4</option>
You can use jQuery to display the field.
Put your field in a <div>
<div id="field"><input type="text" name="firstname"></div>
Now,
<script type="text/javascript">
$(document).ready(function(){
$('input[name="value"]').change(function(){
var v = $('input[name="value"]').val();
if(v=="value4") $('#field').show();
else $('#field').hide();
})
})
This is usually done via javascript; something like this (by using popular JavaScript library, jQuery) :
jQuery(function($){ //calling the code inside braces when document loads and passing jQuery object as '$' parameter;
$("select[name='value']").change(function(){ //binding an event that fires every time select value changes
var select = $(this); //caching select, which value was changed
if(select.val() === "value4"){ //checking if we selected the right option
$("<input>").attr({type: "text", name: "firstname"}).appendTo(select.parent()); //creating new input element object, setting its value to "value4" and appending to select parent element or wherever you want it
}
});
});
Hope that helps. You can find more here jQuery site
I'm not certain this is what you're asking, but it seems you're wanting to add new lines to your select list?
I often do something similar to this for adding new options to lists. The first option would be 'add a new record', like this:
<select name="value">
<option value="-1">Add New Option</option>
<option value="value1">Option 1</option>
<option value="value2">Option 2</option>
<option value="value3">Option 3</option>
</select>
Note that the value for "add new" is -1... you could put anything here, but it should be something that would never show up in the other options, so your javascript knows what to do when it is selected.
Add a 'onchange' to the select box like this:
<select name="value" onchange="if(this.value == -1) addOption(this);">
Note that if the selected option is -1, then a javascript function is called. It references itself (this), so that your function knows who called it.
Then create a function that allows adding a new option:
function addOption(theSelectElement){
// create a text box, underneath the select box:
var newInput=document.createElement('input');
newInput.type='text';
theSelectElement.parentNode.insertAfter(newInput,theSelectElement);
}
You'll want to add more code to this function so that the new text field has a name and perhaps an ID.
Hope this helps, or at least points you in the right direction.