Calculation using JS not showing - javascript

I am trying to build a web app in php something like stock maintenance, in my earlier page the user is allowed to insert the values of the bill which he gets from the dealer, as in from whom he buys the items which he sells from his shop, hence there he inserts details about the products bought, quantity, price per pc and the rest, i mean total price and the grand total is calculated using JS.
now there is another page where the user sets his profit percent on the grand total(Grand total= total price of the products + Transportation price) and i want the selling price of each item to calculated on its own, using a button.
the rest details are being fetched from the database.
the user sets the profit% on the grand total and now i have used the JS to calculate for each item.
<script type="text/javascript">
function poffy()
{
var d=document.getElementById("ui").value;
var e=document.getElementById("grt").value;
var p=parseFloat(d-e);
var t=document.getElementById("noit").value;
var e=p/parseFloat(t);
for(var b=0;b<parseFloat(t);b++)
{
var c=document.getElementById("costprice").value;
var y=new Array();
y[b]=c;
var f=document.getElementById("quant").value;
var mk=new Array();
mk[b]=f;
var g=e/parseFloat(mk[b]);
var h=g + parseFloat(y[b]);
document.getElementById("sellprice").value=h;
}
}
</script>
Now The problem is that the calculated value is being displayed only once and only for the first item ,not for the other rows.
My php code:
<?php
include("connect.php");
$wer='ccat';
$i=0;
$dater=date("D/M/Y");
$sq="SELECT D_id FROM dealerdetail where Dealer='$wer'";
$res=mysql_query($sq);
while($row=mysql_fetch_array($res))
{
$did=$row["D_id"];
}
$io="Select pdate,tranprice,grandtotal from dateprice where D_id='$did'";
$qw=mysql_query($io);
while($row=mysql_fetch_array($qw))
{
$pd=$row["pdate"];
$as=$row["tranprice"];
$as=0 + $as;
$tr=$row["grandtotal"];
$tr=0 + $tr;
$sql="Select * from additem where Ditem_id='$did'";
$result = mysql_query($sql);
while($pop=mysql_fetch_array($result))
{
$b= $pop["Product"];
$c= $pop["Brand"];
$d= $pop["Model"];
$e= $pop["Dprice"];
$f= $pop["Quantity"];
$t=$e*$f;
$g= $pop["Quality"];
echo "<tr>";
echo "<td> $wer</td>";
echo "<td> $b </td>";
echo "<td> $c </td>";
echo "<td> $d </td>";
echo "<td><input type=text name=costprice id=costprice onkeyup=sell(); class=price value=$e /></td>";
echo "<td><input type=text name=quantity[] class=quantity id=quant value=$f /></td> ";
echo "<td><input name=txt type=text class=txt value=$t readonly /></td> ";
echo "<td><input type=text name=sellprice id=sellprice /></td>" ;
echo "<td> <div align=center ><p>$g<p></div> </td>";
/*echo "<td><input type=text name=purchasedt value=$pd /></td> "; */
echo "<td><input type=text name=current value=$dater /></td>
</tr>";
$i++;
/* if(isset($_POST['S.P']))
{
$po=$_POST['grandy'];
$ap=$po-$tr;
$ao=$ap/$i;
$ai=$ao/$f;
$ul=$_POST['costprice'];
$re=$ai + $ul;
echo $re;
}
*/
}
}
echo "<label>Number Of Items : </label> <input type=text name=no size=6 id=noit value= $i> ";
echo " <label>Dealer Name :</label> <input type=text name=fetch id=fetc readonly />";
echo " <label>Purchase Date :</label> <input name=prdt type=text size=10 class=pur value=$pd readonly /> <br><br> ";
?>
</table>
<?php
echo "<br><br><label> Transport Price :</label><input type = text name=transport value=$as > <br>";
echo " <br><label> Grand Total : </label> <input type = text name=grandy id=grt value=$tr > ";
?>
<br >
<br >
<input type="button" name="proft" onclick="calcu();" value="Profit" />
<input type ="text" name="grandy" id="ui" />

I see you already have a variable $i that you're incrementing on every row, you can use that to make the IDs unique:
echo "<td><input type=text name=costprice id=costprice$i onkeyup=sell(); class=price value=$e /></td>";
echo "<td><input type=text name=quantity[] class=quantity id=quant$i value=$f /></td> ";
echo "<td><input name=txt type=text class=txt value=$t readonly /></td> ";
echo "<td><input type=text name=sellprice id=sellprice$i /></td>" ;
Then change your Javascript to take a parameter that specifies the row:
function poffy(i)
{
var d=document.getElementById("ui").value;
var e=document.getElementById("grt").value;
var p=parseFloat(d-e);
var t=document.getElementById("noit").value;
var e=p/parseFloat(t);
for(var b=0;b<parseFloat(t);b++)
{
var c=document.getElementById("costprice"+i).value;
var y=new Array();
y[b]=c;
var f=document.getElementById("quant"+i).value;
var mk=new Array();
mk[b]=f;
var g=e/parseFloat(mk[b]);
var h=g + parseFloat(y[b]);
document.getElementById("sellprice"+i).value=h;
}
}
I don't see where in your HTML you call the poffy() function. You need to change that code to pass the row number as a parameter.
I noticed another problem in your code. You have the following at the bottom of the table:
echo "<label>Number Of Items : </label> <input type=text name=no size=6 id=noit value= $i> ";
echo " <label>Dealer Name :</label> <input type=text name=fetch id=fetc readonly />";
echo " <label>Purchase Date :</label> <input name=prdt type=text size=10 class=pur value=$pd readonly /> <br><br> ";
Everything in a <table> has to be inside <tr> and <td> or <th> elements. You either need to put those wrappers around this line, or move them out of the table.

Related

How can put my values to mysql from my php table with jquery ajax method

<div id="kutu">
<?php
echo "<table ' border='1' width='200px'>";
echo "<tr>";
echo "<th>#</th>";
echo "<th>Oyuncu Adi</th>";
echo "</tr>";
for ($x = 1; $x <= $playernum; $x++) {
echo "<tr>";
echo "<td>$x </td>";
echo "<td><input id='serdar' type='text' name='player$x' > </td>";
echo "</tr>";
}
echo "</table>";
?>
<p> <input type="submit" value="Kayit Et!"></p>
</div>
Im taking $playernum from other page. In this table I will take players name and I wanna put this names to my mysql db with javascript ajax method. But I couldnt find the way.
My table looks like:
I suggest you change your input-text to the same name like this, instead of player$x:
<form method="POST">
<input type="text" name="player" /><br />
<input type="text" name="player" /><br />
<input type="text" name="player" /><br />
<input type="text" name="player" />
<p id="result"></p>
<button id="btn">Get Player Names</button>
</form>
For the javascript:
<script>
$("form").submit(function(e) {
e.preventDefault();
players = document.getElementsByName("player");
var result = [];
for (var i = 0; i < players.length; i++) {
result[i] = players[i].value;
}
$.post(
'index.php', {
data: result
},
function(msg) {
document.getElementById("result").innerHTML = msg;
}
);
});
</script>
I just created an empty array and stored the values of each input into it. Then sent that array over to the PHP file.
For the index.php file:
$data = $_POST["data"];
echo "From PHP<br/>";
foreach ($data as $d){
echo $d . "<br/>";
}
So you can use the variable $d to access individual player names.
Result image

How to calculate from each row data textbox value in array?

I want to show total sum of values for each row data array. I have 5 rows of data, I want to get the results of each of data. Can anyone help me to figure it out?
function subtotal(konversi){
var hitung = (document.getElementById('quantity').value * document.getElementById('packing_value').value);
document.forms.demoform.quantity_konversi.value = hitung;
}
<form id='demoform'>
<?php
$jumlah=5;
for($i=0; $i<$jumlah; $i++){
$nomor = $i + 1;
echo"$nomor";
?>
<input type='text' name='quantity[]' id='quantity' onchange="subtotal(this.value,getElementById('packing_value').value);">
X
<input type='text' id="packing_value" value='10' readonly='yes'>
<input type='text' name='quantity_konversi[]' id='quantity_konversi' placeholder='result ???'><br/>
<?php } $nomor++ ?>
</form>
Your problem is that all the input fields have the same id. In HTML every element/html tag should have a unique id. So if you reference with the id JavaScript will find the first occurence of your id and use this element. All other elements are ignored even though they have the same id.
So remember ids are unique, classes can be used on several elements.
To solve your problem I added an unique ID at the end of the regular quantity id with the PHP variable $nomor:
<input type='text' name='quantity[]' id='quantity<?php echo"$nomor"; ?>' onkeyup="subtotal();">
The PHP server will make the following out of it:
<input type='text' name='quantity[]' id='quantity1' onkeyup="subtotal();">
<input type='text' name='quantity[]' id='quantity2' onkeyup="subtotal();">
...
<input type='text' name='quantity[]' id='quantity5' onkeyup="subtotal();">
You know have a unique id that can be referenced by JavaScript with the help of a running index inside of a for loop:
document.getElementById('quantity'+i).value
The same goes for document.getElementById('packing_value'+i).value
Finally the calculated value is saved in the correct field:
document.getElementById('quantity_konversi'+i).value = hitung;
FULL CODE (runnable at http://phpfiddle.org/):
<script>
function subtotal(konversi){
console.log('subtotal function');
for(var i = 1; i < 5+1; i++){
//console.log('quantity'+i, document.getElementById('quantity'+i).value);
//console.log('packing_value'+i, document.getElementById('packing_value'+i).value);
var hitung = (document.getElementById('quantity'+i).value * document.getElementById('packing_value'+i).value);
//document.forms.demoform.quantity_konversi.value = hitung;
document.getElementById('quantity_konversi'+i).value = hitung;
console.log(i, hitung);
}
}
</script>
<form id='demoform'>
<?php
$jumlah=5;
for($i=0; $i<$jumlah; $i++){
$nomor = $i + 1;
echo"$nomor";
?>
<input type='text' name='quantity[]' id='quantity<?php echo"$nomor"; ?>' onkeyup="subtotal();">
X
<input type='text' id="packing_value<?php echo"$nomor"; ?>" value='10' onkeyup="subtotal();">
<input type='text' name='quantity_konversi[]' id='quantity_konversi<?php echo"$nomor"; ?>' placeholder='result ???'><br/>
<?php
} //for end
$nomor++
?>
</form>

how to hide input values or disable from parsing into table on select

im working with a form and im thinking of making a radio button so that if the no is selected, the row of input will be disabled or hidden or something. So that when it is parsed to the next page, the hidden value is gone and not parsed together with the other values. How do i go about doing it ? Example the row with value of Examination and Consultation. How do i make it so that it will not send any value to the next page when i select a radio button with value of no?
Form
<script type="text/javascript">
var count = 0;
function addTextArea(){
count= count+1;
var div = document.getElementById('name');
div.innerHTML += "<div> <input type='text' name='name[]' value='' "+"id=name"+count+"> </div>";
//div.innerHTML += "\n<br />";
var div = document.getElementById('quantity');
div.innerHTML += "<div><input type='text' name='quantity[]' value ='' "+"id=quantity"+count+"></div>";
//div.innerHTML += "\n<br />";
var div = document.getElementById('amount');
div.innerHTML += "<div><input type='text' name='amount[]' value ='' "+"id=amount"+count+"></div>";
//div.innerHTML += "\n<br />";
}
function removeTextArea(){
document.getElementById("name"+count).remove();
document.getElementById("quantity"+count).remove();
document.getElementById("amount"+count).remove();
count = count-1;
}
</script>
</head>
<body>
<form method="POST" action="confirm_invoice.php" >
<?php
echo "<table border='2'>\n";
echo "<tr>\n";
echo "<th>Description</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Amount($)</th>\n";
echo "</tr>";
echo "<tr>";
echo "<td>"?><input type='text' size="50" name='name[]' value='Examination and Consultation' readonly/><?php "</td>";
echo "<td>"?><input type='text' size="50" name='quantity[]' value='' /><?php "</td>";
echo "<td>"?><input type='text' size="50" name='amount[]' value='' /><?php "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>"?><input type='text' size="50" name='name[]' value='Anesthesia' readonly/><?php "</td>";
echo "<td>"?><input type='text' size="50" name='quantity[]' value='' /><?php "</td>";
echo "<td>"?><input type='text' size="50" name='amount[]' value='' /><?php "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>"?><div id="name"></div> <?php "</td>";
echo "<td>"?><div id="quantity"></div> <?php "</td>";
echo "<td>"?><div id="amount"></div> <?php "</td>";
echo "</tr>";
?>
<br />
<input type="button" value="Add Description" onClick="addTextArea();">
<input type="submit" name="submit" value="submit">
<input type="button" value="Remove Description" onClick="removeTextArea();">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
you should give the attribute disabled="disabled" to the input to prevent it to be sent to the next page with the form post.
use a function like this in js:
function disableInputs(cnt)
{
document.getElementById("name"+cnt).disabled = "disabled";
document.getElementById("quantity"+cnt).disabled = "disabled";
document.getElementById("amount"+cnt).disabled = "disabled";
}
and your radio should look like this:
<input type="radio" name="disableInput" value="1" onclick="javascript:disableInputs(1)">
so in your js function you could add it like this:
function addTextArea(){
count= count+1;
var div = document.getElementById('name');
div.innerHTML += "<div> <input type='text' name='name[]' value='' "+"id=name"+count+"> </div>";
var div = document.getElementById('quantity');
div.innerHTML += "<div><input type='text' name='quantity[]' value ='' "+"id=quantity"+count+"></div>";
var div = document.getElementById('amount');
div.innerHTML += "<div><input type='text' name='amount[]' value ='' "+"id=amount"+count+"></div>";
div.innerHTML += "<div><input type='radio' name='disableInput' value='1' onclick='javascript:disableInputs(" + count + ")'></div>";
}

Php, get the values from a text box to the next page

I am relatively new to PHP Mysql, I am struck on this problem, My problem is I have a from with two textboxes (name, sal) and i got the values from mysql. Now I am making changes in the sal text box, i am making some changes int he form textboxes, I want to see the vlaues from this form which are changed or unchanged to another form. How to do it.
I am getting the Last row only as result not all the values from the text boses.
The code is
File Name: emp.php
<form name = "emp.php" action = "emp_new.php" >
<?php
$result = mysql_query("SELECT * FROM emp");
while($row = mysql_fetch_array($result))
{
$emp_name = $row["emp_name"];
$emp_sal = $row["emp_sal"];
echo "<input type='Text' name='$emp_name' value= '$emp_name' size='8' id='emp_name'>";
echo "<input type='Text' name='emp_sal' value= '$emp_sal' size='8' id='emp_sal'>";
}
<input type=submit name="process" value="Process">
Clicking on the process i want to display all the content in the text boxes in page
emp_new.php
use array element on the form like emp_name[]
<form name = "emp.php" action = "emp_new.php" method="post" >
<?php
$result = mysql_query("SELECT * FROM emp");
while($row = mysql_fetch_array($result))
{
$emp_name = $row["emp_name"];
$emp_sal = $row["emp_sal"];
echo "<input type='Text' name='emp_name[]' value= '$emp_name' size='8' id='emp_name'>";
echo "<input type='Text' name='emp_sal[]' value= '$emp_sal' size='8' id='emp_sal'>";
}
<input type="submit" name="process" value="Process">
on emp_new.php do the following
$emp_name=$_POST['emp_name'];
$emp_sal=$_POST['emp_sal'];
foreach($emp_name as $key=>$val){
$name=$val;
$sal=$emp_sal[$key];
}
just name the text boxex distinctly.. assuming that you have list of less than 100 employees.
<form method="post" action = "emp_new.php" >
<?php
$result = mysql_query("SELECT * FROM emp");
$name=1;
$sal=100;
while($row = mysql_fetch_array($result))
{
$emp_name = $row["emp_name"];
$emp_sal = $row["emp_sal"];
echo "<input type='Text' name='$name++' value= '$emp_name' size='8' id='emp_name'/>";
echo "<input type='Text' name='$sal++' value= '$emp_sal' size='8' id='emp_sal'/>";
}
echo "<input type='hidden' value=$name name='size' />";
?>
<input type=submit name="process" value="Process">
To access these value in new fom is use following code
in new_emp.php
<?php
$name=1;
$sal=100;
for($i=1;$i<=$size;$i++)
{
$emp_name = $_post[$name];
$emp_sal = $row["$sal];
echo "<input type='Text' name='$name++' value= '$emp_name' size='8' id='emp_name'>";
echo "<input type='Text' name='$sal++' value= '$emp_sal' size='8' id='emp_sal'>";
}
?>

How to add and remove attribute in HTML using jquery onchange event?

Hello guys I just want to ask how can i create a jquery that can detect the user input and assign an attribute or remove an attribute based on the user input.
My scenario is I have a table with 4 columns. First is a dropdown. And the rest are textboxes which are readonly.
The default option for dropdown is NULL or empty. If the user choose from the dropdown, the 2 texboxes should not be readonly. On that process I don't have an error but if the user set back again to NULL. The 2 textboxes won't go back to readonly again. I don't know where's my error.
Here's my jquery code:
$("[id^=code]").on('change',function(){
var index = this.id.match(/\d+/)[0];
var validate = $('#code'+index).val();
if(validate != ''){
$("#qty"+index).removeAttr('readonly');
$("#price"+index).removeAttr('readonly');
}
});
$("[id^=code]").on('change',function(){
var index = this.id.match(/\d+/)[0];
var validate = $('#code'+index).val();
if(validate == ''){
$("#qty"+index).attr('readonly');
$("#price"+index).attr('readonly');
}
});
And this is my table
for($i = 1; $i < 16; $i++){
echo "<!-- ITEM {$i} -->";
echo "<tr>";
echo "<td>";
echo "<select name='code[]' id='code{$i}' style='width:100'>";
echo "<option value=''><label>--CHOOSE ITEMS--</label></option>";
foreach($resultGetCode->result_array() as $list){
echo "<option value='".$list['itemid']."'>".$list['itemcode']." --- ".$list['itemname']."</option>";
}
echo "</select>";
echo "</td>";
//echo "<td><input type='text' name='item[]' class='k-textbox' id='item{$i}' value='' /></td>";
echo "<td><input type='text' name='qty[]' id='qty{$i}' style='text-align: center' readonly='readonly' /></td>";
echo "<td><input type='text' name='price[]' id='price{$i}' style='text-align: right;' onblur='' readonly='readonly' /></td>";
echo "<td><input type='text' name='total[]' id='total{$i}' style='font-family: courier; text-align: right; background-color: lightgray; color: red' readonly='readonly' value='' /></td>";
echo "<tr>";
}
Here's the fiddle: http://jsfiddle.net/rochellecanale/jnkc3/5/
Would be easier to help you if you create a fiddle, but try this
if(validate == ''){
$("#qty"+index).prop('readonly', true);
$("#price"+index).prop('readonly', true);
}
Update. FIDDLE

Categories