Here i want the div to be shown if the specified value is present or selected and the div should be hide if the specified value is not selected.
here is my code
<select name="result" id="result" class="form-control select2" style="width: 70%" multiple>
<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>
<option value="q">Q</option>
</select>
here is html
<div class="form-group" id="qaranty_count_full">
<label for="recipient-name" class="col-form-label">Count:</label>
<input type="number" required name="qaranty_count" class="form-control" id="qaranty_count" value="1">
</div>
Here is the script
<script type="text/javascript">
$(document).ready(function(){
$('#qaranty_count_full').hide();
$("#result").change(function(){
if($('option:selected', this).val()=='q')
{
$('#qaranty_count_full').show();
$('#qaranty_count').prop('required', true);
}
else
{
$('#qaranty_count_full').hide();
$('#qaranty_count').prop('required', false);
}
});
});
</script>
in this case if i choose q first time the div becomes visible and if i choose q as second or third value the div is not becoming visible,the div should remain visible if q is selected,hope understood my problem
You can try this code.
$(document).ready(function(){
$("#result").change(function(){
if($.inArray("q", $(this).val()) !== -1){
$('#qaranty_count_full').show();
$('#qaranty_count').prop('required', true);
}else{
$('#qaranty_count_full').hide();
$('#qaranty_count').prop('required', false);
}
});
});
Here is a codepen I have created.
https://codepen.io/smitraval27/pen/BrpPPN
You could do like this, using .each, and loop the selected array.
Stack snippet
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="result" id="result" class="form-control select2" style="width: 70%" multiple>
<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>
<option value="q">Q</option>
</select>
<div class="form-group" id="qaranty_count_full">
<label for="recipient-name" class="col-form-label">Count:</label>
<input type="number" required name="qaranty_count" class="form-control" id="qaranty_count" value="1">
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#qaranty_count_full').hide();
$("#result").change(function(){
$('#qaranty_count_full').hide();
$('#qaranty_count').prop('required', false);
$('option:selected', this).each(function(i,sel){
if ($(sel).val() == 'q') {
$('#qaranty_count_full').show();
$('#qaranty_count').prop('required', true);
}
});
});
});
</script>
Related
How to get the value id from datalist and display title?
<input list="A" name="states" class="form-control states" placeholder="Select your state ..">
<datalist id="A" >
<?php
$sql_states = $DB_con->prepare("SELECT * FROM US_STATES ORDER BY ID ");
$sql_states->execute();
while($row_states=$sql_states->fetch(PDO::FETCH_ASSOC))
{
?>
<option value="<?php echo $row_states['ID'].$row_states['STATE_NAME'];?>" ></option>
<?php
}
?>
</datalist>
Make a custom attribute in options called data-id and you can get the id with help of jquery. lets the following code.
<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
<input list="browsers" id="input">
<datalist id="browsers">
<option data-id="op1" value="Internet Explorer">
<option data-id="op2" value="Firefox">
<option data-id="op3" value="Chrome">
<option data-id="op4" value="Opera">
<option data-id="op5" value="Safari">
</datalist>
<input type="text" id="selectedBrowser" />
<script type="text/javascript">
$(function(){
$('#input').change(function(){
var abc = $("#browsers option[value='" + $('#input').val() + "']").attr('data-id');
$('#selectedBrowser').val(abc)
alert(abc);
});
});
</script>
Before anyone yells Repost, there aren't many cases like mine that I've seen.I have a contact form that has the possibility to have a main Select option, a sub-Select option and then a sub-Select option of that one. Overall, There are 27 options to choose from and I think I could make the php give me all 27 of those and then do a "yes they selected this one but not that one" type of report, but I digress.How do I have the php script for my contact form send only what is selected from a possible three-tiered select menu? EDIT: I'm totally willing to use something other than PHP to help facilitate this, I just need help with the PHP script as well.Edit: To be clear, I'm try to create and populate a php script, not valdiate the form/check that all required sections are filled out.Code
<form action="sending.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Name:<br>
<input name="name" type="text" value="" size="30"/><br>
Email:<br>
<input name="email" type="text" value="" size="30"/><br>
Service:<br>
<select name="service" id="service" class="service">
<option>Select a Service</option>
<option value="screen" data-target="devices" id="screen">Screen Replacement</option>
<option value="comp" data-target="comp" id="comp">Computer Work</option>
<option value="misc" data-target="misc" id="misc">Miscellaneous</option>
</select>
<div style="display:none" id="service-devices">
<select name="devices" id="devices" class="devices">
<option>Select a Device</option>
<option value="iphone" data-target="iphones" id="iphone">iPhone</option>
<option value="ipad" data-target="ipads" id="ipad">iPad</option>
<!--<option value="watch" id="watch">Apple Watch</option> -->
<option value="android" id="android">Android</option>
</select>
<div style="display:none" id="devices-iphones">
<select name="iphone" id="iphone" class="iphone">
<!--<option value="iphone6s" id="iphone6s">iPhone 6S</option> -->
<!--<option value="iphone6splus" id="iphone6splus">iPhone 6S Plus</option>-->
<option>Select a Model</option>
<option value="iphone6" id="iphone6">iPhone 6</option>
<option value="iphone6plus" id="iphone6plus">iPhone 6 Plus</option>
<option value="iphone5s" id="iphone5s">iPhone 5S</option>
<option value="iphone5c" id="iphone5c">iPhone 5C</option>
<option value="iphone5" id="iphone5">iPhone 5</option>
<option value="iphone4s" id="iphone4s">iPhone 4S</option>
<option value="iphone4" id="iphone4">iPhone 4</option>
</select>
</div>
<div style="display:none" id="devices-ipads">
<select name="ipad" id="ipad" class="ipad">
<option>Select a Model</option>
<option value="ipadmini3" id="ipadmini3">iPad Mini 3</option>
<option value="ipadmini2" id="ipadmini2">iPad Mini 2</option>
<option value="ipadair" id="ipadair">iPad Air</option>
<option value="ipad4" id="ipad4">iPad 4</option>
<option value="ipad3" id="ipad3">iPad 3</option>
<option value="ipadmini" id="ipadmini">iPad Mini</option>
<option value="ipad2" id="ipad2">iPad 2</option>
</select>
</div>
</div>
<div style="display:none" id="service-comp">
<select name="compwork" id="compwork" class="compwork">
<option value="desktopcreation" id="desktopcreation">Desktop Creation</option>
<option value="desktopbuild" id="desktopbuild">Desktop Build</option>
<option value="hardwareupgrades" id="hardwareupgrades">Hardware Upgrades</option>
<option value="datarecovery" id="datarecovery">Data Recovery/Transfer</option>
<option value="spywareremoval" id="spywareremoval">Spyware/Adware Removal</option>
<option value="virusremoval" id="virusremoval">Virus Removal</option>
</select>
</div>
<div style="display:none" id="service-misc">
<select name="miscellaneous" id="miscellaneous" class="miscellaneous">
<option value="networksecurity" id="networksecurity">Network Security</option>
<!--<option value="webdesign" id="webdesign">Website Design</option>-->
</select>
</div><br>
Message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Submit Request"/>
</form>
I assume that you do have jquery to display the relevant select boxes upon selection of devices etc... I have made a few changes in your html form, like adding id="form" in your form tag. Also added value="" for options like "Please select Service".. Here is the code. Hope this helps.. Refer to this as well..
Jquery form submit to check empty fields
<form id="form" action="sending.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Name:<br>
<input name="name" type="text" value="" size="30"/><br>
Email:<br>
<input name="email" type="text" value="" size="30"/><br>
Service:<br>
<select name="service" id="service" class="service">
<option value="">Select a Service</option>
<option value="screen" data-target="devices" id="screen">Screen Replacement</option>
<option value="comp" data-target="comp" id="comp">Computer Work</option>
<option value="misc" data-target="misc" id="misc">Miscellaneous</option>
</select>
<div style="display:none" id="service-devices">
<select name="devices" id="devices" class="devices">
<option value="">Select a Device</option>
<option value="iphone" data-target="iphones" id="iphone">iPhone</option>
<option value="ipad" data-target="ipads" id="ipad">iPad</option>
<!--<option value="watch" id="watch">Apple Watch</option> -->
<option value="android" id="android">Android</option>
</select>
<div style="display:none" id="devices-iphones">
<select name="iphone" id="iphone" class="iphone">
<!--<option value="iphone6s" id="iphone6s">iPhone 6S</option> -->
<!--<option value="iphone6splus" id="iphone6splus">iPhone 6S Plus</option>-->
<option value="">Select a Model</option>
<option value="iphone6" id="iphone6">iPhone 6</option>
<option value="iphone6plus" id="iphone6plus">iPhone 6 Plus</option>
<option value="iphone5s" id="iphone5s">iPhone 5S</option>
<option value="iphone5c" id="iphone5c">iPhone 5C</option>
<option value="iphone5" id="iphone5">iPhone 5</option>
<option value="iphone4s" id="iphone4s">iPhone 4S</option>
<option value="iphone4" id="iphone4">iPhone 4</option>
</select>
</div>
<div style="display:none" id="devices-ipads">
<select name="ipad" id="ipad" class="ipad">
<option value="">Select a Model</option>
<option value="ipadmini3" id="ipadmini3">iPad Mini 3</option>
<option value="ipadmini2" id="ipadmini2">iPad Mini 2</option>
<option value="ipadair" id="ipadair">iPad Air</option>
<option value="ipad4" id="ipad4">iPad 4</option>
<option value="ipad3" id="ipad3">iPad 3</option>
<option value="ipadmini" id="ipadmini">iPad Mini</option>
<option value="ipad2" id="ipad2">iPad 2</option>
</select>
</div>
</div>
<div style="display:none" id="service-comp">
<select name="compwork" id="compwork" class="compwork">
<option value="desktopcreation" id="desktopcreation">Desktop Creation</option>
<option value="desktopbuild" id="desktopbuild">Desktop Build</option>
<option value="hardwareupgrades" id="hardwareupgrades">Hardware Upgrades</option>
<option value="datarecovery" id="datarecovery">Data Recovery/Transfer</option>
<option value="spywareremoval" id="spywareremoval">Spyware/Adware Removal</option>
<option value="virusremoval" id="virusremoval">Virus Removal</option>
</select>
</div>
<div style="display:none" id="service-misc">
<select name="miscellaneous" id="miscellaneous" class="miscellaneous">
<option value="networksecurity" id="networksecurity">Network Security</option>
<!--<option value="webdesign" id="webdesign">Website Design</option>-->
</select>
</div><br>
Message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Submit Request"/>
<script src="js/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function() {
$('select').change(function(){
var boxid = $(this).children('option:selected').attr('data-target');
alert(boxid);
if(boxid == "devices") {
var divId = "service-" + boxid;
}
document.getElementById(divId).style.display = "block";
});
$("form").submit(function(){
// $(':input[value=""]').attr('disabled', true);
$("input[type='text'],select,input[type='password'],textarea",this).each(function() {
if($(this).val().trim() == "") {
$(this).attr('disabled', true);
}
})
});
});
</script>
I want to display a simple html drop down box and selected value want to print.
<select name="v">
<option value="fkt">Flipkart</option>
<option value="snd">Snapdeal</option>
</select>
When the flipkart is selected php echo should print "Flipkart".
When the snapdeal is selected php echo should print "Snapdeal".
Use jQuery
$(document).ready(function(){
$(".showDescriptionTextbox").val($(".Product").val());//to initialize
$(".showDescriptionDiv").text($(".Product").val());//to initialize
$(".Product").change(function(){
$(".showDescriptionTextbox").val($(".Product").val());
$(".showDescriptionDiv").text($(".Product").val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="v" class="Product" id="dropdown">
<option value="fkt">Flipkart</option>
<option value="snd">Snapdeal</option>
</select>
<br/>
<br/>
value in textarea: <input type="text" class="showDescriptionTextbox">
<div>
or you can display value in div: <span class="showDescriptionDiv"></span>
</div>
<select name="v" class='Product'>
<option value="fkt">Flipkart</option>
<option value="snd">Snapdeal</option>
</select>
<textarea class='showDescription'></textarea> <br>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(function(){
$('.Product').change(function(){
var Product = $('.Product').val();
$(".showDescription").val(Product);
});
});
</script>
Check for it.
function shop(shop)
{
$("#show").val(shop);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select onchange="shop(this.value)">
<option value="Flipkart">Flipkart</option>
<option value="Snapdeal">Snapdeal</option>
</select>
<br/>
<br/>
<input type="text" id='show'>
I'm creating a system which provide a report generator, now my problem is i dont know how to call data with the variables that i want. For example I only want the data from January // 2014 // from the account department and so on. Now, i don't know what code to show but below is the menu where user select their variables
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
function showForm() {
var selopt = document.getElementById("opts").value;
if (selopt == "Tenaga Nasional Berhad") {
document.getElementById("f1").style.display = "block";
document.getElementById("f2").style.display = "none";
}
if (selopt == "Telekom Malaysia") {
document.getElementById("f2").style.display = "block";
document.getElementById("f1").style.display = "none";
}
if (selopt == 0) {
document.getElementById("f2").style.display = "none";
document.getElementById("f1").style.display = "none";
}
}
</script>
<div id="contact_form">
<h4>Pilih Ketetapan Laporan</h4>
<form method="post" name="pilih_kategori_daftar">
<label for="daerah">Daerah:</label>
<select class="required input_field" name="daerah" id="daerah" required/>
<option value="">--Pilih Daerah--</option>
<option value="Kuantan">Kuantan</option>
<option value="Maran">Maran</option>
<option value="Bentong">Bentong</option>
<option value="Raub">Lipis</option>
<option value="Jerantut">Jerantut</option>
<option value="Cameron Highlands">Cameron Highlands</option>
<option value="P.Tenggara">Pahang Tenggara</option>
<option value="Pekan">Pekan</option>
<option value="Rompin">Rompin</option>
<option value="Temerloh">Temerloh</option>
<option value="Bera">Bera</option>
<option value="Ibu Pejabat">Ibu Pejabat</option>
</select>
<div class="cleaner_h10"></div>
<label for="bulan_bill">Bulan</label>
<select class="required bill_caj" name="bulan_bill" id="bulan_bill" width="300px" required/>
<option value="">--Pilih Bulan--</option>
<option value="Januari">Januari</option>
<option value="Februari">Februari</option>
<option value="Mac">Mac</option>
<option value="April">April</option>
<option value="Mei">Mei</option>
<option value="Jun">Jun</option>
<option value="Julai">Julai</option>
<option value="Ogos">Ogos</option>
<option value="September">September</option>
<option value="Oktober">Oktober</option>
<option value="November">November</option>
<option value="Disember">Disember</option>
</select>
<div class="cleaner_h10"></div>
<label for="tahun_bill">Tahun</label>
<input type="text" maxlength="4" size="4" id="tahun_bill" name="tahun_bill" class="required year_field" placeholder="Tahun" onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')" required/>
<div class="cleaner_h10"></div>
<label for="kategori_akaun">Kategori</label>
<select id="opts" onchange="showForm()" class="input_field" required>
<option value="0">--Pilih Kategori--</option>
<option value="Tenaga Nasional Berhad">Akaun TNB</option>
<option value="Telekom Malaysia">Akaun TM</option>
</select>
<div class="cleaner_h10"></div>
<div id="f1" style="display:none">
<form name="akaun_tnb">
<label for="jenis">Jenis Akaun</label>
<select id="opts" onchange="showForm()"class="input_field">
<option value="0">--Pilih Jenis Akaun--</option>
<option value="Rumah Pam">Rumah Pam</option>
<option value="Loji Air">Loji Air</option>
<option value="Stor">Stor</option>
</select>
</form>
</div>
<div id="f2" style="display:none">
<form name="akaun_tm">
<label for="jenis">Jenis Akaun</label>
<select id="opts" onchange="showForm()"class="input_field">
<option value="0">--Pilih Jenis Akaun--</option>
<option value="Telefon">Telefon</option>
<option value="Telefon/Streamyx">Telefon/Streamyx</option>
<option value="Streamyx">Streamyx</option>
<option value="SMS Blast">SMS Blast</option>
<option value="TM NET">TM NET</option>
</select>
</form>
</div>
</form>
</div>
search in google:
1, How to create mysqli database, tables, columns.
2, PHP, how to connect, extract, insert data from mysqli using php,
3. How to echo mysqli data into html, 4. how to submit form to php and insert data to mysqli, 5. Learn to use AJAX & JSON...this you may leave for few months:)
I have few select dropdown working nicely with selectize.js. On form post, it appears it only send the last option selected. So for example, if the user select's: A, B, C in the form post var_dump($_POST); only C appears. Same is the case with other select.
I went through the documentation and searched online but not able to rectify, I am sure its something really trivial but till I know, I don't know :(
Would greatly appreciate any help! Thank you!
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="http://brianreavis.github.io/selectize.js/js/selectize.js"></script>
<?php
if(isset($_POST["save"])){
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
?>
<form action="" method="post">
<label>Select Class(s)</label>
<select multiple id="classname" name="classname[]" required class="form-control">
<option value="1">NUR</option>
<option value="2">KG</option>
<option value="3">PREP</option>
<option value="4">I</option>
<option value="5">II</option>
</select>
<label>Subjects</label>
<select multiple id="subjects" name="subjects[]" required class="form-control">
<option value="1">English</option>
<option value="2">EVS</option>
<option value="3">Maths</option>
<option value="4">Science</option>
<option value="5">Social</option>
</select>
<label>Examinations</label>
<select multiple="multiple" id="exams" name="exams[]" required="true" class="form-control">
<option value="1">Formative Assessment I</option>
<option value="2">Formative Assessment II</option>
<option value="3">Formative Assessment III</option>
<option value="4">Formative Assessment IV</option>
<option value="5">Annual</option>
</select>
<input type="submit" name="save" value="save" />
</form>
<script>
$(document).ready(function () {
$('#exams').selectize({
hideSelected: 'true'
});
$('#subjects').selectize({
hideSelected: 'true'
});
$('#classname').selectize({
hideSelected: 'true'
});
});
</script>