Add new table row with category, item, measurement, price. I was able to add a new row but was unsure how to populate drop-down data from the database. I have to fetch data for the first row. This is my code.
<tbody>
<tr>
<td><input type="checkbox" name="record"></td>
<td>
<select id="catid" name="catid[]">
<option value="">Select Category</option>
<?php
$getcatid = "SELECT catid,catname FROM category";
$result = mysqli_query($conn,$getcatid);
while($row = mysqli_fetch_array($result)){ ?>
<option value="<?php echo $row['catid']; ?>"><?php echo $row['catname']; ?></option>
<?php }?>
</td>
<td>
<select id="itemid" name="itemid[]">
<option value=''>Select Item</option>
</select>
</select>
</td>
<td>
<select id="msr" name="msr[]" onchange="showPrice(document.cument.getElementById('itemid').value,this.value)">
<option value=''>Measurement</option>
</select>
</td>
<td><div id="price"><input type="text" name="price[]"></div></td>
<td><input type="text" name="qty[]"></td>
<td><input type="text" name="total[]"></td>
</tr>
</tbody>
This is my script for add & remove row
$(document).ready(function(){
$("#add-row").click(function(){
var markup = "<tr><td><input type='checkbox' name='record'></td><td><select name='proid' id='proid'></select></td><td><select></select></td><td><select></select></td><td><input type='text name=''></td><td><input type='text' name=''></td><td><input type='text name=''></td></tr>";
$("table tbody").append(markup);
});
// Find and remove selected table rows
$("#delete-row").click(function(){
$("table tbody").find('input[name="record"]').each(function(){
if($(this).is(":checked")){
$(this).parents("tr").remove();
}
});
});
});
Try this code.
You need to use clone() in jquery.
Here is the jquery script and added working fiddle link.
Fiddle Link
$(document).on("click","#add-row", function(){
var markup = $("table tbody>tr:last").clone();
$("table tbody").append(markup);
});
// Find and remove selected table rows
$(document).on("click","#delete-row", function(){
$("table tbody").find('input[name="record"]').each(function(){
if($(this).is(":checked")){
$(this).parents("tr").remove();
}
});
});
Related
I want to change first table row drop down[client1] of first time service and activity selection and when all first row fill then reload service and activity checkbox and second time value changes in second row of table drop down[client2]without changing first row[client1] values.
Here is my code:
HTML
<select id="service" name="service" onchange="activityFromService()">
<option value="">Select Service</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option selected="selected" value="'.$row['servicename'].'">'.$row['servicename'].'</option>';
}
}else{
echo '<option value="">Service not available</option>';
}
?>
</select>
<label>Name of activity :<span>*</span></label>
<br>
<select id="activity" name="activity" onchange="changevalueofclient()" style="width:100%">
</select>
<br>
<table id="parent" class="tdpadding">
<thead>
<tr>
<th id="clientnm">Client Name</th>
</tr>
</thead>
<!-- data coming in table content -->
<tbody>
<tr>
<td>
<select id="client1" name="client" onchange="periodicityupdate(),partnername()" style="width:100%">
</select>
</td>
</tr>
<tr>
<td>
<select id="client2" name="client" onchange="periodicityupdate(),partnername()" style="width:100%">
</select>
</td>
</tr>
</tbody>
</table>
Ajax
function activityFromService(){
var service = $("#service").val();
$.ajax({
url : "./ajax/activityFromService.php",
type : "POST",
data : {service:service},
success: function(data){
$('#activity').html(data);
}
});
}
function changevalueofclient(){
var service = $("#service").val();
var activity = $("#activity").val();
$.ajax({
url : "./ajax/clientchange.php",
type : "POST",
data : {activity:activity,service:service},
success: function(data){
$("#client").html(data);
}
});
}
I tried to make dynamic ids for client drop down rows but failed.
This is the picture where new row will add after user click's on [add more] button..It works fine but i want to send these values using ajax.I can access all textbox values with array map function in ajax except dropdown values.
how I would implement it with this example. Thanks!
<tr>
<td><input type='checkbox' class='case'/></td>
<td>1.</td>
<td> <select name='relation[]' id='relation' >
<option value='brother'>Brother</option>
<option value='sister'>Sister</option>
</select>
</td>
<td><input type='text' id='name' name='name[]'></td>
<td><input type='text' id='school' name='school[]'></td>
<td<select name='medium[]' id='medium'>
<option value='marathi'>marathi</option>
<option value='english'>english </option>
</select>
</td>
<td> <?php
//Get all class data
$query = $db->query("SELECT * FROM `class_master`");
//Count total number of rows
$rowCount = $query->num_rows;
?>
<select name="std[]" id="std" class="form-control focusedInput" >
<option value="">Select Standard </option>
<?php
if($rowCount > 0){
while($row72 = $query->fetch_assoc()){
echo '<option value="'.$row72['class_id'].'">'.$row72['class_name'].'</option>';
}
} else {
echo '<option value="">Class not available</option>';
}
?>
</select>
User form at the starting of the page and and while sending data using ajax just use ("#form_id").serialize() instead of using array map , serialize is the method of jquery. Please read about the method and your problem will be solved.
I'm using .append() to create new lines inside a form composed by select boxes that are updated based on the value of the first one.
The purpose is to create as many lines (groups of select boxes) as needed and by changing the value of the first select box they update the others in the line.
I managed to do this with the following code:
HTML:
<div class="input_fields_wrap">
<table>
<tr>
<td class="span1">Select_1: </td>
<td class="span1">Select_2: </td>
<td class="span1">Select_3: </td>
<td class="span1">Select_4: </td>
</tr>
<tr>
<td>
<select class="span2" id='Select_1' name='Select_1[]'>
<option value="0">Seleccione...</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
<td>
<select class="span2" id='Select_2' name='Select_2[]'>
<option value="0">Seleccione...</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
<td>
<select class="span2" id='Select_3' name='Select_3[]'>
<option value="0">Seleccione...</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
<td>
<select class="span2" id='Select_4' name='Select_4[]'>
<option value="0">Seleccione...</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
</tr>
</table>
</div>
JQUERY:
<script type="text/javascript">
$('#Select_1').change(function(){
$('#Select_2').load("file.php?ID1=" + $(this).val());
$('#Select_3').load("file.php?ID2=" + $(this).val());
$('#Select_4').load("file.php?ID3=" + $(this).val());
});
var max_fields = 20;
var x = 1;
var wrapper = $(".input_fields_wrap");
var add_button = $(".add_field_button");
$(add_button).click(function(e){
e.preventDefault();
if(x < max_fields){
x++;
var html = '<div class="input_fields_wrap">';
html = html + '<select class="span2" id="Select_1" name="Select_1[]"><option value="0">Seleccione...</option><?php foreach($var1 as $row1){echo '<option value="'.$row1['ID'].'">'.$row1['Name'].'</option>';}?></select>';
html = html + '<select class="span2" id="Select_2" name="Select_2[]"><option value="0">Seleccione...</option><?php foreach($var2 as $row2){echo '<option value="'.$row2['ID'].'">'.$row2['Name'].'</option>';}?></select>';
html = html + '<select class="span2" id="Select_3" name="Select_3[]"><option value="0">Seleccione...</option><?php foreach($var3 as $row3){echo '<option value="'.$row3['ID'].'">'.$row3['Name'].'</option>';}?></select>';
html = html + '<select class="span2" id="Select_4" name="Select_4[]"><option value="0">Seleccione...</option><?php foreach($var4 as $row4){echo '<option value="'.$row4['ID'].'">'.$row4['Name'].'</option>';}?></select>';
html = html + '<button onclick="#" class="remove_field">Remove</button></div>';
$(wrapper).append(html);
}
});
$(wrapper).on("click",".remove_field", function(e){
e.preventDefault();
$(this).parent('div').remove();
x--;
});
</script>
This all works ok for the first line.
All the others that are created by the .append() in Jquery arenĀ“t updated when the select box "Select_1" value is changed.
I need that all the lines created by append have the funcionality to update the 3 select boxes by change the first select box value.
I'm using JQuery 2.1.4
Thanks in advance.
Just found the problem.
It was a noob question after all.
Just have to:
Call the autocomplete function again to resolve the combobox problem;
Rename the comboboxes in the append code with a autoincrement number to contemplate each line;
Run the update comboboxes code after the append with the new comboboxes name;
Here's the code to put right after the append command:
$(wrapper).append(html);
$("#Select"+x).combobox({
select: function (event, ui){
$('#Select'+x).load("file.php?ID1=" + $(this).val());
$('#Select'+x).load("file.php?ID2=" + $(this).val());
$('#Select'+x).load("file.php?ID3=" + $(this).val());
}
});
The solution was found with some help found here: jQuery append elements not working with autocomplete
Thanks for all!
I want to retrieve data to check-box list when user selected an option from a drop-down list. So, the main categories are loaded in a drop-down list and subcategories are suppose to be loaded in a list of check-boxes. Right now, I sorted out to populate data to another drop-down list. here is my code:
JQuery Code:
<script type="text/javascript">
$(document).ready(function() {
$("#parent_cat").change(function() {
$(this).after('<div id="loader" style="position:inline;"><img src="device manager/img/loading.gif" alt="loading subcategory" /></div>');
$.get('user manager/loadsubcat.php?parent_cat=' + $(this).val(), function(data) {
$("#sub_cat").html(data);
$('#loader').slideUp(200, function() {
$(this).remove();
});
});
});
});
</script>
PHP Code:
<tr>
<?php
include('config.php');
$query_parent = mysql_query("SELECT * FROM tblclient") or die("Query failed: ".mysql_error());
?>
<form method="get">
<tr>
<td><label>Client</label></td>
<td>
<select name="parent_cat" id="parent_cat">
<option selected="selected" disabled="disabled">Select a Client</option>
<?php while($row = mysql_fetch_array($query_parent)): ?>
<option value="<?php echo $row['clientId']; ?>"><?php echo $row['clientName']; ?></option>
<?php endwhile; ?>
</select>
</td>
</tr>
<br/><br/>
<tr>
<td><label>Branch</label></td>
<td>
<select name="sub_cat" id="sub_cat" multiple>
<option selected="selected" disabled="disabled">Select a Branch</option>
</select>
</td>
</tr>
</form>
Loading subcategory list PHP Code:
<?php
include('config.php');
$parent_cat = $_GET['parent_cat'];
$query = mysql_query("SELECT B.*, C.* FROM tblbranch B INNER JOIN tblclientbranch CB on CB.branchId = B.branchId INNER JOIN tblclient C ON CB.clientId = C.clientId WHERE C.clientId = {$parent_cat}");
while($row = mysql_fetch_array($query)) {
echo "<option value='$row[branchId]'>$row[branchName]</option>";
}
?>
Does anyone know how to populate subcategories to a list of check-boxes?
Any solution would be great.
Thanks for reading my long code!
something like
<input type="checkbox" name="branch" value="$row[branchid]">$row[branchname]<br>
I need to populate the relevant text box with the results of a Select drop down.
My Javascript so far is
<script language="JavaScript">
var mytextbox = document.getElementById('error');
var mydropdown = document.getElementById('errormsg_id');
mydropdown.onchange = function(){
mytextbox.value = this.value;
}
</script>
My select box and text boxes are built as arrays from a query so there is nultiple of them.
I need to be able to populate the corresponding text box with the results from the Select next to it.
Any ideas?
Thanks
<td width="1%">
<select style="width:100%" name="errormsg_id[]" id="errormsg_id[]">
<?php do { ?>
<option value="<?php echo $row_rserrormsg['errormsg_id']?>"
<?php if ($row_rsdates['errormsg_id'] == $row_rserrormsg['errormsg_id']) { echo("selected='selected'"); } ; ?> >
<?php echo $row_rserrormsg['error_message']?></option>
<?php } while ($row_rserrormsg = mysql_fetch_assoc($rserrormsg)); ?>
<?php mysql_data_seek($rserrormsg, 0);
$row_rserrormsg = mysql_fetch_assoc($rserrormsg);?>
</select>
</td>
<TD align="center" class="adminuser">
<input style="width:96%;text-align:left;" autocomplete="on" title="Enter Error Message" type="text" name="error[]" id="error[]" value="<?php echo $row_rsdates['error_message']; ?>" maxlength="100"/>
</TD>
Assuming You will be using jQuery and Your HTML is like this:
<table>
<tr>
<td><select class="my_select" name="errormsg_id1[]" id="errormsg_id1">...</select></td>
<td><input name="errormsg_id1_txt" id="errormsg_id1_txt" title="..." />
</tr>
...
<tr>
<td><select class="my_select" name="errormsg_idX[]" id="errormsg_idX">...</select></td>
<td><input name="errormsg_idX_txt" id="errormsg_idX_txt" title="..." />
</tr>
</table>
this could be done using jQuery:
$(document).ready(function(){
$('.my_select').change(function(){
$('#'+$(this).attr('id')+'_txt').val($(this).val());
});
});
Hope it is correct, have no time to test it...
Here is a JSFiddle/DEMO on how to get it to work with sample selects:
I think this approach is a little better than #shadyyx since it removed the need for you to manually increment the input and select field names.
<table>
<tr>
<td><select class="select" name="errormsg_id[]">
<option val="test1">Test1</option>
<option val="test2">Test2</option>
<option val="test3">Test3</option>
</select></td>
<td><input class="error_text" name="errormsg_id_txt[]" title="..." />
</tr>
...
<tr>
<td><select class="select" name="errormsg_id[]">
<option val="test1">Test1</option>
<option val="test2">Test2</option>
<option val="test3">Test3</option>
</select></td>
<td><input class="error_text" name="errormsg_id_txt[]" title="..." />
</tr>
</table>
And the JQuery:
$(document).ready(function(){
$('.select').change(function(){
$(this).parent().parent().find('.error_text').val($(this).val());
});
});
NOTICE: If you are using plain Javascript, see this DEMO
<table>
<tr>
<td><select class="select" name="errormsg_id[]" id="errormsg_id1" onChange="update('errormsg_id1')">
<option val="test1">Test1</option>
<option val="test2">Test2</option>
<option val="test3">Test3</option>
</select></td>
<td><input class="error_text"name="errormsg_id_txt[]" id="errormsg_id_txt1" title="..." />
</tr>
...
<tr>
<td><select class="select" name="errormsg_id[]" id="errormsg_id2" onChange="update('errormsg_id2')">
<option val="test1">Test1</option>
<option val="test2">Test2</option>
<option val="test3">Test3</option>
</select></td>
<td><input class="error_text" name="errormsg_id_txt[]" id="errormsg_id_txt2" title="..." />
</tr>
</table>
And Javascript:
function update(element){
var e = document.getElementById(element);
var id = element.substring(element.length-1);
document.getElementById("errormsg_id_txt"+id).value = e.value;
}
You need to think of way to uniquely identify your corresponding set of elements. The most convenient way I would suggest would be to use the primary key value of the data row.
And example would be like
while($row = mysqli_fetch_assoc($result)) {
$id = $row['id']; //A primary key field
echo '<input type="text" name="text'.$id.'" id="text'.$id.'" />';
echo '<select name="select'.$id.'" id="select'.$id.'" />...</select>";
}
Next, call a function from the select menu, so that only the relevant text box is updated. Update the above select portion to read something similar to this.
echo '<select name="select'.$id.'" id="select'.$id.'" onchange="updateBox(\'text'.$id.'\', this.value)" />...</select>";
Now, create a very simple simple function to update the text box.
function updateBox(element, value) {
el = document.getElementById(element);
el.value = value;
}