set function onchange in dropdown - PHP [duplicate] - php

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
I have a dropdown list and when when user selects a value and a different forms will appear depends on what the selected value. My file is in html. I'm using bootstrap for my design.
here is my sample code:
<form name="exmType" method = "POST">
<select class="form-control" name="txtType" class="select" method = "post"
onChange="this.form.submit();" style="width:300px;">
<option value="">Option A</option>
<option value="1">Option B</option>
<option value="2">Option C </option>
<?php
if (isset($_POST['txtType'])) {
if ( $_POST['txtType'] == "" ){
*display form a*
} elseif ( $_POST['txtType'] == 1 ){
*display form b*
} elseif ( $_POST['txtType'] == 2 ){
*display form c*
}
}
?>
</select>
</form>

Here is a simple code example for what you need, you can fiddle with it for your needs.
The code is really simple so I don't see any reason to elaborate on what it does, if you have any questions post a comment.
$(document).ready(function() {
$('select').change(function(){
if($(this).val() == '0') {
$('form').css('display','none');
}
if ($(this).val() == '1') {
$('form').css('display','none');
$('#form_a').css('display','block');
}
if($(this).val() == '2') {
$('form').css('display','none');
$('#form_b').css('display','block');
}
if($(this).val() == '3') {
$('form').css('display','none');
$('#form_c').css('display','block');
}
});
});
form {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option value="0">-- Choose Form --</option>
<option value="1">Form A</option>
<option value="2">Form B</option>
<option value="3">Form C</option>
</select>
<form id="form_a" method="post" role="form">
Form a: <input type="text" value="form_a_text"/>
</form>
<form id="form_b" method="post" role="form">
Form b: <input type="text" value="form_b_text"/>
</form>
<form id="form_c" method="post" role="form">
Form c: <input type="text" value="form_c_text"/>
</form>

You should try using Jquery:
<script>
$('form select[name=txtType]').change(function(){
if ($('form select option:selected').val() == '1'){
$('#optionA').show();
}else{
$('#optionA').hide();
}
});
$('form select[name= txtType]').change(function(){
if ($('form select option:selected').val() == '2'){
$('#optionB').show();
}else{
$('#optionB ').hide();
}
});
</script>
You can find the Jquery: http://jsfiddle.net/ecZrE/
Some good answers:
Show Form Field if Drop Down Item is Selected Using jquery
Javascript: Trying to show different forms based on drop down value?
Drop down menu selection ditactes what form will display
Show different elements of a form depending on dropdown box selection

First of all your select tag has two class=. Choose the right one.
`<select class="form-control" name="txtType" class="select" method = "post"
onChange="this.form.submit();" style="width:300px;">
<option value="">Option A</option>
<option value="1">Option B</option>
<option value="2">Option C </option>`
To add the onClick or onChange event use below function. Before adding it would be better if you add an id to the select tag.
$('#select_tag_id').change(function(){
$('#form_id_to_display').show('slow');
$('#form_id_to_hide').hide('slow');
});
Add above code in head section or at the botton of the page.
Sorry for wrong alignments. posting from mobile phone. Editing option is limited.

Related

Get select option value from HTML with php to disable input field

I use select as below:
<select name="account_type" required>
<option value="">Choose Account Type</option>
<option value="1">Asset</option>
<option value="2">Bank</option>
<option value="3">Capital</option>
<option value="4">Cash</option>
<option value="5">Expense</option>
<option value="6">Income</option>
<option value="7">Liability</option>
</select>
Now I want to catch those option values using php variable, and here is the important part: I will have an input field, and based on the values I want to enable/disable that input filed.
How can I do that?
Using jQuery give your select an id like below
<select name="account_type" id="account" required>
<option value="">Choose Account Type</option>
<option value="1">Asset</option>
<option value="2">Bank</option>
<option value="3">Capital</option>
<option value="4">Cash</option>
<option value="5">Expense</option>
<option value="6">Income</option>
<option value="7">Liability</option>
</select>
<!--input to be disabled -->
<input type="text" id="disable">
jQuery
$('#account').on('change', function(){
if($(this).val() === '1') {
$("#disable").prop('disabled', true);
//you can also send data to PHP here using AJAX
//var data = {one: $(this).val()};
//$.post( "test.php", data, function( data ) {
//$( ".result" ).html( data );
//});
}else if($(this).val() === '2') {
//code here
}
//check for other values
});
example
You could do this with jQuery. If you don't know jQuery, check this out: jQuery.com
After you "installed" jQuery, you can use this in your js-File. I hope that you know how to create and use a js-File.
The next thing is to implement a good method for your select-button.
This could look like this in your js-File:
$('#my-select').change(function(){ // "#my-select" is the ID of your select. You need to implement a ID.
//do stuff here, eg.
if ($(this).val() == '5') { //check the selected option etc.
$("input").prop('disabled', true);
} else {
$("input").prop('disabled', false);
}
});
Here's a jsFiddle.
Hope this helps you.
html code
<select>
<option data-id="1">1</option>
<option data-id="2">2</option>
</select>
JQuery code
$(document).ready(function(){
$("select").change(function(){
var display= $("select").val();
if(display == 1){
$("select").attr("disabled", 'disabled');
}
});
});
I am not sure what you want exactly.check out this may be it will be helpful for you JSFiddle

Prevent user from submitting finish button when a value of a drop down list is not selected

I would like to ask how to prevent the user from submitting the form if the value of a drop down list is not selected. I want a prompt something like "Please select from a drop down list". below is my jquery and html code.
<div class="form-group" id="COPPRooms">
<label class="col-sm-2 control-label">COPP:</label>
<div class="col-sm-10">
<select class="form-control" id='COPPMeetingRoom'>
<option value="1">COPP Board Room</option>
<option value="2">COPP Training Room</option>
<option value="Others">Others</option>
</div>
</div>
if($("#COPPMeetingRoom").val()=='0' && $("#NOPFMeetingRoom").val()=='0' && $("#MOPBMeetingRoom").val()=='0' && $("#NOPBMeetingRoom").val()=='0' && $("#LOPPMeetingRoom").val()=='0'){
}
else{
You could use something like this:
with option:selected you can get the current selected option in your drop down menu and then compare it to what ever you like.
if($("#COPPMeetingRoom option:selected").val() == "0" ){
promt code...
alert("Please select...");
or open a popup...
}
else{
your code...
}
when you are submitting form by submit button there you should check for the value of this select
in the form tag add this on submit attribute as: <form onsubmit="selectValueCheck()">
add this function containing code:
if ($("#COPPMeetingRoom option:selected").val() == "0" ) {
alert("Please select...");
return false
}
else {
return true
}
the value false will not let the form to be submitted
By default the select will have the first option selected when you submit a form , so you need to create a default option that will equal to the select beeing empty
<select class="form-control" id='COPPMeetingRoom'>
<option value="-1">Chose Room</option>
<option value="1">COPP Board Room</option>
<option value="2">COPP Training Room</option>
<option value="Others">Others</option>
</select>
js:
if($('#COPPMeetingRoom option:checked').val() == -1) {
alert('chose a room from the select');
}
demo:https://jsfiddle.net/b5vdvbbj/

make the <select> tag selected display HTML

I'm trying to make appear some HTML code if the selected option is the right one, is it possible?
example:
<select name="ref_rubrique">
<option value="1"> number 1 </option>
<option value="2" selected> number 2 </option>
</select>
and
<?php if( $selected==true ): ?>
<p>hello<p>
<?php else(): ?>
<p><p>
<?php endif(); ?>
When the form is submitted you can check $_REQUEST['ref_rubrique']. You can then compare the value, e.g. 1 == $_REQUEST['ref_rubrique']. Both "1" and "2" are truthy values.
In order for this to happen immediately without refreshing the page, it needs to happen entirely in client-side code (JavaScript). So, let's start with your page elements:
<select name="ref_rubrique">
<option value="1"> number 1 </option>
<option value="2" selected> number 2 </option>
</select>
You'll also need some sort of placeholder for the output. And let's use id attributes so we can easily uniquely identify the elements in question. So let's use this:
<select name="ref_rubrique" id="ref_rubrique">
<option value="1"> number 1 </option>
<option value="2" selected> number 2 </option>
</select>
<div id="ref_output"></div>
For now the output is empty. It will be populated by our JavaScript code when the select element's value changes. So now we need a script code block to attach that functionality to the change event of that element:
<select name="ref_rubrique" id="ref_rubrique">
<option value="1"> number 1 </option>
<option value="2" selected> number 2 </option>
</select>
<div id="ref_output"></div>
<script type="text/javascript">
var selectElement = document.getElementById('ref_rubrique');
var divElement = document.getElementById('ref_output');
selectElement.onchange = function () {
var selectedValue = selectElement.options[selectElement.selectedIndex].value;
if (selectedValue == '1') {
divElement.innerHTML = '<p>hello</p>';
} else if (selectedValue == '2') {
divElement.innerHTML = '<p></p>';
}
};
</script>
So what I've done here is:
Get a reference to each of the HTML elements as JavaScript variables
Bind a function to the change event for the select element, which will...
Get a the selected value of the select element
If that selected value is something, write HTML to the output div element
If it's something else, write other HTML to the div element
You can see a live example of this here.
I did it that way if anyone is looking for my solution:
<select name="ref_rubrique" id="ref_rubrique">
<option value="11">11</option>
<option value="12" selected>12</option>
<option value="13">13</option>
</select>
<div id="ref_output">
<?php
if (isset($_GET["id_rubrique"])){
if($_GET["id_rubrique"]==12){
?>
<p>Your question here !</p>
<p><input name="type" type="radio" value="1" <? if($page_type=='true'){ ?>checked<? } ?>>Oui<input name="type" type="radio" value="0" <? if($page_type=='false'){ ?>checked<? } ?>>Non</p>
<?php
}
}
?>
</div>
<script language="javascript" type="text/javascript">
var selectElement = document.getElementById('ref_rubrique');
var divElement = document.getElementById('ref_output');
selectElement.onchange = function () {
var selectedValue = selectElement.options[selectElement.selectedIndex].value;
if (selectedValue == '12') {
divElement.style.display = "block";
} else if (selectedValue != '12') {
divElement.style.display = "none";
}
};
</script>
Thanks to everyone for the help, bye :)
You probably should use id better than name
<select id="ref_rubrique">
<option value="1"> number 1 </option>
<option value="2" selected> number 2 </option>
</select>
<div id="textPlace"></div>
You can use JQuery to check for the value that has been selected, then get the p or div element and add your text to it.
//check if the selected option has a value of 2, than set the text
if($('#ref_rubrique').val() == 2){
$("#textPlace").html("My text");
}
Here is a live example: Here

Show input box o if value from drop down menu is selected

I'm working on a simple php contact form and struggling to get the "other" selection on the dropdown to show a text field option when it's selected. The form is included onto the page with <?php require_once('includes/contact_form.php'); ?> and the footer is also an include (the footer is where I have added the JS).
But it just wont work...
Here is the Form:
<label>How did you hear about us?</label>
<select name="how" class="selectfield" id="how">
<option value="">Please Select...</option>
<option value="Advertisement">Advertisement</option>
<option value="Care at Home Today">Care at Home Today</option>
<option value="Email-Newsletter">Email/Newsletter</option>
<option value="Facebook">Facebook</option>
<option value="Family-Friend">Family or Friend</option>
<option value="Magazine">Magazine Article</option>
<option value="Twitter">Twitter</option>
<option value="Website-Search Engine">Website/Search Engine</option>
<option value="Other">Other</option>
</select>
<input type='text' id="other" class="hidden" />
<input name="contactus" type="submit" class="submit" id="contactus" value="Submit" />
</form>
Here is the JS
$('#how').change(function(){
var selected_item = $(this).val()
if(selected_item == "other"){
$('#other').val("").removeClass('hidden');
}else{
$('#other').val(selected_item).addClass('hidden');
}
});
Not sure if it is a typo in the question but your dropdown value is "Other" and in your js you are testing for "other". So there is a mismatch. Might be your issue
Here is a working fiddle of your example just changing the "other" to "Other" in your if statement.
DEMO
As coder1984 has suggested though, simply using jquery's .show() and .hide() in your if
statement would also work.
if(selected_item == "Other"){
$('#other').val("").show()
}else{
$('#other').val(selected_item).hide()
}
Your problem is in your string comparison, it should be:
if(selected_item == "Other"){
With the Uppercase O, Tested
Try:
$('#how').change(function(){
var selected_item = $(this).val()
if(selected_item == "Other"){ // 'Other'
$('#other').val('').show(); //show textbox if Other is selected
}else{
$('#other').hide(); //Hide textbox if anything else is selected
}
});

form select object dynamically changing class name with php and javascript?

Having some trouble here with this.
The setup:
A select object with a choice of "Other"
User selects "Other".
Script runs when this specific value is chosen, and dynamically changes the class of input object from "hide" to "show" (hidden and visible with css display).
<p>Select a Grade:</p>
<select name="grade" id="grade" onchange="changeCssClass('otherGrade')">
<option value="No Specified Grade">Please Select</option>
<option value="201">201</option>
...
<option value="Secondary">Secondary</option>
<option value="otherGrade">Other</option>
</select>
<p>If "Other":</p>
<?php
$otherGrade = $_POST['grade'];
if($otherGrade = "otherGrade")
{
echo("<script language='javascript' type='text/javascript'>
function changeCssClass(objInputID)
{
if(document.getElementById(objInputID).className=='hide')
{
document.getElementById(objInputID).className = 'show';
}
else
{
document.getElementById(objInputID).className = 'hide';
}
}
</script>");
}
else
{
echo ("");
}
?>
<input type="text" name="otherGrade" id="otherGrade" class="hide" />
Should I remove the onchange event from the select object? If so, I am at a loss as how to have the script executed.
Any solutions would be greatly appreciated.
The script should always be included in your page, since you want this to run while you make the changes in the dropdown list..
<script language='javascript' type='text/javascript'>
function changeCssClass(objInputID, currentVal, correctVal)
{
if(correctVal == currentVal)
{
document.getElementById(objInputID).className = 'show';
}
else
{
document.getElementById(objInputID).className = 'hide';
}
}
</script>
<p>Select a Grade:</p>
<select name="grade" id="grade" onchange="changeCssClass('otherGrade', this.value, 'otherGrade')">
<option value="No Specified Grade">Please Select</option>
<option value="201">201</option>
...
<option value="Secondary">Secondary</option>
<option value="otherGrade">Other</option>
</select>
<p>If "Other":</p>
<input type="text" name="otherGrade" id="otherGrade" class="hide" />
Live example at : http://www.jsfiddle.net/MVymF/
why are you putting this in the post event? you can just render this as standard javascript as part of the page.
On post , the page will be refreshing , ad on reloading you are not saving the selected value of the select box.
The select box is not keep selected after posting. You have to do that.
>Other
As the value of option change the display hide also gone.
Fix that
If no need to submit the page . use this
http://api.jquery.com/val/
check demo of select

Categories