I have a question about the "select" function. I have two input "select" functions. When I select the first input option, then I select the second input option. If I go back to the first input option to change other options, how can the second one automatically turn into "Please Select"word?
Below is my coding:
<label for="cp8" class="control-label col-lg-4">Merchant</label>
<div class="col-lg-5">
<select class="form-control required" id="branch_id3" name="branch_id3">
<option value="" selected>Please Select</option>
<!--<option value="0" selected>All</option>-->
<?php
$sql_branch = 'SELECT * FROM merchant_list;';
$arr_branch = db_conn_select($sql_branch);
foreach ($arr_branch as $rs_branch) {
echo '<option value="' . $rs_branch['id'] . '">' . $rs_branch['shop_name'] . '</option>';
}
?>
</select>
</div><br><br><br>
<label for="cp8" class="control-label col-lg-4">Merchant Types</label>
<div class="col-lg-5">
<select class="form-control required" id="merchant_type" name="merchant_type">
<option value="" selected>Please Select</option>
<option value="1">Event Sponsored</option>
<option value="2">Normal Merchant</option>
</select>
</div><br><br><br>
Below is my output picture:
Output
For example:
First I choose the Merchant option is "PALVO", then I choose the Merchant Types is "Event Sponsored". After that, if I want go back the Merchant to change other option, the Merchant Types will automatically turn to "Please Select" option. So that, my question is how to automatically make Merchant Types turn to "Please Select" option? Thanks for helping.
i hope this is what you looking for
$('#option').change(function(){
$('#type').val(0);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="option">
<option>Please Select</option>
<option>Option</option>
<option>Option1</option>
<option>Option2</option>
</select>
<select id="type">
<option value="0">Please Select</option>
<option>Type</option>
<option>Type1</option>
<option>Type2</option>
</select>
You can use jquery change for manipulating DOM element.
If you do not want to disable second select you can remove diabled attribute from select tag.
And also remove this from jquery code $("#select2").removeAttr('disabled');
$(document).ready(function(){
$("#select1").on('change',function(){
$("#select2").val('Please Select');
$("#select2").removeAttr('disabled');
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Select Value:
<select id="select1">
<option>Please Select</option>
<option>value1</option>
<option>value2</option>
<option>value3</option>
</select>
<select id="select2" disabled>
<option>Please Select</option>
<option>value1</option>
<option>value2</option>
<option>value3</option>
</select>
Use .val() on select DOM , and give option's value it will be selected.
With this check what is selected in first select then use .val()
$("#events").click(function(){
if($("#events option:selected").text() == "Reset First Select"){
$("#merchants").val('Please Select');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Merchants:
<select id="merchants">
<option>Please Select</option>
<option>Saab</option>
<option>Mercedes</option>
<option>Audi</option>
</select><br>
Event:
<select id="events">
<option>Reset First Select</option>
<option selected>Saab</option>
<option>Mercedes</option>
<option>Audi</option>
</select><br>
EDIT:
I have just added that when in second option reset first option will be selected it will select first
Related
Is there any way that the value from select option to be auto-inserted into a textarea? (in php)
Option Select:
<select id="Branch">
<option value="MCG">MCG</option>
<option value="KB">KB</option>
<option value="KT">KT</option>
</select>
TextArea
<input name="street" type=text value=>
Sorry in advance if I didn't ask the question correctly
It is not possible using PHP at a time on change select but,
You can select option to be auto-inserted into a textarea using javascript like blow example
function updateTextareaValue(selectVal){
document.getElementById("street").value = selectVal;
}
<html>
<body>
<select id="Branch" onchange="updateTextareaValue(this.value);">
<option value="MCG">MCG</option>
<option value="KB">KB</option>
<option value="KT">KT</option>
<option value="XX">XX</option>
<option value="YY">YY</option>
</select>
<textarea id="street"></textarea>
</body>
</html>
I have issues posting my select option values from html form to php back-end
Here are my codes:
<form action="submit.php" method="POST">
<select name="approver" class="form-control" autofocus>
<option selected disabled value="0">— Select Approver —</option>
<option selected disabled value="1">User A</option>
<option selected disabled value="2">User B</option>
</select>
</form>
submit.php:
$approver = mysqli_real_escape_string($conn, $_POST['approver']);
Is there a way for php to receive the value of the option, eg. 1, instead of the text inside the option tag?
Thanks in advance
"selected disabled" attributes should be used for the first option(generally which is not really an option for the user to select). It is used so that the user cannot choose the first option again after changing the option. Remove the "selected disabled" attributes from other options.
<select name="approver" class="form-control" autofocus>
<option selected disabled value="0">— Select Approver —</option>
<option value="1">User A</option>
<option value="2">User B</option>
</select>
I don't want it as a label.
Something like this:
But this 'value' should go away as soon as I click on it once and the actual values should start showing.
$('select').change(function() {
if ($(this).children('option:first-child').is(':selected')) {
$(this).addClass('ddplaceholder');
} else {
$(this).removeClass('ddplaceholder');
}
});
.ddplaceholder{color: grey;}
select option:first-child{color: grey; display: none;}
select option{color: #555;} // bootstrap default color
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<select class="form-control ddplaceholder">
<option value="">Type your own text</option>
<option value="1">example 1</option>
<option value="2">example 2</option>
<option value="3">example 3</option>
</select>
</form>
You need to add some jQuery/See the above example.This is what you want.
You can use script in addition to this. You can use remove first child on mouse hover using jquery.
You can disable and pre select the default value. Then when user clicks cant re select this option.
<option selected="true" disabled="disabled">
Just create empty option's
<option value="" disabled selected>Select Category</option>
<option value="" disabled selected>Select Item</option>
<option value="" disabled selected>Select Vencdor</option>
I want to display a number of textboxes in a form based on the selected option from a dropdown listbox.
For example, if user selects 1 then 1 textbox should be shown and if user selects 2 then 2 textboxes should be displayed. And I need to do it in PHP.
I found some answers using jQuery. Can we use jQuery inside PHP? If yes, then how?
Edit
#Edwin Alex
This is how my select option looks like.
<h2><u>DEPENDENT DETAILS</u></h2><br />
<table border="1" style="border-style:dotted" width="100%" id="dependenttable">
<tr><td>No of Dependent</td><td><select name="numDep" id="dropdown">
<option value="">Please Select</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option></select></td></tr>
<tr id="textboxDiv"></tr>
At the end of file inside <> these I have written your code.
You can use Jquery to get this. Try this,
HTML :
<tr><td>No of Dependent</td><td><select name="numDep" id="dropdown">
<option value="">Please Select</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option></select></td>
</tr>
<tr id="textboxDiv"></tr>
Jquery :
<script type="text/javascript">
$(document).ready(function() {
$("#dropdown").change(function() {
var selVal = $(this).val();
$("#textboxDiv").html('');
if(selVal > 0) {
for(var i = 1; i<= selVal; i++) {
$("#textboxDiv").append('<input type="text" name="textVal[]" value="" />');
}
}
});
});
</script>
Paste this code at the bottom of your page inside tag.
Your select box ID should be dropdown.
You need to have a div with an ID textboxDiv to place your generated textboxes.
I think you can do it easily with the help of JavaScript. Here is an example of it. Try it and modify it according to your requirement
<html>
<head>
<title>Select DIV to show</title>
<script type="text/javascript">
function show(obj) {
no = obj.options[obj.selectedIndex].value;
count = obj.options.length;
for(i=1;i<count;i++)
document.getElementById('myDiv'+i).style.display = 'none';
if(no>0)
document.getElementById('myDiv'+no).style.display = 'block';
}
</script>
</head>
<body>
<form name="myForm">
<select onChange="show(this)">
<option value="0">Select</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</form>
<div id="myDiv1" style="display:none"> <form>
<select>
<option value="0">Select</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</form></div>
<div id="myDiv2" style="display:none"><form><input type="text" ><br>
<input type="text"/><br>
<input type="submit"/></form></div>
<div id="myDiv3" style="display:none"><form><input type="text" ><br>
<input type="text"/><br>
<input type="submit"/></form></div>
</body>
</html>
In this example just change the code in "myDiv1", "myDiv2", "myDiv3". I think it will hep you.:)
I have a html form defined . I have a button for the user to select his occupation . If his occupation happens to be a student then i need to generate another new button dynamically . If the user selects anything other than a student then i dont want any new button . I am unsure how to do . Do i use jquery ? I have no idea to use jquery . Can some one help ? Any pointers
Thanks.
<?php
?>
<script language="javascript" type="text/javascript">
$(document).ready(function()
{
$('#occupation').change(function()
{
if($(this).val() == 'Student')
{
$('#school').show();
} else {
$('#school').hide();
}
});
});
</script>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
<form>
<select name="occupation" id="occupation">
<option value="">- Select Occupation -</option>
<option value="Worker">Worker</option>
<option value="Student">Student</option>
</select>
<select name="school" id="school" style="display: none;">
<option value="">Select School Type</option>
<option value="4 Year">4 Year</option>
<option value="2 Year">2 Year</option>
</select>
</form>
</body>
</html>
Personally I accomplish this by placing the additional form element in the HTML, simply hidden like so:
<select name="occupation" id="occupation">
<option value="">- Select Occupation -</option>
<option value="Worker">Worker</option>
<option value="Student">Student</option>
</select>
<select name="school" id="school" style="display: none;">
<option value="">Select School Type</option>
<option value="4 Year">4 Year</option>
<option value="2 Year">2 Year</option>
</select>
Then you can use JS, as I prefer jQuery, to show the div or hide the div when applicable:
$(document).ready(function()
{
$('#occupation').change(function()
{
if($(this).val() == 'Student')
{
$('#school').show();
} else {
$('#school').hide();
}
});
});
Using jQuery would simplify this:
Let's say you have the following:
<form>
<select name="occupation">
<option value="Non-Student">Non-Student</option>
<option value="Student">Student</option>
</select>
</form>
Then the following would be used to append the button if the user selected 'Student':
$(function(){
$('select[name="occupation"]').change(function(){
var val = $(this).val();
if(val=='Student'){
$('form').append('<button>New Button</button>');
}
});
});