jQuery dependent fields (reset value), add to MySQL same name attribute fields - php

I have setup a 4phases multiple dependent dropdown fields. I want to select region then country, owned or franchise and finally store name.
My issues:
1) Show/hide works OK but doesn’t reset values when I choose “-”. This mean that I can choose one store, then change my opinion, choose another option from upper field category and choose a second store. The POST will have both values. I want to reset the first value when I choose another option
2) Store names have all name attribute store_name. I can’t managed to make it record correctly on mysql db while I used implode().
jQuery:
$( document ).ready(function() {
$("#region").bind("change",function(){
var selectedValue=$(this).val().toLowerCase();
switch(selectedValue){
case "asia":
$("#country_asia").show();
$("#country_america").hide()
$("#country_europe").hide()
break;
case "america":
$("#country_asia").hide();
$("#country_america").show();
$("#country_europe").hide();
break;
case "europe":
$("#country_asia").hide();
$("#country_america").hide();
$("#country_europe").show();
break;
case "-":
$("#country_asia").hide()
$("#country_america").hide()
$("#country_europe").hide()
break;
}
});
$("#country_europe_dd").change(function()
{
var selectedValue=$(this).val().toLowerCase();
if(selectedValue == "greece")
{
$("#owned_gr").show();
}
else if(selectedValue == "-")
{
$("#owned_gr").hide();
}
})
$("#owned_gr_dd").change(function()
{
var selectedValue=$(this).val().toLowerCase();
if(selectedValue == "own")
{
$("#store_name").show();
$("#store_name_greece_fran").hide();
}
else if(selectedValue == "fran")
{
$("#store_name_greece_fran").show();
$("#store_name").hide();
}
else if(selectedValue == "-")
{
$("#store_name").hide();
$("#store_name_greece_fran").hide();
}
})
});
</script>
Form:
<div class="form-group region required" data-cid="region">
<label class="control-label" for="region">Region</label>
<div class="input-group"><span class="input-group-addon left"><i class="glyphicon glyphicon-pushpin"></i></span>
<select class="select form-control" onChange="displayForm(this)" id="region" name="region">
<option value="-">- Select -</option>
<option value="america">America</option>
<option value="asia">Asia</option>
<option value="europe">Europe</option>
</select>
</div>
</div>
<div id="country_europe" class="form-group country_europe required" style="display: none" data-cid="country_europe">
<label class="control-label" for="country">Country</label>
<div class="input-group"><span class="input-group-addon left"><i class="glyphicon glyphicon-pushpin"></i></span>
<select class="select form-control" id="country_europe_dd" name="country">
<option value="-">- Select -</option>
<option value="cyprus">Cyprus</option>
<option value="france">France</option>
<option value="germany">Germany</option>
<option value="greece">Greece</option>
<option value="iceland">Iceland</option>
<option value="italy">Italy</option>
<option value="netherlands">Netherlands</option>
<option value="norway">Norway</option>
<option value="serbia">Serbia</option>
<option value="switzerland">Switzerland</option>
</select>
</div>
</div>
<div id="owned_gr" class="form-group owned_gr required" style="display: none" data-cid="owned_gr">
<label class="control-label" for="owned_gr">Owned or Franchise ?</label>
<div class="input-group"><span class="input-group-addon left"><i class="glyphicon glyphicon-pushpin"></i></span>
<select class="select form-control" id="owned_gr_dd" name="owned">
<option value="-">- Select -</option>
<option value="own">Owned</option>
<option value="fran">Franchise</option>
</select>
</div>
</div>
<div id="store_name" class="form-group store_name required" style="display: none" data-cid="store_name">
<label class="control-label" for="store_name">Store Name - Greece (Owned)</label>
<div class="input-group"><span class="input-group-addon left"><i class="fa fa-buysellads"></i> </span>
<select class="form-control" name="store_name[]" id="store_name_gr_owned"
data-rule-required="true" >
<option value="-">- Select -</option>
<option value="option1">Option1</option>
<option value="option2">Option2</option>
</select>
</div>
</div>
<div id="store_name_greece_fran" class="form-group store_name_greece_fran required" style="display: none" data-cid="store_name_greece_fran">
<label class="control-label" for="store_name_greece_fran">Store Name - Greece (Franchise)</label>
<div class="input-group"><span class="input-group-addon left"><i class="fa fa-buysellads"></i> </span>
<select class="form-control" name="store_name[]" id="store_name_greece_fran"
data-rule-required="true" >
<option value="">- Select -</option>
<option value="athens">Athens</option>
<option value="crete">Crete</option>
</select>
</div>
</div>
PHP:
$store_name = implode($_POST['store_name']);

EDIT: From your comment below, sounds like solving #1 will also solve #2.
For #1, Whenever you hide an element, set its value to - as well. E.g.
case "asia":
$("#country_asia").show();
$("#country_america").hide();
$("#country_america").val("-");
$("#country_europe").hide();
$("#country_europe").val("-");
break;
Or, using vanilla JS:
document.getElementById("country_europe_dd").value = '-';
For #2, I can't see anything wrong with your code. Can you elaborate? Maybe include sample output of what you're seeing for $store_name?

I found the solution.
I have to go one step further on jQuery and use val(). I reset other category values when i select specific values on the field i choose
$("#store_name_greece_fran_dd").change(function()
{
var selectedValue=$(this).val().toLowerCase();
if(selectedValue == "athens" || selectedValue == "athens" || selectedValue == "crete" || selectedValue == "crete")
{
$("#store_name_owned_dd").val('');
}
})

Related

If the son is yes then the dropdown from list 1 should come if the son dropdown is no then the drop down from list 2 must come

I am setting up a conditional dropdown.
<div class="form-group">
<label asp-for="son" class="control-label"></label>
<select name="son" id="son">
<option value="none" class="a" selected="selected"> -- choose one --</option>
<option>Yes</option>
<option>No</option>
</select>
<span asp-validation-for="son" class="text-danger"></span>
</div>
Here is the dropdown for son, if it is NO then then the below 1st dropdown must come.
<div id="fatherNo" style="display: none" class="form-group">
<label asp-for="NO" class="control-label"></label>
<select name="NO">
<option value="none" class="a" selected="selected"> -- choose one --</option>
<option>Not_Applicable</option>
</select>
<span asp-validation-for="No" class="text-danger"></span>
</div>
If it is Yes in the son dropdown then then the below 2nd dropdown must come.
<div id="fatheryes" style="display: none" class="form-group">
<label asp-for="fatheryes" class="control-label"></label>
<select name="fatheryes">
<option value="none" class="a" selected="selected"> -- choose one --</option>
<option>working</option>
</select>
<span asp-validation-for="fatheryes" class="text-danger"></span>
</div>
as you mention jQuery in your tags, it is implemented by jQuery. Please use a dropdown
library for convenient.
Any way check snippet below:
(function ($){
'use strict';
$(function() {
var
namespace = 'customNamespace',
//-----
son = $('select[name="son"]'),
ddYes = $('#fatherNo'),
ddNo = $('#fatherYes');
var selected;
son.on('change.' + namespace, function () {
selected = $(this).find(':selected');
if(selected.val().toLowerCase() == 'yes') {
ddYes.show();
ddNo.hide();
} else if(selected.val().toLowerCase() == 'no') {
ddYes.hide();
ddNo.show();
} else {
ddYes.hide();
ddNo.hide();
}
});
});
})(jQuery);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group">
<label asp-for="son" class="control-label"></label>
<select name="son" id="son">
<option value="none" class="a" selected="selected"> -- choose one --</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<span asp-validation-for="son" class="text-danger"></span>
</div>
<div id="fatherNo" style="display: none" class="form-group">
<label asp-for="NO" class="control-label"></label>
<select name="NO">
<option value="none" class="a" selected="selected"> -- choose one --</option>
<option>Not_Applicable</option>
</select>
<span asp-validation-for="No" class="text-danger"></span>
</div>
<div id="fatherYes" style="display: none" class="form-group">
<label asp-for="fatheryes" class="control-label"></label>
<select name="fatheryes">
<option value="none" class="a" selected="selected"> -- choose one --</option>
<option>working</option>
</select>
<span asp-validation-for="fatheryes" class="text-danger"></span>
</div>
$("#son").change(function(){
var selected=$(this).children("option:selected").text();
if(selected.includes("Yes")){
$("fatheryes").css("display","block");
$("fatherNo").css("display","none");
}
else{
$("fatheryes").css("display","none");
$("fatherNo").css("display","block");
}
});
When son select is change, This function be play

how to filter and validate select option in form using PHP

$batch = htmlentities($_POST['batch']);
if($batch === 'NULL'){
echo "Batch Failed";
} else {
echo "$bacth";
}
<div class="col-md-12">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-globe"></i></span>
<select class="form-control" name="batch" id="select">
<option selected disabled>Batch Preference</option>
<option value="Weekends">Weekends</option>
<option value="Holidays">Holidays</option>
</select>
</div>
</div>
</div>
How to select those select options in form using php, how i can validate them...
Thank You

Validate select form in laravel

I have this html on my contact form:
<div class="form-group">
<label class="col-md-2 control-label" for="type">Type of website?</label>
<div class="col-md-10 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
<select class="form-control" id="type">
<option>Business</option>
<option>Company</option>
<option>Personal</option>
<option>Online Shopping</option>
<option>Other</option>
</select>
</div>
</div>
</div>
How can I validate and pass data of this field in email?
Currently I have this:
<!-- Text input-->
<div class="form-group">
<label class="col-md-2 control-label">Type</label>
<div class="col-md-10 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
<input name="type" placeholder="eg. Business, Company, Personal, Online Shopping, etc." class="form-control" type="text">
</div>
</div>
</div>
And I validate and pass it like this:
$data = array(
'type' => $request->type,
);
but that's input field what I want is my first sample (select box), so I'm not sure how to do it!
thanks.
First Give THe Name To Select drop down fields, then after Simplest Way Is use in Validation Rules with the name of select input fields. that you give required
Example:
<select class="form-control" id="type" name="Business_Type">
<option value="">Select Type</option>
<option value="Business">Business</option>
<option value="Company">Company</option>
<option value="Personal">Personal</option>
<option value="Online Shopping">Online Shopping</option>
<option value="Other">Other</option>
</select>
Validation rules
// Validation rules somewhere...
$rules = [
...
'Business_Type' => 'required',
...
];
This will work perfectly.
$rules = [
'selType' => 'required|in:Business,Company,Personal,Online Shopping,Others'
];
You can do it like this :
<div class="form-group">
<label class="col-md-2 control-label" for="type">Type of website?</label>
<div class="col-md-10 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
<select class="form-control" id="type" name="typeSelection">
<option>--Select type--</option>
<option>Business</option>
<option>Company</option>
<option>Personal</option>
<option>Online Shopping</option>
<option>Other</option>
</select>
</div>
</div>
</div>
For the rules :
$rules = [
'typeSelection' => 'required|not_in:0'
];
not_in:0 to avoid submission of the first choice witch is --Select type-- :)
And to pass infos to the validation just use $request->all() because it will use the field name in this case typeSelection no need for ctreating a new array !
In Laravel 8 i use this, i found out that if you change the value to an other value trough inspect element 'yes i know this sounds funky' still can be inserted into the validator and pass. Here's the solution
Select:
<select class="form-control" id="type" name="Business_Type">
<option value="">Select Type</option>
<option value="Business">Business</option>
<option value="Company">Company</option>
<option value="Personal">Personal</option>
<option value="Online Shopping">Online Shopping</option>
<option value="Other">Other</option></select>
Validator:
$this->validate($request, [
'selType' => 'required|in:Business,Company,Personal,Online Shopping,Others'
]);
Just enter the values that the validator must pass, all other values will not pass
You need to validate your select with required and for that you need to setup select like:
Select into blade:
<select class="form-control" id="type" name="selType">
<option value="">Select Type</option>
<option value="Business">Business</option>
<option value="Company">Company</option>
<option value="Personal">Personal</option>
<option value="Online Shopping">Online Shopping</option>
<option value="Other">Other</option>
</select>
Validation rules somewhere:
$rules = [
'selType' => 'required'
];
if you want to check if the selected value is exist in your table's column you can check it like this example
<select class="form-control" id="type" name="Business_Type">
<option value="">Select Type</option>
<option value="Business">Business</option>
<option value="Company">Company</option>
<option value="Personal">Personal</option>
<option value="Online Shopping">Online Shopping</option>
<option value="Other">Other</option>
</select>
for above selection
'Business_Type' => 'required|my_business ,type'
This will make sure the selected value exists for example in the my_business table on the type column.
if you select the first option it will return the error
The selected type is invalid.
You should put Value in each option
The first one should be
value=""
and the rest put the values of them like this
<option value="">Select Type</option>
<option value="Business">Business</option>
<option value="Company">Company</option>
<option value="Personal">Personal</option>
<option value="Online Shopping">Online Shopping</option>
<option value="Other">Other</option>

PHP echo select menu value but delete duplicate

I'm working on a profile update page and i have a form field where users can select enabled or disabled. When i echo the value, it has a duplicate and can be confusing for the user. How can i get rid of the duplicate?
<div class="form-group">
<label for="status" class="col-sm-3 control-label">Change Status</label>
<div class="col-sm-9">
<select class="form-control btn-danger" id="status" name="status">
<option value="<?php echo $status ?>"><?php echo $status ?></option>
<option value="Disabled">Disabled</option>
<option value="Enabled">Enabled</option>
</select>
</div>
</div>
As you can see, if the status is enabled and i hit the select menu its gonna say Enabled twice, same thing for disabled.
The only thing i thought of was to have another field labeled current status and have the select menu to change status.
I really dont want to do that.
<div class="form-group">
<label for="currentstatus" class="col-sm-3 control-label">Current Status</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="currentstatus" name="currentstatus" value="<?php echo $status ?>">
</div>
</div>
<div class="form-group">
<label for="status" class="col-sm-3 control-label">Change Status</label>
<div class="col-sm-9">
<select class="form-control btn-danger" id="status" name="status">
<option value="<?php echo $status ?>"><?php echo $status ?></option>
<option value="Disabled">Disabled</option>
<option value="Enabled">Enabled</option>
</select>
</div>
</div>
This worked for me:
<option <?php if ($status=="Disabled") echo " selected"; ?>>Disabled</option>
<option <?php if ($status=="Enabled") echo " selected"; ?>>Enabled</option>
Instead of outputting the selected value as another option, output it as the selected attribute of an existing option. Something like this:
<select class="form-control btn-danger" id="status" name="status">
<option value="Disabled" <?php echo ($status == "Disabled") ? "selected" : ""; ?>>Disabled</option>
<option value="Enabled" <?php echo ($status == "Enabled") ? "selected" : ""; ?>>Enabled</option>
</select>
There are likely a variety of ways to achieve the same end result, but the point is to not add options based on the data you have but instead use that data to determine which of the existing, finite, hard-coded options you already have should be selected.
Here is some jQuery that will let you change the value of the select box.
function setStatusSelect(val) {
$("select#status").val(val);
}
setStatusSelect('Enabled');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<label for="status" class="col-sm-3 control-label">Change Status</label>
<div class="col-sm-9">
<select class="form-control btn-danger" id="status" name="status">
<option value="Disabled">Disabled</option>
<option value="Enabled">Enabled</option>
</select>
</div>
</div>
If you include the Jquery function at the top of the page (so that it loads quickly) then you can do something like this:
<?php
print("<script type=\"text/javascript\">");
print("setStatusSelect(".$status.")");
print("</script>");
?>
to set the value.
I haven't tested the PHP portion, but I know the jQuery portion works.

How to get the selected dropdown value using jQuery

I want to get the selected value in dropdown for edit. But I got all the values in dropdown are selected.
function getinstruments() {
var model=$('#model').val();
var brand=$('#brand').val();
brand = brand.split('%') ;
model = model.split('%') ;
$.getJSON("{!! URL::route('appraisal.getinstrument') !!}", {brand: brand[0],model:model[0]}, function(data) {
if(data.success)
{
$('select[name="sub_model"]').find(":selected").html(data.sub_models_array);
$('select[name="color"]').find(":selected").html(data.colors_array);
$('select[name="top"]').find(":selected").html(data.top_array);
$('select[name="neck"]').find(":selected").html(data.neck_array);
}
});
}
Don't know what the problem with this code is. Reply of the question is highly appreciated
<div class="col-lg-6 col-xsm-6">
<h4>Enter Body wood details</h4>
<div class="form-group">
<label>Top Wood</label>
<select name='top_wood' class='form-control' placeholder ='Enter Top wood' id='top'>
<option value=''>Pick a topWood</option>
</select>
</div>
<div class="form-group">
<label>Back Woord</label>
<select name='back_wood' class='form-control' placeholder ='Enter Back wood' id='back'>
<option value=''>Pick a topWood</option>
</select>
</div>
<div class="form-group">
<label>Neck Wood</label>
<select name='neck_wood' class='form-control' placeholder ='Enter Neck wood' id='neck'>
<option value=''>Pick a NeckWood</option>
</select>
</div>
<div class="form-group">
<label>Select color</label>
<select name='color' class='form-control' id='color' >
<option value=''>Pick a color</option>
</select>
</div>
<div class="form-group">
<label>Frets</label>
<select name='frets' class='form-control' placeholder ='Enter Frets' id='frets'>
<option value=''>Pick a Fingerboard Material</option>
</select>
</div>
<div class="form-group">
<label>Turner</label>
<select name='turner' class='form-control' placeholder ='Enter Turner' id='turner'>
<option value=''>Pick a Fingerboard Material</option>
</select>
</div>
this is my view file where i want selected value of dropdaown

Categories