How can I auto select from drop down.
Like where customer/reserve.php then click the dropdown and goes to customer/reserve_time&date.php
I'm comfortable using PHP if there's any tutorials can you link it to me
I search in Google but not a single one comes out.
I think you wants to redirect to another page whenever user changes dropdown value. So, you can do a javascript function call on dropdown change event.
<select name="dropdown" onchange="redirectPage(this.value);">
<option value="url1">Option A</option>
<option value="url2">Option B</option>
</select>
Your javascript function starts here:
<script type="text/javascript">
function redirectPage(value)
{
window.location.href = value;
}
</script>
Note: url1 and url2 are the link for the pages. Try This...
You need get in the second page the value selected for the user at the first page.
If you are using GET
$value = $_GET['param'];
If you are using POST
$value = $_POST['param'];
-
<select>
<option value="foo" <?php if($value=="foo") echo "selected"; ?>>bar</option>
<option value="foo1" <?php if($value=="foo1") echo "selected"; ?>>bar2</option>
</select>
UPDATE: Change page and pass parameter when select option.
<select onchange="window.location.href='other.php?value=' + this.value">
<option value="">-- Select one --</option>
<option value="men">Men</option>
<option value="woman">Woman</option>
</select>
Related
I have a filter that uses a select for the user to pick a value.
Using javascript, is it possible to set the selection as a "session" variable so that when the user reloads/revisits that page their previous filter is already picked?
This should not persist after the user closes their browser, just like a PHP session would.
Sample Select box markup:
<select name="filter-with" class="filter-with">
<option value="">Select . . .</option>
<option value="demo">Session</option>
<option value="ambassador">Ambassador</option>
<option value="customer_coordinator">Customer Coordinator</option>
<option value="account_prime">Account Prime</option>
<option value="marketing_prime">Host</option>
</select>
Thanks!
Good morning,
I have a form
<select name="select" onchange="window.open(this.options[this.selectedIndex].value,'_self')">
<option selected="selected">Select your size</option>
<option value="https://www.webpage.com/01">Size S</option>
<option value="https://www.webpage.com/02">Size M</option>
<option value="https://www.webpage.com/03">Size L</option>
<option value="https://www.webpage.com/04">Size XL</option>
</select>
So if people select size S they go to https://www.webpage.html/01 and so on.....
On al the pages i have the same select form, but how can i change the chosen option as selected="selected" using javascript?
So if people select size M, the go to https://www.webpage.com/02 and that the form is displaying size M on this page. So if option value url is the same as the page url it automaticly give the select="selected" to this option.
maybe a dump question, but javascript isnt my best part ;)
Thanks for all your help.
You can try
$("option[value='" + window.location.href.toString() + "']").closest("select").val(window.location.href.toString());
It will find the option exactly matching with page url
If url exactly corresponds option values
$("selectElementId").val(window.location);
You can achieve this using javascript or jQuery
HTML
<select name="select" id='option' onchange="window.open(this.options[this.selectedIndex].value,'_self')">
<option selected="selected">Select your size</option>
<option value="https://www.webpage.com/01">Size S</option>
<option value="https://www.webpage.com/02">Size M</option>
<option value="https://www.webpage.com/03">Size L</option>
<option value="https://www.webpage.com/04">Size XL</option>
</select>
jQuery solution
$(document).ready(function(){
$('#option').on('change',function(){
$("#option option:selected").attr('selected','selected');
});
});
If you want to do this with JavaScript only, I would recommend you to do this:
add an id attribute to each of the option tags
<option id="sizeS" value="https://www.webpage.com/01">Size S</option>
<option id="sizeM" value="https://www.webpage.com/02">Size M</option>
add a name attribute to the body-tag of each page:
For example, on the page https:.../01:
<body name="S">
add this function to the JavaScript part of each page:
function updateForm() {
var currentSize = document.getElementsByTagName['body'][0].getAttribute('name');
document.getElementById("size" + currentSize).selected = "selected";
}
execute the function updateForm() when loading the page. For example, with onload:
<body name="M" onload="updateForm()">
you can also use an event listener with the "DOMContentLoaded" event, click here for more information:
I am working on a piece of code that will display the content of a mysql query in a template, since the query could feasibly return hundreds of results Ive included a page limiter and navigation tool. Unfortunately I am having a problem passing the page number after I select the number of results to show per page. The page nav is separate and working fine except it cant see the page number once this function has run.
<script>
function refreshpage(results_per_page){
window.location="classifieds.php?&results_per_page="+results_per_page
}
</script>
<?php
$results_per_page = $_GET['results_per_page'];
?>
<select onchange="refreshpage(this.value);">
<option selected value="<?php echo $results_per_page?>"><?php echo $results_per_page?></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
The above code works fine and automatically reloads the page, but when ever i try to introduce a $page variable the pageRefresh function fails to work. Is this the right way to go about what I am after or is there another way that will let me include another variable. All it needs to be is;
$page;
taken from the url
$page = $_GET['page'];
Ive tried sending it with the onchange function, calling it from within the refreshPage Function, even using an intermediate function to assemble an extension with all the relevant data and nothing seems to give the result.
Thanks
EDIT:
Thanks to sasse, I got enough Info to correct the code I believe. This seems to now work so if anyone else has had this issue, this seems to be a good work around. Thanks! (never occurred to me to use val!)
<script>
function refreshpage(results_per_page){
var page = <?php echo $_GET['page'] ?>;
window.location="classifieds.php?page=" + page + "&results_per_page="+results_per_page
}
</script>
<?php
$results_per_page = $_GET['results_per_page'];
?>
<select onchange="refreshpage(this.value);">
<option selected value="<?php echo $results_per_page?>"><?php echo $results_per_page?></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
Since you're calling the "refreshPage" with onchange for the "select"-element and sending "this.value" you will send the selected value to the method.
Therefore you will send 1, 2, 3, 4 or 5 to the refreshPage method as parameter "results_per_page".
I would think you don't want one result per page, but I might be wrong.
When are you setting either "results_per_page" or "page"? My thought was that the "select"-element was for the "page" attribute and therefore the method "refreshPage" should accept "page" as parameter.
I'll write some code to demonstrate my intention.
<script>
function refreshpage(page){
var results_per_page = <?php echo $_GET['results_per_page'] ?>;
window.location="classifieds.php?page=" + page + "&results_per_page="+results_per_page
}
</script>
<select onchange="refreshpage(this.value);">
<!-- If this select is for "page" then I don't understand this next line -->
<option selected value="<?php echo $results_per_page?>"><?php echo $results_per_page?></option>
<option value="1">1</option>
<option value="2">2</option>
...
</select>
If the case is that I didn't understand correctly then tell me what I thought wrong and I'll update my answer. And also, update your question with what you've tried so far as long as the "page" goes, when is it set? Do you have a "next" and a "previous" button?
hey guys i am building a form in which i want when a user select an option from the dropdown menus a text to be displayed and calculate the price.
The dropdown menu is:
<select id="recipient" name="recipient" tabindex="6" class="selmenu">
<option value="staff">Site Staff</option>
<option value="editor">Editor-in-Chief</option>
<option value="technical">Tech Department</option>
<option value="pr">Public Relations</option>
<option value="support">General Support</option>
</select>
and the other dropdown menu is:
<select id="recipient" name="recipient" tabindex="6" class="selmenu">
<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>
<option value="four">4</option>
<option value="five">5</option>
</select>
What i want to do is when the user selects something from the first and the second menu i want a textfield to change dynamically... can anyone point me the way?
only ajax/jquery could do that in combination of php step might be...
onchange of any dropdown make a ajax request to php script.
return the json response from php script
use this response to either manipulate your form or display data on page.
you don't need to use php for this, it can all be done with jquery/javscript
an example using jquery is below (i had to rename your second select as you had re-used an ID):
$('.selmenu').change(function() {
$('#output').text($('#recipient option:selected').text() + ' ' + $('#recipient2 option:selected').text());
})
output is the id of your textbox.
on a select value being changed, output will be filled with the selected value in both select controls
I am trying to set value from php the option selected in a drop down list in html.
can anyone tell me how to give the "selected" attribute as the answer is generated dynamically
<select form="onsite_assessment" class="form-control" id="onsite_assessment">
<option value="no">NO</option>
<option value="yes">YES</option>
</select>
In the above code if the user has selected No in the previous session. i want to display that option by default.
This is just some general code which can be shaped for your needs. As I can't see all your code I cannot give a FULL answer. Basically you need to store the users' choice and retrieve this in a variable whenever needed, either string or Boolean (true/false). In the example I will show this using a string.
<?php
$user_choice = 'yes';
?>
<select form="onsite_assessment" class="form-control" id="onside_assessment">
<option value="no" <?php if($user_choice == 'no'){echo 'selected'; }>NO</option>
<option value="yes" <?php if($user_choice == 'yes'){echo 'selected'; }>YES</option>
</select>