I have created a code that adds input values and display the result in the third input box but the result is always Nan, how to solve this?
<?php
for ($i=0; $i < 5 ; $i++) {
?>
<input type="number" name="" id="pop<?php echo $i; ?>" >
<input onKeyup="su<?php echo $i; ?>();" type="number" name="" id="ta<?php echo $i; ?>">
<input type="number" name="" id="display<?php echo $i; ?>">
<br>
<script>
function su<?php echo $i; ?>(){
var x = document.getElementById("pop<?php echo $i; ?>");
var y = document.getElementById("ta<?php echo $i; ?>");
var sum;
if (y != ''){
sum = parseFloat(x + y);
document.getElementById("display<?php echo $i; ?>").value = parseFloat(x + y);
}
}
</script>
<?php
}
?>
You need to get the value from the pop and ta and add those, NOT the elements themselves. SEE:
function su<?php echo $i; ?>(){
var x = parseFloat(document.getElementById("pop<?php echo $i; ?>").value); // HERE
var y = parseFloat(document.getElementById("ta<?php echo $i; ?>").value); // HERE
var sum;
if (y != ''){
sum = parseFloat(x + y);
document.getElementById("display<?php echo $i; ?>").value = x + y;
}
}
</script>
Change these 2 lines.
Before:
var x = document.getElementById("pop<?php echo $i; ?>");
var y = document.getElementById("ta<?php echo $i; ?>");
After:
var x = document.getElementById("pop<?php echo $i; ?>").value;
var y = document.getElementById("ta<?php echo $i; ?>").value;
Can not create dynamic function.
Pass dynamic value to the function.
PHP
<?php
for ($i=0; $i < 5 ; $i++) {
?>
<input type="number" name="" id="pop<?php echo $i; ?>" >
<input onKeyup="su(<?php echo $i; ?>);" type="number" name="" id="ta<?php echo $i; ?>">
<input type="number" name="" id="display<?php echo $i; ?>">
<br />
<?php
}
?>
Javascript
function su(id){
var x = document.getElementById("pop"+id);
var y = document.getElementById("ta"+id);
var sum;
if (y != ''){
sum = parseFloat(x + y);
document.getElementById("display"+id).value = parseFloat(x + y);
}
}
Related
function some_name() {
if (document.getElementById('first').checked)
document.getElementById('second').checked=true;
else
document.getElementById('second').checked=false;
}
<?php
$idno = $vt->veriTemizle($_GET['idno']);
$rest = $vt->sqlSorgu("Select * From rest ");
foreach($vt->fetch_assocAll() as $rest){
echo '
<input type="checkbox" name="option1[]" onclick="some_name();" id="first" value="'.$rest["price"].'" />
<input type="hidden" name="extra[]" onclick="some_name();" id="second" value="'.$rest["productname"].'-'.$rest["price"].' " />
';
}
?>
is a code I have been trying to do for a long time.
I have to select another one when the checkboxes listed above are clicked.
The first record listed is working, others are not
You can generate IDs for all checkboxes like and solve the problem like this
<script>
function some_name( i) {
console.log(i);
if (document.getElementById('first'+i).checked)
document.getElementById('second'+i).checked=true;
else
document.getElementById('second'+i).checked=false;
}
</script>
<?php
$row['price'] = 100;
$row['productname'] = "ABC";
$arr[] = $row;
$row['price'] = 1001;
$row['productname'] = "asd";
$arr[] = $row;
$row['price'] = 1002;
$row['productname'] = "ABC2";
$arr[] = $row;
$row['price'] = 1003;
$row['productname'] = "ABC1";
$arr[] = $row;
$i=0;
foreach($arr as $rest){
echo '
<input type="checkbox" name="option1[]" onclick="some_name('.$i.');" id="first'.$i.'" value="'.$rest["price"].'" />
<input type="checkbox" name="extra[]" onclick="some_name('.$i.');" id="second'.$i.'" value="'.$rest["productname"].'-'.$rest["price"].' " />
';
$i++;
}
?>
I have foreach loop input type and I want to validate it with JavaScript.
<tbody>
<?php $no=1; $total=0; $total1=0;$total2=0;$total3=0; foreach ($modal as $id=>$r){ ?>
<tr class="gradeU">
<td>
<input type="hidden" name="modal[<?php echo $r->t_detail_id ?>][id_nego]" class="form-control" value="<?php echo $r->t_detail_id ?>">
</td>
<td><?php echo $no ?></td>
<td><?php echo $r->jenis_barang ?></td>
<td><?php echo $r->qty ?></td>
<td><?php echo $r->mata_uang ?> <?php echo number_format($r->harga) ?></td>
<td><?php echo $r->mata_uang ?> <?php echo number_format($r->ppn) ?>
<td><input type="text" name="modal[<?php echo $r->t_detail_id ?>][penawaran]" class="form-control" value="<?php echo $r->harga_penawaran ?> ">
</td>
<td> <input type="text" name="modal[<?php echo $r->t_detail_id ?>][nego]" class="form-control" value="<?php echo $r->harga_nego ?>"id="x"></td>
<td>
<input type="hidden" name="y" id="y"class="form-control" value="<?php echo $r->harga ?>"></td>
<td colspan="5"><button type="submit" name="submit1" class="btn btn-warning btn-sm" onClick="myFunction()">Verify</button></td>
In myFunction, I tried like this:
function myFunction() {
var y= document.getElementById("y").value;
var x = document.getElementById("x").value;
if (x > y) {
document.getElementById("demo").innerHTML ="<font color=red>Price Nego < HPS Price";
} else if(x <= y) {
document.getElementById("demo").innerHTML ="<font color=green>PRice Nego OK";
document.getElementById('submit').removeAttribute("disabled");
}
}
Only the last loop input type is validated.
I have tried making many changes to it, but to no result.
i tried myFunction like this and its worked.
thanks guys
function myFunction() {
var allinputs= document.querySelectorAll(".form-control1");
var a= document.querySelectorAll('input[type="hidden"][name="y"]');
var myLength = allinputs.length;
for (var i = 0;i < myLength ; ++i) {
if (allinputs[i].value > a[i].value) {
allinputs[i].style.backgroundColor = "red";
document.getElementById('submit').setAttribute("disabled","disabled");
document.getElementById("demo").innerHTML ="<font color=red>Harga Negosiasi Harus Dibawah Harga HPS";
}else if(allinputs[i].value < a[i].value) {
allinputs[i].style.backgroundColor = "lime";
document.getElementById('submit').removeAttribute("disabled");
document.getElementById("demo").innerHTML ="<font color=green>Harga Negosiasi OK";
}else if(allinputs[i].value == a[i].value) {
allinputs[i].style.backgroundColor = "white";
document.getElementById('submit').removeAttribute("disabled");
document.getElementById("demo").innerHTML ="<font color=green>Harga Negosiasi OK";
}
else{
document.getElementById('submit').setAttribute("disabled","disabled");
}
}
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">
Im trying to get a value of an input into my buttons action url before the button is pushed.
I need to get the value of:
id="qty<?php echo $i;?>"
Into where it says:
<?php echo VALUE OF $qtyforaction; ?>
My Code is:
<?php $i = $_product->getId();?>
<form action="<?php echo $this->helper('checkout/cart')->getAddUrl($_product); ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
<input type="text" name="qty" id="qty<?php echo $i;?>" maxlength="12" value="1" title="Qty" class="cartnum" />
<?php $qtyforaction = 'qty'.$i; ?>
<?php echo $qtyforaction; ?>
<button type="button" class="addtocartbutton button btn-cart" onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($_product); ?>qty/<?php echo VALUE OF $qtyforaction; ?>')" ><span><span>Order Again</span></span></button>
</form>
Would I have to use javascript to get it into the url string? My javascript is a bit ropey, would something like this work?
<script type="text/javascript">
var something= document.getElementById('<?php echo $qtyforaction;?>');
</script>
UPDATE WITH FULL CODE:
<?php
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
/* Get the customer data */
$customer = Mage::getSingleton('customer/session')->getCustomer();
/* Get the customer's email address */
$customer_email = $customer->getEmail();
}
$collection = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer_email', array(
'like' => $customer_email
));
$uniuqProductSkus = array();
foreach ($collection as $order) {
$order_id = $order->getId();
$order = Mage::getModel("sales/order")->load($order_id);
$ordered_items = $order->getAllItems();
foreach ($ordered_items as $item)
{
if (in_array($item->getProduct()->getSku(), $uniuqProductSkus)) {
continue;
} else {
array_push($uniuqProductSkus, $item->getProduct()->getSku());
$_product = Mage::getModel('catalog/product')->load($item->getProductId());
$product_small_image_path = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(200);
$product_thumbnail_path = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(150);
$summaryData = Mage::getModel('review/review_summary')->load($item->getProductId());
echo "<li>";
echo "<div class='previous-name'><p><a style='color:black; font-weight:bold; font-size:14px;' href='" . $_product->getProductUrl() . "'>";
echo $item->getName() . "</a></p></div>";
echo "<div class='previous-image'><a href='" . $_product->getProductUrl() . "'>";
echo "<img src='" . $product_small_image_path . "' />";
echo "</a></div>";
echo "<div class='previous-rating'>";
echo "<p><a style='color:black; font-weight:bold; font-size:14px;' href='" . $_product->getProductUrl() . "#product_tabs_review_tabbed'>Review this beer now</a></p>";
echo $summaryData->getRatingSummary() . '% Would buy again <br/>';
echo "<div class='rating-box' style='float:left;'>";
echo "<div class='rating' style='width:" . $summaryData->getRatingSummary() . "%'></div></div>";
echo "</div>";
/**echo "<div class='previous-button'>";
echo '<button type="button" title="Add to Cart" class="button btn-cart" onclick="setLocation(\'';
echo $this->helper('checkout/cart')->getAddUrl($_product);
echo '\')"><span><span>Order Again</span></span></button>';
echo "</div>";**/
?>
<?php $i = $_product->getId();?>
<div class='previous-button'>
<form action="<?php echo $this->helper('checkout/cart')->getAddUrl($_product); ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
<input type="text" name="qty" id="qty<?php echo $i;?>" maxlength="12" value="1" title="Qty" class="cartnum" />
<?php $qtyforaction = 'qty'.$i; ?>
<script type="text/javascript">
var xy = document.getElementsByTagName("input")[0].getAttribute("qty");
var x = "<button type='button' class='addtocartbutton button btn-cart' onclick='setLocation()'><span><span>Order Again</span></span></button>";
document.getElementById("buttonaddcart").innerHTML = x;
</script>
<div id="buttonaddcart">
<button type="button" class="addtocartbutton button btn-cart" onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($_product); ?>qty/')" ><span><span>Order Again</span></span></button>
</div>
</form>
</div>
<?php
echo "<div class='previous-clear'></div>";
echo "</li>";
}
}
}
?>
You need to get the value of the id attribute:
var x = document.getElementsByTagName("input")[0].getAttribute("id");
But how do you plan on using PHP to echo out this variable if PHP is loaded and done with before Javascript even comes into play? You could use Javascript though to display this variable on the page if that's all you needed.
If you had something in your HTML to target, you could do it like this:
document.getElementById("result").innerHTML = x;
But anyways, get the action attribute value...
var y = document.getElementsByTagName("form")[0].getAttribute("action");
Then set the action attribute to this + the qty...
document.getElementsByTagName("form")[0].setAttribute("action", x + y);
That should work for you, if not, try wrapping x + y in additional parentheses...("action", (x + y));
I have this text field
<?php if($row_questionset['Constructor']=="TextField"){?>
<?php while ($row_Answer=mysql_fetch_array($AnswersValue)){ ?>
<fieldset class="field" >
<?php $question=$row_questionset['QuestionIDFKPK'];?>
<span class="symbol">
<?php echo $row_questionset['QuestionValue']; ?>
<input class="validate[required] text-input" type="text" name="sampletextanswer<?= $row_Answer['QuestionIDFK'];?>[]" id=" < ?php echo $question ?> " value="<?= $answerSelect=$row_Answer['AnswerIDPK'] ; ?>" onblur="textfieldfocus()" <?php if ($row_Answer['UserAnswer']!='') {echo "checked=checked";} ?> >
</span>
</fieldset>
<?php } ?>
<?php } ?>
And i tried with these scripts for save in a table usage the info that user introduce on texfield each time that he click on it but don't works.What can i do ?
<script>
function textfieldfocus(){
var node_list = document.getElementsByTagName('input');
for (var i = 0; i < node_list.length; i++) {
var node = node_list[i];
if (node.getAttribute('type') == 'text'){
UpdateItem('<?php echo $US_ID ?>;' + this.id + ';' + this.value);
}
}
};
</script>