im having problem getting the value of a two input textbox using an onclick function.
<?php foreach ($showItems as $items) { ?>
<tr>
<td><?php if(!empty($items['ItemCode'])) echo $items['ItemCode'], ' '; ?><input type="hidden" name="pid" id="pid" value="<?php echo $items['ItemCode']; ?>" /></td>
<td><img src="" /></td>
<td><?php if(!empty($items['Name'])) echo $items['Name'], ' '; ?></td>
<td><?php if(!empty($items['Price'])) echo $items['Price'], ' '; ?></td>
<td><?php if(!empty($items['Description'])) echo $items['Description'], ' '; ?></td>
<td><input type="text" name="qty" id="qty" maxlength="3" size="2" onblur='if(this.value==""){this.value=1;}' /></td>
<td ><input type="submit" value="Add to Registry" onClick="addRegistry(<?php echo $items['ItemCode']; ?>, document.getElementById('qty').value;)"></td>
</tr>
<?php } ?>
javascript script:
function addRegistry(pid, qty){
alert(qty);
document.form.pid.value=pid;
document.form.command.value='add';
document.form.submit();
}
php script:
if($_REQUEST['command']=='add' && $_REQUEST['pid']>0){
$qty = $_POST['qty'];
//echo $qty;
$customerid = $_SESSION['id'];
$pid=$_REQUEST['pid'];
// echo $pid;
$admin->addToRegistry($pid, $customerid, $qty);
header('location:registryCart.php');
}
how do i get the two values so that i can insert it to my database, i was able to get the itemcode, but i cant get the value of the quantity.
Use $counter variable which will increment and differentiate each input id
<?php
$counter=1;
foreach ($showItems as $items) { ?>
<tr>
<td><?php if(!empty($items['ItemCode'])) echo $items['ItemCode'], ' '; ?><input type="hidden" name="pid" id="pid" value="<?php echo $items['ItemCode']; ?>" /></td>
<td><img src="" /></td>
<td><?php if(!empty($items['Name'])) echo $items['Name'], ' '; ?></td>
<td><?php if(!empty($items['Price'])) echo $items['Price'], ' '; ?></td>
<td><?php if(!empty($items['Description'])) echo $items['Description'], ' '; ?></td>
<td><input type="text" name="qty" id="qty-<?php echo $counter; ?>" maxlength="3" size="2" onblur='if(this.value==""){this.value=1;}' /></td>
<td ><input type="submit" value="Add to Registry" onClick="addRegistry(<?php echo $items['ItemCode']; ?>, document.getElementById('qty-<?php echo $counter; ?>').value;)"></td>
</tr>
<?php
$counter++;
} ?>
Related
I want to add value to td in jquery. I have this code but not working.
Return all values for single td. I need that input [name="text-194] contain further values not all.
<table id="mytable" style="width: 100%;" border="1">
<tbody>
<?php
$counter = 1;
foreach ($result_list as $key => $row) { ?>
<tr>
<td class="jakas"><p><?php echo $counter; ?></p></td>
<td class="numer" value="<?php echo $row->numer; ?>"><p><?php echo $row->numer; ?></p></td>
<td class="nazwa" value="<?php echo $row->nazwa; ?>"><p><?php echo $row->nazwa; ?></p></td>
<td class="input" value="<?php htmlspecialchars($row->nazwa); ?>">
<?php echo do_shortcode('[contact-form-7 id="73" title="plik"]'); ?></td>
<?php echo '<input type="text" name="idtest" value="' . htmlspecialchars($row->nazwa) . '">'; ?>
</tr>
<?php $counter++; ?>
<script>
(function ($) {
$(document).ready(function () {
var value = '<?=htmlspecialchars($row->nazwa)?>';
console.log(value);
$('input[name="text-194"]').val($('input[name="text-194"').val() + value);
});
})(jQuery);
</script>
<?php
}
?>
I want to add the payment. if the payment is for the same contractor, add the payment as grand total and remove the previous row containing payment. I want to compare that is more than one payment exists against the contractor add that payment as grand total remove the previous row regarding payment. And if the contractor payment is a single entity(only one payment against the contractor) fetch from the database no addition will be made and the fetched row is displayed as the grandtotal .Here is my code
//code to compare that that payment is for thr same contractor whos row is fetched before
<?php
$contractor =0 ;
$payment_con=0;
while($row = mysqli_fetch_array($result))
{
$price = $row["price"];
$total = $row["CONCAT(Extract(HOUR From timeStamp) ,'.', Extract(MINUTE From timeStamp))"];
$grandtotal = $total * $price;
$grandtotal = $grandtotal *0.05;
if($contractor == $row['cnic']) //false
{
$payment_con = $payment_con + $grandtotal ;
?>
<script type="text/javascript">
function run(){
document.getElementById('123').style.display='none';
}
<?php
echo "run();";
?>
</script>
<tr>
<td id="serviceId_<?php echo $row["mazdoorId"];?>"><?php echo $row["mazdoorId"]; ?></td>
<td><?php echo $row["serviceName"]; ?></td>
<td><?php echo $row["price"]; ?></td>
<td><?php echo $row["CONCAT(Extract(HOUR From timeStamp) ,'.', Extract(MINUTE From timeStamp))"]; ?></td>
<td><?php echo $row["contractorName"]; ?></td>
<td><?php echo $row["cnic"]; ?></td>
<td><?php echo $grandtotal. "PKR" ?></td>
<th> GRAND TOTAL : <?php echo $payment_con. "PKR" ?></th> <td> <input type="button" name="edit" value="PAID" id="<?php echo $row["customerId"]; ?>" class="btn btn-danger btn-xs deactivate_accounts" /></td>
</td>
</tr>
<?php
}
else
{
?>
<tr id= "123">
<td id="serviceId_<?php echo $row["mazdoorId"];?>"><?php echo $row["mazdoorId"]; ?></td>
<td><?php echo $row["serviceName"]; ?></td>
<td><?php echo $row["price"]; ?></td>
<td><?php echo $row["CONCAT(Extract(HOUR From timeStamp) ,'.', Extract(MINUTE From timeStamp))"]; ?></td>
<td><?php echo $row["contractorName"]; ?></td>
<td><?php echo $row["cnic"]; ?></td>
<td><?php echo $grandtotal. "PKR" ?></td>
<th> GRAND TOTAL : <?php echo $grandtotal. "PKR" ?></th> <td> <input type="button" name="edit" value="PAID" id="<?php echo $row["customerId"]; ?>" class="btn btn-danger btn-xs deactivate_accounts" /></td>
</td>
</tr>
<?php
$contractor = $row['cnic'];
$payment_con =$grandtotal ;
}
}
?>
</thead>
</table>
i have fetched data from database with PHP in while loop.i want to assign id or class to this data to work with it in javaScript individually with every id or class which are in table td.
For example
$i = 0 ;
if($result->rowCount() > 0)
{
while($row_pro = $prod->fetch())
{
$type = $row->type;
$brand = $row->brand;
$qty = $row->qty;
$total = $row->total;
$i++ ;
?>
<tr>
<td><input type="text" value="<?php echo $type; ?>" ></td>
<td><input type="text" value="<?php echo $brand; ?>" ></td>
<td><input type="text" value="<?php echo $qty; ?>" ></td>
<td><input type="text" value="<?php echo $total; ?>" ></td>
</tr>
<?php }} ?>
One solution would be to simply echo out your loop's index ($i) as a class:
<tr>
<td><input type="text" value="<?php echo $type; ?>" class="<?php echo $i; ?>"></td>
<td><input type="text" value="<?php echo $brand; ?>" class="<?php echo $i; ?>"></td>
<td><input type="text" value="<?php echo $qty; ?>" class="<?php echo $i; ?>"></td>
<td><input type="text" value="<?php echo $total; ?>" class="<?php echo $i; ?>"></td>
</tr>
Keep in mind that you can't do this for IDs, as IDs must be unique. Also, you don't actually need to do this in the first place, as you can directly target elements with JavaScript that have neither IDs nor classes. This is most commonly with .getElementsByTagName(), which returns a NodeList collection of elements. Here's an example of that:
let three = document.getElementsByTagName('td')[2];
console.log(three.innerHTML);
<table>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
</table>
In my project I need to make the subtotal of the line (quantite*montant) dynamicali.
For the moment I have there:
I want the montant total it is auto make 1*110 for example and it is directly displayed.
My code :
<div id="contenu">
<h2>Renseigner ma fiche de frais du mois <?php echo $numMois."-".$numAnnee ?></h2>
<form method="POST" action="index.php?uc=gererFrais&action=validerMajFraisForfait">
<div class="corpsForm">
<fieldset>
<legend>Eléments forfaitisés
</legend>
<table width=100%>
<tr>
<td>Libelle</td>
<td>Nombre</td>
<td>Montant unitaire</td>
<td>Montant total</td>
</tr>
<?php
foreach ($lesFraisForfait as $unFrais)
{
$idFrais = $unFrais['idfrais'];
$libelle = $unFrais['libelle'];
$quantite = $unFrais['quantite'];
$montant = $unFrais['montant'];
?>
<tr>
<td><?php echo $libelle ?></td>
<td><input type="number" id="idFrais" name="lesFrais[<?php echo $idFrais?>]" size="10" min="0" autocomplete="off" maxlength="5" value="<?php echo $quantite?>" onkeyup="calculer()">
<td><input type="text" id="montant" value="<?php echo $montant ?>" disabled></td>
</tr>
<?php
}
?>
</table>
</fieldset>
</div>
<div class="piedForm">
<p>
<input id="ok" type="submit" value="Valider" size="20" />
<input id="annuler" type="reset" value="Effacer" size="20" />
</p>
</div>
</form>
Here I added another Answer for you.
So basically in your FOREACH Loop you have to create INCREMENTAL VARIABLE
<?php
$incr = 1;
foreach ($lesFraisForfait as $unFrais)
{
$idFrais = $unFrais['idfrais'];
$libelle = $unFrais['libelle'];
$quantite = $unFrais['quantite'];
$montant = $unFrais['montant'];
?>
<tr>
<td><?php echo $libelle ?></td>
<td><input type="number" id="<?php echo 'idFrais'.$incr; ?>" size="10" min="0" autocomplete="off" maxlength="5" value="<?php echo $quantite?>" onkeyup="calculer(this)">
<td><input type="text" id="<?php echo 'montant'.$incr; ?>" value="<?php echo $montant ?>" disabled></td>
<td id='subtotal<?php echo $incr;?>'><?php echo $quantite*$montant; ?></td>
</tr>
<?php
$incr ++;
}
?>
Since you already have ONKEYUP='calculer' on your javascript code this below.
function calculer( e ){
var i = e.getAttribute('id').length;
var input_identifier = e.getAttribute('id').substring(i-1,i);
var subtotal = document.getElementById('subtotal'+input_identifier)
var quantity = e.value;
var montant = document.getElementById('montant'+input_identifier);
subtotal.textContent = parseInt(quantity) + parseInt(montant.value);
return;
}
This should work.
please add td for total :
<tr>
<td><?php echo $libelle ?></td>
<td><input type="number" id="idFrais" name="lesFrais[<?php echo $idFrais?>]" size="10" min="0" autocomplete="off" maxlength="5" value="<?php echo $quantite?>" onkeyup="calculer()">
<td><input type="text" id="montant" value="<?php echo $montant ?>" disabled></td>
<td><input type="text" value="<?php echo ($montant*$quantite) ?>" disabled></td>
</tr>
You just have to add the following 4th TD.
<td><?php echo $quantite*$montant; ?></td>
That should work.
How to uppercase each j1 "click" then the f1 text change to uppercase, j2 "click" then the f2 text change to uppercase, et seq..
<table>
<form>
<?
$i=0;
foreach ($im as $row):
$i++;
?>
<tr>
<td><? echo $i;?></td>
<td><? echo $row['Something'] ?></td>
<td><input type="checkbox" id="j<? echo $i ?>" onclick="if(this.checked){myFunction()}"></td>
<td><input type="text" id="f<? echo $i ?>" value="Test"></td>
</tr>
<script>
function myFunction() {
document.getElementById("f"+<? echo $i ?>).value = document.getElementById("f"+<? echo $i ?>).value.toUpperCase();
}
</script>
<? endforeach; ?>
</form>
</table>
Pass the value of $i to the function and use it to identify the element. Try with -
<td><input type="checkbox" id="j<? echo $i ?>" onclick="if(this.checked){myFunction('<? echo $i ?>')}"></td>
And define the function -
<script>
function myFunction(i) {
document.getElementById("f"+i).value = document.getElementById("f"+i).value.toUpperCase();
}
</script>
You dont have to put the function inside the loop.
for another option, you don't need to declare "id" attribute in input text element, you can use "name" attribute http://jsfiddle.net/j4080ytq/
<form>
<table>
<?
$i=0;
foreach ($im as $row):
$i++;
?>
<tr>
<td><? echo $i;?></td>
<td><? echo $row['Something'] ?></td>
<td><input type="checkbox" onclick="if(this.checked){f<? echo $i; ?>.value = f<? echo $i; ?>.value.toUpperCase();}"></td>
<td><input type="text" name="f<? echo $i ?>" value="Test"></td>
</tr>
<? endforeach; ?>
</table>
</form>