I have select option input. When I choose the number on it and than click the button, I want to display the forms. The number of form displayed based on the select option I choose. I don't know how to loop it in jquery and make the form display:none (before the button click the form is not visible). And is it posible to input more than one data in one table in the same time and how to do that? I've tried this
Thank You
<div class="form-group col-sm-6">
<label for="sel1">Jrekrut : </label>
<select class="form-control" name="sel1" id="sel1" >
<option selected>Pilih</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option> </select>
</div>
<form class="form-horizontal striped-rows b-form" id="form1">
<div class="card-body">
<div class="form-group row">
<div class="col-sm-3">
<div class="b-label">
<label for="inputEmail3" class="control-label col-form-label">Full Name</label>
</div>
</div>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputEmail3" placeholder="Full Name Here">
</div>
</div>
</form>
<script type="text/javascript">
var num;
$('#sel1').on('change', function(event) {
alert("This is the value selected in solutions: " + $(this).val());
num = $(this).val();
});
$("#btn").click(function(){
//$("#form1").hide();
$("#form1").toggle();
console.log(num);
});
Okey. So first you need to make a wrapper for all forms like '.form-container' and next you need to create html in javascript and push into it.
$(document).ready(function(){
$('#sel1').on('change', function(event) {
initForms($(this).val());
});
$('body').on('click', '.btn', function(){
$('.form-horizontal').each(function(){
$(this).show();
});
)};
});
function initForms(formCount){
var finalHTML= "";
for(i=0, i<formCount; i++){
var tempHTML ='<form id="$formid$" class="form-horizontal striped-rows b-form" style="display:none;"><div class="card-body"><div class="form-group row"><div class="col-sm-3"><div class="b-label"><label for="inputEmail3" class="control-label col-form-label">Full Name</label></div></div><div class="col-sm-9"><input type="text" class="form-control" id="inputEmail3" placeholder="Full Name Here"></div></div></div></form>';
tempHTML = tempHTML.split("$formid$").join("form-" + i);
finalHTML += tempHTML;
}
$('.form-container').empty().html(finalHTML);
}
I have placed your form in a div with id equal to form_container, so that you can alter the number of forms in a cleaner way.
<div id="form_container">
<form class="form-horizontal striped-rows b-form" id="form1">
...
</form>
</div>
You can then use append() to add more some forms to your div in a for loop.
var num;
$('#sel1').on('change', function(event) {
alert("This is the value selected in solutions: " + $(this).val());
num = $(this).val();
var formHTML = $("#form1").html(); // Get the HTML content of '#form1'
$('#form_container').html(''); // Clear the contents of form_container
for(var i = 0; i < num; i++){
var id = i + 1;
$('#form_container').append('<form class="form-horizontal striped-rows b-form" id="form'+id+'">'+formHTML+'</form>');
}
});
form display:none
<form class="form-horizontal striped-rows b-form" id="form1" style="display:none">
</form>
$('#sel1').click(function(){
$("#form1").show();
});
Related
I have module that you will transfer the qty from "warehouse from" to "warehouse to"(Warehouse From & Warehouse To these are my dropdown)
I want put some validation that you cannot longer choose the first selected warehouse coming from "warehouse from"
View
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 input-padding-left">
<div class="form-group">
<label>Document Reference</label>
<input type="text" name="document_reference" class="form-control">
</div>
<div class="form-group">
<label>From:</label>
<select class="form-control warehouse" name="warehouse_from" required="required" id="warehouse_from" style="width: 100%;">
<option></option>
<option value="0">Unassigned Warehouse</option>
#if($warehouses)
#foreach($warehouses as $v => $warehouse)
<option value="{{ $warehouse->id }}">{{ $warehouse->name }}</option>
#endforeach
#endif
</select>
</div>
</div>
<hr>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 input-padding-right">
<div class="form-group">
<label>Posting Date</label>
<input type="text" name="posting_date" class="form-control dates">
</div>
<div class="form-group">
<label>To:</label>
<select class="form-control warehouse" name="warehouse_to" id="warehouse_to" required="required" style="width: 100%;">
<option></option>
#if($warehouses)
#foreach($warehouses as $v => $warehouse)
<option value="{{ $warehouse->id }}">{{ $warehouse->name }}</option>
#endforeach
#endif
</select>
</div>
</div>
Jquery
$('.warehouse').select2({
placeholder: "Select a Warehouse",
});
$(document).on('select2:select', '#warehouse_from', function(e){
// $('#warehouse_from ').on('change',function(){
//send ajax request
var id = $(this).val();
var val = $('#warehouse_from option:selected').text();
alert(val);
$(this).find('option').prop('disabled',false);
$(this).find('option[value='+val+']').prop('disabled',true);
getItemsByWarehouse(id);
e.preventDefault();
return false;
});
Question: Why my disabled didnt work?
You getting the text, not the value of the selected option element:
var val = $('#warehouse_from option:selected').text();
// ...
$(this).find('option[value='+val+']').prop('disabled',true);
It should be:
var val = $('#warehouse_from option:selected').val();
// ...
$(this).find('option[value='+val+']').prop('disabled',true);
My mistake is im disabling the value coming from warehouse_from not in the warehouse_to.
**Jquery**
$(document).on('select2:select', '#warehouse_from', function(e){
//send ajax request
var id = $(this).val();
var val = $('#warehouse_from option:selected').text();
alert(id);
$('select#warehouse_to>option[value="'+id+'"]').attr('disabled','disabled');
getItemsByWarehouse(id);
e.preventDefault();
return false;
});
i really need help with something...so im dynamically generating radio buttons from php,based on the number of items in the database,the radio buttons come with a value and a text,value is the id of the corresponding entry in the databse,the problem is when i try to get that value,it returns the value for the first radio button,for all the radio buttons,below is the jquery and php code i used,your help will be very much appreciated,Thanks
$(function(){
var pName;
var selectedValue;
$(".sel1").on('change',function(){
$selectedValue = $("input[name='id']:checked").val();
alert($selectedValue);
});
$(".submit_btn").on('click',function(event){
event.preventDefault();
var $this=$(this);
var $cat_id=$this.data('uid');
$.ajax({
url:"vote.php",
method:"get",
data:"cat_id="+$cat_id+"&hisId="+$selectedValue,
success:function(result){
alert(result);
}
})
});
});
php
<div class="col-md-4 col-lg-4 col-sm-4 col-xsm-4 col-xxl-4 mt-4">
<div class="cat_holder">
<form action="" class="ml-4">
<div class="form-group">
<label for="#select1" class="cat_name"><?php echo $catNames; ?></label><br>
<?php
foreach($player_id as $real_player_id){
$player_name=$caller->getPlayerNames($real_player_id);
?>
<input class="text_style sel1" id="select1" type="radio" name="id" value="<?php echo $real_player_id;?>"> <?php echo $player_name;?><br>
<?php
}
?>
</select>
</div>
<div class="form-row">
<div class="col-md-6 col-lg-6 col-sm-6 col-xsm-6 col-xxl-6">
<input type="submit" value="Vote" class="btn btn-primary btn-block submit_btn" data-uid="<?php echo $realcatIds;?>" readonly >
</div>
</div>
</form>
</div>
</div>
1.Remove id="select1" from radio buttons in your PHP code. (As id's need to be unique)
2.Convert
$selectedValue = $("input[name='id']:checked").val();
to
$selectedValue = $(this).val();
Note:-$("input[name='id']:checked") will give always the first checked checkbox value among all checked checkboxes.
Hi everyone i'm new here in stackoverflow so.. my first question is:
How i can get the value from HTML <select></select> and declare in <form action="here i want to declare">
html
<div class="panel-body">
<select required>
<option value="#">Select Type of File</option>
<option value="upload1.php">uno</option>
<option value="upload2.php">dos</option>
<option value="upload3.php">tres</option>
</select>
<form name="form1" id="form1" method="post" action="" enctype="multipart/form-data">
<h4 class="text-center">Cargar Multiple Archivos</h4>
<div class="form-group">
<label class="col-sm-2 control-label">Archivos</label>
<div class="col-sm-8">
<input type="file" class="form-control" id="archivo[]" name="archivo[]" multiple="">
</div>
<button type="submit" class="btn btn-primary">Cargar</button>
</div>
</form>
</div>
I want to get the value upload1.php, or upload2.php or upload3.php depending the option selected on the select HTML and declare in <form action="HERE"></form>
The project is cloud to upload files and i want to sort out if is music to a concret folder (upload1.php) if is photos in another folder (upload2.php)
I would use jQuery.
$(document).ready(function() {
$(select).on('change', function() {
var selected = $(this).val();
$('#form1').prop('action', selected);
});
});
But it is good to give id to select field first.
I have problem in updating value of textarea.
when this checkbox is checked , it will show textarea. Now the problem is i am not able to add and save some more text in the textarea.
For example, as of now the textarea shows CBC value, but if i change it to CBC NEW, then it should be update to CBC NEW.
('#contactform :checkbox.one_c').change(function() {
var $this = $(this);
if ($this.is(':checked')) {
var newInput = $('<textarea id="addme" class="doctor1 value" ><div id="c-content"></div></textarea>');
$('#contactform').append(newInput);
$("textarea").text("CBC");
$('#contactform :checkbox.one_c').attr("checked", "checked");
} else {
$('#contactform').find('#addme').remove();
$('#contactform :checkbox.one_c').removeAttr('checked');
}
});
<form class="textboxes1" method="post" id="tb2_1964">
<div id="contactform">
<div class="row">
<div class="col-md-3 col-sm-4 col-xs-12 remove_all">
<input type="checkbox" class="checkbox-inline one_c" checked="checked">
<label>CBC </label>
</div>
</div>
<textarea id="addme" class="doctor1 value">CBC</textarea>
<div id="c-content"></div>
</div> <button class="patient_add2 no_print">Save</button>
</form>
problem in your JS code. Try this hope this is helpful for you
$('.one_c').change(function() {
var $this = $(this);
if ($this.is(':checked')) {
var newInput = $('<textarea id="addme" class="doctor1 value" ><div id="c-content"></div></textarea>');
$('#contactform').append(newInput);
$("textarea").text("CBC");
$('#contactform :checkbox.one_c').attr("checked", "checked");
} else {
$('#contactform').find('#addme').remove();
$('#contactform :checkbox.one_c').removeAttr('checked');
}
});
$('.patient_add2').click(function(){
alert($("textarea").val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<form class="textboxes1" method="post" id="tb2_1964">
<div id="contactform">
<div class="row">
<div class="col-md-3 col-sm-4 col-xs-12 remove_all">
<input type="checkbox" class="checkbox-inline one_c" checked="checked">
<label>CBC </label>
</div>
</div>
<textarea id="addme" class="doctor1 value">CBC</textarea>
<div id="c-content"></div>
</div> <button class="patient_add2 no_print">Save</button>
</form>
I have a problem with chained selected script in my mvc structure
I have the form view and it contain this:
<div class="col-md-6 col-md-offset-3">
<form action="" method="post" role="form">
<div class="form-group">
<label>Nom de l'annonce:</label>
<input class="form-control input-lg" type="text" name="reg_name">
</div>
<div class="form-group">
<label>Choisi une catégorie:</label>
<select id="first_drop" class="form-control input-lg" name="categories">
<option disabled="disabled" selected="selected">choisi une categorie</option>
<?php
for ($i = 0; $i < count($dispalyCat); $i++) {
echo'<option value="' . $dispalyCat[$i]['id'] . '">' . $dispalyCat[$i]['categorieName'] . '</option>';
}
?>
</select>
</div>
<span id="loading" style="display: none;">
<img alt="Loading..." src="resources/images/loader.gif"/>
</span>
<div class="form-group" id="result" style="display: none">
<label>Choisi une sou-catégorie:</label>
<select id="second_drop" class="form-control input-lg" name="subCategories">
<?php
for ($i = 0; $i < count($dispalySubCat); $i++) {
echo'<option>' . $dispalySubCat[$i]['subCategorieName'] . '</option>';
}
?>
</select>
</div>
<div class="form-group">
<label>Prix:</label>
<input class="form-control input-lg" name="reg_pass2" type="text"/>
</div>
<div class="form-group">
<label>Surface:</label>
<input class="form-control input-lg" name="reg_pass2" type="text"/>
</div>
<div class="form-group">
<label>Description:</label>
<textarea class="form-control input-lg"></textarea>
</div>
<div class="form-group">
<label>Images:</label>
<input type="file" value="Ajouter l'annonce" class="form-control input-lg" multiple="" />
</div>
<div class="form-group">
<input type="submit" value="Ajouter l'annonce" class="btn btn-danger btn-lg" />
</div>
<input type="hidden" name="do" value="register"/>
</form>
</div>
and then the information was manipulated in the controller and this is the code:
<?php
$display = new Display('categories');
$dispalyCat = $display->getAllData();
$func = $_POST['func'];
$drop_val = $_POST['drop_val'];
if (isset($_POST['drop_val'])) {
$display2 = new Display('subcategories');
$dispalySubCat = $display2->getAllDataFromParentId($drop_val, 'categorieId');
}
include 'views/ajouterAnnonce.php';
?>
and the jquery script is:
$(document).ready(function () {
"use strict";
$('#loading').hide();
$('#first_drop').change(function () {
$('#loading').show();
$('#result').hide();
$.post('addAds.php', {
drop_val: $('select[name=categories]').val()
}, function (response) {
$('#result').fadeOut();
setTimeout("finishAjax('result', '" + escape(response) + "')", 400);
});
return false;
});
});
function finishAjax(id, response) {
"use strict";
$('#loading').hide();
$('#' + id).html(unescape(response));
$('#' + id).fadeIn();
}
when I tested this codes and I choose an option from the first select but I had an error message that the class Display not found in line 3 (in 2nd code) although it work in the first select and show me all the categories in the database
where is the problem ?
Divide the problem in 2 parts:
1.- Server side:
//In your file php
<?php
// get values from url, because for a query from a select, is not necessary use request POST
// then
echo $.GET['value'];
// Here you use queries or you do procedures for obtain data related with $.GET['value']
// Last you return data on format Json
?>
// This file php, has a url, for example: get_data_select.php
// Then whitout jquery or javascript, you should test the url, on the browser.
// http://somedomain/get_data_select.php?value=12
// If it return data with header json. server side works good.
2.- Client side: you change selected
$('#idselect').on("change", function(){
var values = '?value=' + $('#idselect').val();
$.get('http://somedomain/get_data_select.php' + values, function(res){
console.log("response", res);
callFunctionRender(res);
});
});
function callFunctionRender(data) {
// here You have data from server
// then you should rendered
};