javascript innerhtml returning one result - javascript

When I click the checkbox, only the first row shows the text box.
Expected When I check the checkbox, the text box will show in that row.
When i click the submit button, it only inserts the data from the first row.
Example When I checked 3 boxes, it will insert the data from the first row 3 times.
How can I fix my code?
<script type="text/javascript">
function changeText(codeasset){
if(codeasset.checked){
document.getElementById('hotStuff').innerHTML = '<input type="text" id="hotStuff" style="width: 150px;" />';
}else {
document.getElementById('hotStuff').innerHTML='';
}}
</script>
<?php
$assets_supplier_query = mysql_query("SELECT * FROM supplier_assets WHERE status = '0' AND supplier_id = '$supplier_id'");
while ($get_supplier_asset = mysql_fetch_array($assets_supplier_query))
{
$asset_code = $get_supplier_asset['asset_code'];
$asset_qty = $get_supplier_asset['supply_quantity'];
$asset_query = mysql_query("SELECT * FROM ref_assetmodel WHERE modelID = '$asset_code' ");
$get_asset = mysql_fetch_array($asset_query);
$asset_model = $get_asset['assetModel'];
$specifications = $get_asset['specifications'];
$category_id = $get_asset['categoryID'];
$type_id = $get_asset['typeID'];
$category_query = mysql_query("SELECT * FROM ref_assetcategory WHERE categoryID = '$category_id' ORDER BY categoryID");
$get_category = mysql_fetch_array($category_query);
$category = $get_category['assetCategory'];
$type_query = mysql_query("SELECT * FROM ref_assettype WHERE typeID = '$type_id' ");
$get_type = mysql_fetch_array($type_query);
$type = $get_type['assetType'];
?>
<form action="../process/approve_supplies_process.php" method="post" name="supplier">
<td><input type="checkbox" id="codeasset" name="supply[]" onclick="changeText(this)" value="<?php echo $asset_code ?>"> </td>
<td name="supply[]"><?php echo $type; ?></td>
<td name="supply[]"><?php echo $asset_model; ?></td>
<td name="supply[]"><?php echo $asset_qty; ?></td>
<td name="supply[]" id="hotStuff" value="<?php echo $asset_code ?>"></td>
<?php
}
?>
<tr>
<td align="center"><input type="submit" class="edit_butt" name="action" value="Submit" onclick=""></td>
<input type="hidden" name="supplier_id" value="<?php echo $supplier_id; ?>">
</tr>
</form>
<?php
$asset_code = $_POST['asset_code'];
$categoryID = $_POST['category_id'];
$type_id = $_POST['type_id'];
$supplier_id = $_POST['supplier_id'];
date_default_timezone_set('Asia/Manila');
$inventoryDate= date("Y-m-d");
if(isset($_POST['supply'])){
foreach ($_POST['supply'] as $_value)
{
echo "Box #{$_value} was selected!\n";
mysql_query("INSERT INTO ref_inventory(inventoryDate, categoryID, typeID, modelID, serialNumber, qrCode, is_owned, supplier_id)
VALUES('$inventoryDate', '$categoryID', '$type_id', '$asset_code', '954', '', 1, '$supplier_id') ");
}
}
?>

Related

Populating dynamic form row from generated list PHP

I'm making something for a stocktake.
I have a form that generates with 4 fields. item_code, item_name, packing, quantity
<form action="goods.php" method="post">
<table>
<!-- Headers -->
<tr>
<td><b>Item Code</b></td>
<td><b>Description</b></td>
<td><b>Packing</b></td>
<td><b>Quantity</b></td>
</tr>
<?php
for ($i = 0; $i <= 50; $i++) {
?>
<tr>
<td>
<INPUT TYPE="TEXT" NAME="item_code[<?php echo $i; ?>]" SIZE="6" VALUE="
<?php
if (!empty($_POST["item_code"][($i)])) {
echo $_POST["item_code"][($i)];
}
?>"></td>
<td><?php
if (!empty($_POST["item_code"][($i)])) {
$result = FetchData($_POST["item_code"][($i)]);
echo $result['category'];
}
?>
</td>
<td>
<?php
if (!empty($_POST["item_code"][($i)])) {
echo $result['item_name'];
}
?></td>
<td>
<?php
if (!empty($_POST["item_code"][($i)])) {
echo $result['packing'];
}
?></td>
<td><INPUT TYPE="TEXT" NAME="quantity[<?php echo $i; ?>]" SIZE="5" VALUE="
<?php
if (!empty($_POST["quantity"][($i)])) {
echo $_POST["quantity"][($i)];
} else {
echo "";
}
?>"></td>
A js function for the button
<script>
function fillForm(value) {
document.getElementById('value').innerHTML = value;
}
</script>
and a list that is generated with 3 fields. item_code, item_name, packing
<?php
$dbh = dbh_get();
$sql = 'SELECT * FROM goods as goods(item_code, sort) order by human_sort(goods.item_code)';
$v = array();
$stmt = $dbh->prepare($sql);
$stmt->execute();
while (true) {
$r = $stmt->fetch();
if (is_bool($r)) break;
print '
<tr>
<td class="buttonL" id="<php ' . $r['item_code'] . ' ?>" onclick="fillForm()">' . $r['item_code'] . '</td>
<td>' . $r['item_name'] . '</td>
<td>' . $r['packing'] . '</td>
</tr>' . "\n";
}
dbh_free($dbh);
?>
}
I want to put a button on each list row and when it's clicked it populates the first three fields in the form, leaving quantity to be filled out. Then when another is clicked it populates the next form row etc,. It's working fine manually entering from the list, but the list is nearly 5000 items so it's a hassle to keep searching then scrolling up and entering the values.
I don't see how to do this with PHP so I assume I need a javascript function, which is where I'm lost. Let me know if you need more info.

Call API only for Selected items

I am calling Api : $url = 'https://plapi.ecomexpress.in/track_me/api/mawbd/?awb=awbnumber&order=' . $orderrecords[$k]["order_id"] . '&username=admin&password=admin123'; and fetching Status results of all Order IDS & displaying in php page when we refresh php page.
Now i want to select Order IDs through checkbox, than when i click on button "Show Status" , than only i want to Call Api & update the Selected Order IDs status in web page.
<p><button type= "button" class="call">Show Status</button></p>
<table class="tbl-qa" border="1">
<thead>
<tr>
<th class="table-header"></th>
<th class="table-header">ORDERID</th>
<th class="table-header">Status</th>
</tr>
</thead>
<tbody id="table-body">
<?php
$tabindex = 1;
if (!empty($orderrecords))
{
foreach($orderrecords as $k => $v)
{ ?>
<?php
$hide = '';
$data['username'] = 'admin';
$data['password'] = 'admin123';
$url = 'https://plapi.ecomexpress.in/track_me/api/mawbd/?awb=awbnumber&order=' . $orderrecords[$k]["order_id"] . '&username=admin&password=admin123';
$ch = curl_init();
// some curl code
$res = explode("\n", $output);
if (!isset($res[13]))
{
$res[13] = null;
}
$status = $res[13];
?>
<tr class="table-row" id="table-row-<?php echo $orderrecords[$k]["id"]; ?>" tabindex="<?php echo $tabindex; ?>">
<td><input onclick="assignorderids('<?php echo $orderrecords[$k]["order_id"]; ?>')" type="checkbox" name="assigneeid" id="assigneeid-<?php echo $orderrecords[$k]["order_id"]; ?>" value="<?php echo $orderrecords[$k]["order_id"]; ?>"></td>
<td><?php echo $orderrecords[$k]["order_id"]; ?></td>
<td><?php echo $status; ?></td>
</tr>
<?php
$tabindex++;
}
} ?>
</tbody>
</table>
Please help me how i can achieve this ?
Update : assignorderids function
function assignorderids(oid)
{
var checkstatus=$("#assigneeid-"+oid).is(":checked");
var morderId =document.getElementById("orderids").value;
if(checkstatus==false)
{
var arrayorder = JSON.parse("[" + morderId + "]");
document.getElementById("orderids").value='';
for (var i = 0; i < arrayorder.length; i++) {
var orderstatusValue=arrayorder[i];
if(orderstatusValue!=oid){
if (document.getElementById("orderids").value=='')
{
document.getElementById("orderids").value=orderstatusValue;
}
else
{
var newvalue=document.getElementById("orderids").value;
document.getElementById("orderids").value=newvalue+","+orderstatusValue;
}
}
}
}
else
{
if(morderId=='')
{
document.getElementById("orderids").value=oid;
}
else
{
document.getElementById("orderids").value=morderId+","+oid;
}
}
}
Url Output

Trying to use JavaScript to calculate total booking price for selected seats using php pdo?

I am trying to use JavaScript to calculate total price for the tickets which are selected by using the checkbox. The result is should be shown in a alert window by clicking on the check button which invokes the JavaScript function. But when I click the button nothing happens. I don't even get any error message. I am a beginner so please if anyone can help me, I will be extremely thankful.
<?php foreach ($res as $row): ?>
<form action="book.php" method="get">
<tr><td><?php echo $row['RowNumber']; ?></td><td><?php echo $row['Price']; ?></td>
<td><input type="checkbox" type="hidden" name="myForm[<?php echo $row['RowNumber']; ?>][row]" id="row" value="<?php echo $row['RowNumber']; ?>"></input></td></tr>
<input type="hidden" name="myForm[<?php echo $row['RowNumber']; ?>][price]" id="price" value="<?php echo $row['Price']; ?>"></input>
</form>
<?php endforeach; ?>
</table>
<form action='book.php' method='get'>
Enter Email:<input type='text' name='name'></form>
<script>
function summary() {
var total = 0.0;
var seats = document.getElementById("row").value;
for(i = 1; i <= document.getElementById("row").value; i++) {
if(document.getElementId("row").checked) {
total = total + parseFloat(document.getElementById("price").innerHTML);
}
}
return total;
alert("Price of seats =" + total);
}
</script>
<input type="button" onclick="summary()" value="check">
<?php
You're returning before calling the alert, which means you never get to the alert itself. Remove the return total line.
This is a very common problem.All we have face atleast once.you should use Chrome istead of firefox or internet explorer.close your webpage and restart again.
return total should be after alertbox.
thankx.
You can try this sample
<form action="book.php" method="get">
<table>
<?php foreach ($res as $row): ?>
<tr data-rownumber="<?php echo $row['RowNumber']; ?>">
<td><?php echo $row['RowNumber']; ?></td>
<td><?php echo $row['Price']; ?></td>
<td>
<input type="checkbox" name="myForm[<?php echo $row['RowNumber']; ?>][row]" id="row-<?php echo $row['RowNumber']; ?>" value="<?php echo $row['RowNumber']; ?>" onclick="summary()" />
<input type="hidden" name="myForm[<?php echo $row['RowNumber']; ?>][price]" id="price-<?php echo $row['RowNumber']; ?>" value="<?php echo $row['Price']; ?>" />
</td>
</tr>
<?php endforeach; ?>
</table>
</form>
<form action='book.php' method='get'>
Enter Email:<input type='text' name='name'>
</form>
<script>
function summary() {
var total = 0.0;
var table = document.getElementById('items-table');
var rows = table.getElementsByTagName('tr');
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
var rowNumber = row.getAttribute('data-rownumber');
var isChecked = document.getElementById('row-' + rowNumber).checked;
if (isChecked) {
var price = parseFloat(document.getElementById('price-' + rowNumber).value);
total += price;
}
}
alert("Price of seats = " + total);
return total;
}
</script>
<input type="button" onclick="summary()" value="check">

javascript method executing twice - bootstrap-slider

I have n dynamically generated sliders each with the given DOM id slider{n}.
Because I don't know how many sliders I will have, I have attached my on slideStop event to the class, and then using $(this) to get the id, and do whatever's relevant to that slider.
$(".slider").on("slideStop", function(slideEvt) {
var rowoffset = $(this).attr("id").substring(6);
$("#percentage" + rowoffset).text(slideEvt.value);
updateNumbers(rowoffset);
});
Everything works as planned except the script is executing the updateNumbers() method twice.
As you can see here my postdata is logged to console twice along with each response, one with the correct values, and a 2nd call which has undefined values.
it's clear that the first response is a response to the 2nd call.
Why is my method firing twice? Is it something to do with the slider library or jquery?
How come the response to the failed call comes back first even though the failed call is always executed 2nd?
Here is the updateNumbers method, which certainly doesn't call its self.
function updateNumbers(rowoffset){
//get variables from DOM element values
var percentage = $("#slider" + rowoffset).val();
var group = $("#rrgroup").val();
var rrclass = $("#rrclass").val();
var category = $("#rrcategory").val();
var subcategory = $("#subcategory" + rowoffset).val();
var period = $("#rrperiod").val();
var year = $("#rryear").val();
var yearmonth = $("#rryearmonth").val();
var rangesize = $("#rangesize" + rowoffset).text();
var storesstring = $("#rrstores").val();
if(storesstring == ""){
storesarray = [];
} else {
var storesarray = storesstring.split(",");
}
var postdata = {percentage: percentage,
group: group,
class: rrclass,
category: category,
subcategory: subcategory,
period: period,
year: year,
yearmonth: yearmonth,
stores: storesarray,
rangesize: rangesize};
console.log(postdata);
$.ajax({
url: "ajaxrangemix.php",
type: "post",
dataType: 'json',
data: postdata,
success: function (response) {
console.log(response);
$("#dropskus" + rowoffset).text(response.skus);
$("#keepsales" + rowoffset).text(response.sales);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
}
HTML
<div class='panel-body' id='rangemixbody'>
<input type='hidden' id='rrgroup' value="<?php echo $group; ?>">
<input type='hidden' id='rrclass' value="<?php echo $class; ?>">
<input type='hidden' id='rrcategory' value="<?php echo $category; ?>">
<input type='hidden' id='rrperiod' value="<?php echo $period; ?>">
<input type='hidden' id='rryear' value="<?php echo $yr; ?>">
<input type='hidden' id='rryearmonth' value="<?php echo $yrmth; ?>">
<input type='hidden' id='rrstores' value='<?php implode(",", $storename); ?>'>
<table class='table table-striped'>
<thead>
<tr>
<th>Subcategory</th>
<th><span>0%</span><span class="pull-right">100%</span></th>
<th>%</th>
<th>Current Range Size</th>
<th>Drop Skus</th>
<th>Keep Sales</th>
<th>Sales Share</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
$subcategories = array();
foreach($rangereviewarray as $row){
?>
<tr>
<td>
<input type="hidden" id="subcategory<?php echo $i;?>" value="<?php echo $row['subcategory']; ?>">
<?php $subcategories[$i] = $row['subcategory']; ?>
<?php echo $row['subcategory']; ?>
</td>
<td>
<input id="slider<?php echo $i; ?>" type="text" class="slider" data-slider-min="0" data-slider-max="100" data-slider-step="1" data-slider-value="100" data-slider-tooltip="hide">
</td>
<td>
<span id="percentage<?php echo $i; ?>">100</span>%
</td>
<td>
<span id="rangesize<?php echo $i; ?>"><?php echo $row['rangesize']; ?></span>
</td>
<td><span id="dropskus<?php echo $i; ?>">0</span></td>
<td>
<input type="hidden" id="sales<?php echo $i; ?>" value="<?php echo $row['sales']; ?>">
<span id="keepsales<?php echo $i; ?>"><?php echo number_format($row['sales'], 2); ?></span>
</td>
<td><?php echo number_format($row['share'], 2); ?></td>
</tr>
<?php
$i++;
}
?>
</tbody>
</table>
<input type='hidden' id='rrsubcategories' value='<?php implode(",", $subcategories); ?>'>
</div>
It's really hard to determine the reason for the event fire twice from the question. My only suggestion, based on the attached screenshot, is to cancel the request when the slider is not related to any DOM element:
function updateNumbers(rowoffset){
var $el = $("#slider" + rowoffset);
if( $el.length == 0 ) {
return;
}
//get variables from DOM element values
var percentage = $el.val();
var group = $("#rrgroup").val();
var rrclass = $("#rrclass").val();
var category = $("#rrcategory").val();
var subcategory = $("#subcategory" + rowoffset).val();
..... rest of the function
}

Remove particular row by clicking remove link present in the same row

I have form 1st page(test.php) as follows
<form method="post" action="newtest.php">
<input name="product[]" type="checkbox" value="1" <?php if(in_array("1", $session_products)) echo "checked='checked'"; ?> alt="1607.00" />
<input name="product[]" type="checkbox" value="2" <?php if(in_array("2", $session_products)) echo "checked='checked'"; ?> alt="1848.00" />
<input name="product[]" type="checkbox" value="3" <?php if(in_array("3", $session_products)) echo "checked='checked'"; ?> alt="180.00" />
<input name="product[]" type="checkbox" value="4" <?php if(in_array("4", $session_products)) echo "checked='checked'"; ?> alt="650.00" />
<input name="product[]" type="checkbox" value="5" <?php if(in_array("5", $session_products)) echo "checked='checked'"; ?> alt="495.00" />
<div class="ph-float">
<input type="submit" name="button" value="Checkout >>" class="ph-button ph-btn-green fm-submit" disabled="true" >
</div>
</form>
Based on checkbox selection am displaying Book name,Amount and total in a table as follows(newtest.php)
(Remember am not using data base, these values are not comming from data base instad am taking these values from
product array as specified below)
<?php
$product = array();
$product[1] = array('name' => "Text Book of Human Anatomy by B.D.Chaurasiavol 6th edition Vol -I Vol-II Vol-III.", 'price' => 1607);
$product[2] = array('name' => "Nettars Atlas of Anatomy", 'price' => 1848);
$product[3] = array('name' => "Genera Anatomy by B.D.Chaurasia", 'price' => 180);
$product[4] = array('name' => "Inderbir Singh Embryology 10th edition ", 'price' => 650);
$product[5] = array('name' => "Inderbir Singh Histology ", 'price' => 495);
if(isset($_POST['button']))
{
$first = array();
$second = array();
foreach ($_POST['product'] as $pId)
{
$first[] = $product[$pId]['name'];
$second[] = $product[$pId]['price'];
}
$bookauthor = count($first);
$bookprice = count($second);
$max = ($bookauthor > $bookprice ? $bookauthor : $bookprice);
echo '<br />';
echo '<i style="font-color:#000000;font-weight:bold;text-decoration:underline;font-size:21px; padding-left:110px;margin-top:10px"> List of books you have selected:</i>';
echo '<table>';
echo '<tr>';
echo '<th style="text-align: center">SL No.</th>';
echo "<th>Book Name</th>";
echo "<th>Amount in INR</th>";
echo "<th>Action</th>";
echo '</tr>';
$count = 0;
for ($i = 0; $i < $max; $i++)
{
$count++;
echo '<tr>';
echo "<td style='text-align: center'>{$count}</td>";
echo "<td>{$first[$i]}</td>";
echo "<td>{$second[$i]}</td>";
echo "<td><a href='#'><i style='color:#F5F5F5;background:#D52020'>REMOVE</i> </a></td>";
echo '</tr>';
}
$total = array_sum($second);
echo '<tr>';
echo "<td colspan='2' style='font-weight:bold;font-size:14px'>Total Amount</td>";
echo "<td style='font-weight:bold;font-size:14px;'>{$total}</td>";
echo "</tr>";
echo '</table>';
}
?>
I want to remove particular row from table when i click on REMOVE link displaying in a same row, how to achive this?
link is ok or should i have to use button.If any one write some code may helpfull as am new webie...
Thanks in advance.
$('i').click(function(){
$(this).parent('a').parent('td').parent('tr').remove();
});
The row is deleted but as clicking makes you follow the link, it's immediately restored when the page is refreshed.
Add return false; or event.preventDefault(); at the end of the callback to prevent the default behavior :
$(document).ready(function() {
$("#favoriteFoodTable .deleteLink").on("click",function() {
var tr = $(this).closest('tr');
tr.css("background-color","#FF3700");
tr.fadeOut(400, function(){
tr.remove();
});
return false;
});
});

Categories

Resources