Jquery Autocomplet get data in database - php

Im create pop up form add item and create jquery autocomplete
I'm already done jquery autocomplete in my form, but i want
when i write item_name
example : Laptop
form brands, type/seri, serial also appears
Laptop (item_name)
Dell (brands)
Dell Inspirion (Seri or Type)
1239196 (Licensi)
i Confused, because i write item name,
form brands, seri, licensi also show value in form item name
Screenshot
This is my code
<script type='text/javascript'>
function add_item()
{
//alert("tes");
var content = '<div id="dialog_user_add" title="Add Data">';
content += ' <table width="100%">';
content += ' <tr>';
content += ' <td>Item</td>';
content += ' <td>:</td>';
content += ' <td><input style="width:100%"type="text" name="txt_item" id="txt_item" value="" class="text ui-widget-content ui-corner-all" /></td>';
content += ' </tr>';
content += ' <tr>';
content += ' <td>Brand</td>';
content += ' <td>:</td>';
content += ' <td><input style="width:100%"type="text" name="txt_brand" id="txt_brand" value="" class="text ui-widget-content ui-corner-all" /></td>';
content += ' </tr>';
content += ' <tr>';
content += ' <td>Type / Seri</td>';
content += ' <td>:</td>';
content += ' <td><input style="width:100%"type="text" name="txt_seri" id="txt_seri" value="" class="text ui-widget-content ui-corner-all" /></td>';
content += ' </tr>';
content += ' <tr>';
content += ' <td>Serial Number</td>';
content += ' <td>:</td>';
content += ' <td><input style="width:100%"type="text" name="txt_number" id="txt_number" value="" class="text ui-widget-content ui-corner-all" /></td>';
content += ' </tr>';
content += '</div>';
$("#txt_item").autocomplete("get_item.php",{
width: 260,
matchContains: true,
max: 50
});
$( "#txt_item" ).focusout(function() {
data = $("#txt_item").val();
$("#txt_brand").val(data);
$("#txt_seri").val(data);
$("#txt_number").val(data);
});
}
</script>
get_item.php
<?php
require_once "conection.php";
$q = strtolower($_GET["q"]);
if (!$q) return;
$sql = "select item_name,pnx_licensing_freq,pnx_manufacturing,pnx_equipment_type from item where item_name LIKE '%$q%' limit 50 ";
$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd)) {
// $cname = $rs['site_id']." / ".$rs['site_name']." - ".$rs['area'];
$cname = $rs['item_name']; //item name
$cname2 = $rs['pnx_manufacturing']; //brand
$cname3 = $rs['pnx_licensing_freq']; //licensi
$cname4 = $rs['pnx_equipment_type']; //type or seri
echo "$cname\n";
} ?>
Help me thank's :)

This function
$( "#txt_item" ).focusout(function() {
data = $("#txt_item").val();
$("#txt_brand").val(data);
$("#txt_seri").val(data);
$("#txt_number").val(data);
});
Copying the #txt_item value to #txt_brand #txt_seri #txt_number that's why item name autocomletes to all of them, because when you type item_name you focused when auto complete happens.
If you don't need to copy the value of text field to other fields, just remove this function.

Related

Get values from dynamic dropdown values and show them in a delimited text box on each new dynamic row that is generated in php and jquery

I have a table in which there is a row. The first cell of the row contains a drop-down list, and other rows are generated dynamically in case of an argument. The drop-down list contains an option with two fields from the database, one of which is text and the other is a number. The two are linked to each other. The problem that I am stuck with is that in the table The text and its associated number from the database appear only in the first row in the table, I want to repeat this in the table in the rest of the dynamically generated rows, that each text and its associated value appear in the drop down list
The problem in the image
php code:
`
<td class="td"><select class="form-control select_acount" name="account[]"required>
<option value="">--الحساب--</option>
<?php
$query = "select * from sah7atmain_acouts WHERE company like '".$multi."'";
$result = mysqli_query($con,$query);
$data = array();
if($result)
{
while($row = mysqli_fetch_assoc($result)){
$data[] = $row;
?>
<?php echo '<option value="'.$row['acount_mounte'].'">'.$row['acount_name'].'</option>'?>;
<?php
}
} else{
}
?>
</select>
</td>
jquery code:
$('.select_acount').on('change', function() {
$(this).closest('tr').find('.mount').val($(this).val());
});
var data = <?php echo json_encode($data); ?>;
var product_dd = "";
product_dd += '<select class="form-control select_acount" name="account[]"required>';
product_dd += '<option value="">--الحساب--</option>';
if(data.length > 0){
for(let i = 0; i < data.length; i ++) {
product_dd += `<option value="${data[i]['acount_mounte']}">${data[i]['acount_name']}</option>`;
}
}
product_dd += "</select>";
var i = 0;
$("#add-btn").click(function() {
++i;
$("#dynamicAddRemove").append('<tr><td class="td">'+product_dd+'</td> <td class="td"><input type="number" name="debtor[' + i + ']" id="fname"class="form-control debtor arabicNumbers" onkeydown="return event.keyCode !== 69" required></td> <td class="td"><input type="number" name="creditor[' + i + ']" id="james" class="form-control creditor arabicNumbers" onkeydown="return event.keyCode !== 69" required></td> <td class="td"><input type="text" name="description[' + i + ']" class="form-control" required></td> <td> <input type="text" name="mount[' + i + ']" class="form-control mount"required></td><td class="td2"><button type="button" name="add" class="btn btn-danger remove-tr"><i class="fas fa-trash" ></i></button></td></tr>');
});
$(document).on('click', '.remove-tr', function() {
$(this).parents('tr').remove();
});
`

A problem with fetching data from the database in dynamically generated drop-down lists

I have a table that contains a row with the ability to generate other dynamic rows. The drop-down list works now in every new row that is created. I have the first row. The drop-down list that carries data from the database works well and the data is shown in it well, but when adding a new dynamic row and when Clicking on the drop down list the values written on it all show with the word Undefined all the values of the drop down list written on it show up with the word : Undefined I am really stuck any suggestions would be appreciated and thanks
php code:
`
<td class="td"><select class="form-control select_acount" name="account[]"required>
<option value="">--الحساب--</option>
<?php
$query = "select * from sah7atmain_acouts WHERE company like '".$multi."'";
$result = mysqli_query($con,$query);
$data = array();
if($result)
{
while($row = mysqli_fetch_assoc($result)){
$data[] = $row;
?>
<?php echo '<option value="'.$row['acount_mounte'].'">'.$row['acount_name'].'</option>'?>;
<?php
}
} else{
}
?>
</select>
</td>
jquery code:
var data = '<?php echo $data; ?>';
var product_dd = "";
product_dd += '<select class="form-control select_acount" name="account[]"required>';
product_dd += '<option value="">--الحساب--</option>';
if(data.length > 0){
for(let i = 0; i < data.length; i ++) {
product_dd += `<option value="${data[i]['acount_mounte']}">${data[i]['acount_name']}</option>`;
}
}
product_dd += "</select>";
var i = 0;
$("#add-btn").click(function() {
++i;
$("#dynamicAddRemove").append('<tr><td class="td">'+product_dd+'</td> <td class="td"><input type="number" name="debtor[' + i + ']" id="fname"class="form-control debtor arabicNumbers" onkeydown="return event.keyCode !== 69" required></td> <td class="td"><input type="number" name="creditor[' + i + ']" id="james" class="form-control creditor arabicNumbers" onkeydown="return event.keyCode !== 69" required></td> <td class="td"><input type="text" name="description[' + i + ']" class="form-control" required></td> <td> <input type="text" name="mount[' + i + ']" class="form-control mount" required> </td><button type="button" name="add" class="btn btn-danger remove-tr"><i class="fas fa-trash-alt"></i></button></td></tr>');
});
$(document).on('click', '.remove-tr', function() {
$(this).parents('tr').remove();
});
`

$data[] = $row array give undefined in php

I used an array to print the data in several dynamically generated drop-down lists, but the result is very disappointing and does not bring the expected values from the database in the drop-down lists that I generate when needed, noting that the first drop-down list, which is outside the dynamic generation context, works well and brings the expected data, but the problem is Any new list generated dynamically, the values will only appear with the word "undefined" Any solution will be greatly appreciated...
here problem as GIF amage
php code:
`
<td class="td"><select class="form-control select_acount" name="account[]"required>
<option value="">--الحساب--</option>
<?php
$query = "select * from sah7atmain_acouts WHERE company like '".$multi."'";
$result = mysqli_query($con,$query);
$data = array();
if($result)
{
while($row = mysqli_fetch_assoc($result)){
$data[] = $row;
?>
<?php echo '<option value="'.$row['acount_mounte'].'">'.$row['acount_name'].'</option>'?>;
<?php
}
} else{
}
?>
</select>
</td>
jquery dynamic genaration code:
var data = '<?php echo $data; ?>';
var product_dd = "";
product_dd += '<select class="form-control select_acount" name="account[]"required>';
product_dd += '<option value="">--الحساب--</option>';
if(data.length > 0){
for(let i = 0; i < data.length; i ++) {
product_dd += `<option value="${data[i]['acount_mounte']}">${data[i]['acount_name']}</option>`;
}
}
product_dd += "</select>";
var i = 0;
$("#add-btn").click(function() {
++i;
$("#dynamicAddRemove").append('<tr><td class="td">'+product_dd+'</td> <td class="td"><input type="number" name="debtor[' + i + ']" id="fname"class="form-control debtor arabicNumbers" onkeydown="return event.keyCode !== 69" required></td> <td class="td"><input type="number" name="creditor[' + i + ']" id="james" class="form-control creditor arabicNumbers" onkeydown="return event.keyCode !== 69" required></td> <td class="td"><input type="text" name="description[' + i + ']" class="form-control" required></td> <td> <input type="text" name="mount[' + i + ']" class="form-control mount" required> </td><button type="button" name="add" class="btn btn-danger remove-tr"><i class="fas fa-trash-alt"></i></button></td></tr>');
});
$(document).on('click', '.remove-tr', function() {
$(this).parents('tr').remove();
});
`

How to preview values of dynamically generated fields with jquery

I have a form on my website where users fill an input field and selects from a drop down. For more options, a button is provided to generate more fields dynamically for users. That works fine.
Now, I’m trying to implement a functionality that previews the orders made by users (in a table format) immediately the user finish filling the orders field and the name field receives focus but I can’t seems to get it done. Any assistance will be appreciated please.
Here is what I’ve tried so far:
<form class="form-horizontal" method="post" action="" id="order_form">
<fieldset class="orders_det">
<div class="form-group">
<label for="prod_type">Type</label>
<select name="prod_type[]" class="form-control prod">
<option value="">-- Select Products --</option>
<option value="prod1">Prod1</option>
<option value="prod2">Prod2</option>
<option value="prod3">Prod3</option>
</select>
</div>
<div class="form-group">
<label for="quant" >
<input type="text" class="form-control quant" name="quant[]">
</label>
<div>
<button type="button" class="btn btn-success" id="more_btn">Add More Orders + </button>
</div>
</fieldset>
<div class="form-group">
<label for="name" class="col-md-2 control-label">Name</label>
<div class="col-md-8">
<input type="text" class="form-control name" name="name">
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success" name="order_submit" id="order_submit">Submit Order</button>
</div>
</form>
<div class="orders_preview"></div>
My jquery:
$("form input.name").on("focus", function(e){
var prod = $("form select[name='prod_type[]']").map(function(){
return $(this).val();
}).get();
var quant = $("form input[name='quant[]']").map(function(){
return ($(this).val());
}).get();
var table = '';
table = '<table>';
table = '<thead>';
table = '<tr>';
table = '<th>Product</th>';
table = '<th>Quantity</th>';
table = '</tr>';
table = '</thead>';
table = '<tbody>';
table = '<tr>';
table = '<td>'+prod+'</td>';
table = '<td>'+quant+'</td>';
table = '</tr>';
table = '</tbody>';
table = '</table>';
$(".orders_preview").append(table);
});
I will modify your jquery slightly, changing the event handler to 'on change' instead of 'on focus'. Also, just before the opening tr tag, i will loop through your var prod values, like so;
$("form").on("change", function(e) { //to generate a preview table
var prod = $("form select[name='prod_type[]']")
var quant = $("form input[name='quant[]']")
var table = '';
table += '<table>';
table += '<thead>';
table += '<tr>';
table += '<th>Product</th>';
table += '<th>Quantity</th>';
table += '</tr>';
table += '</thead>';
table += '<tbody>';
prod.each(function(i) {
table += '<tr>';
table += '<td>' + $(this).val() + '</td>';
table += '<td>' + quant.eq(i).val() + '</td>';
table += '</tr>';
})
table += '</tbody>';
table += '</table>';
$(".orders_preview").html(table);
});
You need to use += (concatenate each data to the variable)like below:-
table += '<table>';
table += '<thead>';...... so on for others
Working example:-
$(document).ready(function() {
$('form button#more_btn').on("click", function(e) {//to generate dynamic fields
e.preventDefault();
var moreOrders = '';
moreOrders += '<div class="new_order">';
moreOrders += '<div class="form-group">';
moreOrders += '<label for="prod_type">Type</label>';
moreOrders += '<select name="prod_type[]" class="form-control prod">';
moreOrders += '<option value="">-- Select Products --</option>';
moreOrders += '<option value="prod1">Prod1</option>';
moreOrders += '<option value="prod2">Prod2</option>';
moreOrders += '<option value="prod3">Prod3</option>';
moreOrders += '</select>';
moreOrders += '</div> ';
moreOrders += '<div class="form-group">';
moreOrders += '<label for="quant" class="control-label col-md-2" >Quantity</label>';
moreOrders += '<div class="form-control col-md-6">';
moreOrders += '<input type="text" class="form-control quant" name="quant[]">';
moreOrders += '</div>';
moreOrders += '<div>';
moreOrders += '</div><br />';
$('.more_orders').append(moreOrders);
});
$("form input.name").on("focus", function(e) {//to generate a preview table
var prododuct = $("form select[name='prod_type[]']");
var quantity = $("form input[name='quant[]']");
var table = '';
table += '<table>';
table += '<thead>';
table += '<tr>';
table += '<th>Product</th>';
table += '<th>Quantity</th>';
table += '</tr>';
table += '</thead>';
table += '<tbody>';
table += '<tr>';
prododuct.each(function(i) {
table += '<tr>';
table += '<td>' + $(this).val() + '</td>';
table += '<td>' + quantity.eq(i).val() + '</td>';
table += '</tr>';
})
table += '</tr>';
table += '</tbody>';
table += '</table>';
$(".orders_preview").append(table);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="form-horizontal" method="post" action="" id="order_form">
<fieldset class="orders_det">
<div class="form-group">
<label for="prod_type">Type</label>
<select name="prod_type[]" class="form-control prod">
<option value="">-- Select Products --</option>
<option value="prod1">Prod1</option>
<option value="prod2">Prod2</option>
<option value="prod3">Prod3</option>
</select>
</div>
<div class="form-group">
<label for="quant" class="control-label col-md-2" >Quantity</label>
<div class="form-control col-md-6">
<input type="text" class="form-control quant" name="quant[]">
</div>
<div>
<button type="button" class="btn btn-success" id="more_btn">Add More Orders + </button>
</div>
</div>
<div class="more_orders">
</div>
</fieldset>
<div class="form-group">
<label for="name" class="col-md-2 control-label">Name</label>
<div class="col-md-8">
<input type="text" class="form-control name" name="name">
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success" name="order_submit" id="order_submit">Submit Order</button>
</div>
</form>
<div class="orders_preview"></div>
jsFiddle working link:- https://jsfiddle.net/8jmz4w14/
By itself = does not concatenate, so you need to use += to realize the full table:
var table = '';
table += '<table>';
table += '<thead>';
table += '<tr>';
table += '<th>Product</th>';
table += '<th>Quantity</th>';
table += '</tr>';
table += '</thead>';
table += '<tbody>';
table += '<tr>';
table += '<td>'+prod+'</td>';
table += '<td>'+quant+'</td>';
table += '</tr>';
table += '</tbody>';
table += '</table>';

jquery php count loop on name to email

In form, I will be adding product info in. looping name eg. pname1, pname2 when add.
I realise now that when i wanna send to email we need the 'name'. what code should i put on php file to loop the pname?
noted: my form and php code is not in the same file.
or i should put them together and click to loop?
Php file put this?
<?php
$x=1;
while($x<=10)
{
echo "The number is: $x <br>";
echo ;
$x++;
}
?>
form in html
<div class="" id="" name="productlist"> <!-- Add Product Here -->
<ol><input class="fpname" type="text" id="addpname"placeholder="Product Name"/><input class="fpqty" type="number" id="addpqty" placeholder="Quantity"/><input class="fpprice" type="text" id="addpprice" placeholder="Price per Item"/><input type="button" id="padd" value="add"/>
</br><font color="red" size="1.5"><i>*Becareful when adding the Product, once added you are unable to edit the information except changing the quantity.</i></font>
</ol>
<ol id="addhereform"><input type="text" class="fpname" value="Product Name" readonly/><input class="fpqty" type="text" id="pqty" value="Quantity" readonly/><input type="text" class="fpprice" value="Total Price" readonly/>
</ol>
<ol><input class="fpname" id="fpships" type="text" Value="" readonly/><input class="fpqty" id="overallqty" type="text" value="" readonly/><input class="fpprice" type="text" id="overallprice" value="" readonly/>
</ol> <!-- Result -->
</div> <!-- Add Product End Here -->
jquery add button
var count = 1;
$('#padd').click(function(){
var pname = $('#addpname').val();
var pqty = $('#addpqty').val();
var pprice = $('#addpprice').val();
var totalpprice = (pqty * pprice).toFixed(2);
$('#addhereform').append('<li><input class="fpname" type="text" id="pname" name="pname' + count + '" value="' + pname + '" readonly/><input class="fpqty" type="number" id="pqty" name="pqty' + count + '" value="' + pqty + '"/><input type="text" id="pprice" name="pprice' + count + '" value="' + pprice + '" readonly hidden/><input class="fpprice" type="text" id="totalpprice" name="totalpprice' + count + '" value="' + totalpprice + '" readonly/><input type="button" id="premove" value="X"/></li>').before('</li>');
count++;
});
Ans:
$name1 = $_POST["name1"];
$name2 = $_POST["name2"];
$name3 = $_POST["name3"];
# and so on...
for($i = 1; $i <=5; $i++){
$productlist = ${'name' . $i};
echo $productlist;}
Thanks for everyones help,
Here's another method(better) I just found. no need to type all the super long loop way.
Stristr is find name keyword.
if($_POST){
foreach($_POST as $key => $value){
if(stristr($k, 'pname')){
echo "$v";
}
if(stristr($k, 'pqty')){
echo "$v";
}
if(stristr($k, 'pprice')){
echo "$v";
}
if(stristr($k, 'ptotalprice')){
echo "$v</br>";
}
}
}

Categories