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;
});
});
Related
I am trying to pass hidden value from page to another page and it work fine only for first record however for other records it's showing error
Here is the code:
$sql = "SELECT id,jdes,title FROM job";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<input type="hidden" id="hidden_user_id" value="<?php echo $row["id"] ?>">
<h3><?php echo $row["title"] ?>:</h3>
<p class="lead">
<?php echo $row["jdes"] ?>
</p>
<button type="button" id="requestthis" class="btn btn-primary">
Request
</button>
<?php
}
} else {
echo "Nothing to display Yet";
}
?>
jobs-inner.php
<?php
echo $_GET['hidden_id'];
?>
Javascript:-
$(function() { //ready function
$('#requestthis').on('click', function(e){ //click event
e.preventDefault();
var hidden_id = $('#hidden_user_id').val();
var url = "jobs-inner.php?hidden_id="+hidden_id;
window.location.replace(url);
})
})
Error:-
Undefined index: hidden_id in C:\wamp64\www\project\jobs-inner.php on line 3
It might be a simple problem but I am a beginner and I can't figure it out.
Your value is unique but the id isn't. Make the id of the input unique something like below.
<input type="hidden" id="hidden_user_<?php echo $row["id"] ?>" value="<?php echo $row["id"] ?>">
but you would have to do a count on code below to make it display base on how many rows you have.
<?php
echo $_GET['hidden_id'];
?>
Without JavaScript
$sql = "SELECT id,jdes,title FROM job";
$result = $conn->query($sql);
$count = 1;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<input type="hidden" id="hidden_user_<?php echo $count ?>" value="<?php echo $row["id"] ?>">
<h3><?php echo $row["title"] ?>:</h3>
<p class="lead"><?php echo $row["jdes"] ?></p>
<form id="<?php echo $count ?>" action="jobs-inner.php?hidden_id=<?php echo $row["id"] ?>" method="post">
<input type="submit" vaule="Request">
</form>
<?php
$count++;
}
} else {
echo "Nothing to display Yet";
}
?>
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">
I have search everywhere and learn that input tag can have an array name attribute like writing name="mark_delete[ ]" as array PHP post by form. But what I'm struggling of is the javascript part (I only use simple javascript anyway) to work.
I have 3 input name: prize[ ], quantity[ ], total[ ];
I want the total get the prize times the quantity.
The prize is fixed
The quantity is where the user's input
The total is read only.
Using onchange in input tag with array name
The 2 code show below work successfully in PHP:
Sample code 1 (using foreach): *You may skip this code
<?php
//delete_account.php
session_start();
if (isset($_SESSION['login_user'])) {
include("dbconn.php");
if (isset($_POST['delete']) && $_POST['mark_delete'] > 0) {
$mark_delete = $_POST['mark_delete'];
foreach ($mark_delete as $x) {
$sql = "UPDATE account SET status = 0 WHERE id_number = '$x'"; //status change to 0 as inactive instead of deleting the row data
mysqli_query($conn, $sql);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Delete Account</title>
</head>
<body>
<h1>Restaurant Management Information System</h1>
<h3>Mark the accounts that you want to delete and press the delete button...</h3>
<div>
<button>Menu</button>
<button>Add New Item</button>
<button>View Activity Log</button>
<button>Logout</button>
</div>
<div>
<form action="delete_account.php" method="post">
<?php
$sql = "SELECT * FROM account ORDER BY last_name, first_name";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<ol>";
while ($row = mysqli_fetch_assoc($result)) {
if ($row['status'] == 1) { //display account with status value of 1 as active
echo "<li>";
echo "<input type='checkbox' name='mark_delete[]' value='" . $row['id_number'] . "' />";
echo $row['last_name'] . ", " . $row['first_name'] . "</li>";
}
}
echo "</ol>";
}
else
echo "No account existed...";
?>
<input type="submit" name="delete" value="Delete" />
</form>
</div>
</body>
</html>
<?php
}
else {
header("Location: home.php");
exit();
}
mysqli_close($conn);
?>
Sample code 2 (using while / mysqli_fetch_assoc()): *You may skip this code
<?php
//items.php
session_start();
if (isset($_SESSION['login_user'])) {
include("dbconn.php");
if (isset($_POST['add_stocks'])) {
$amount_add = $_POST['amount_add'];
$sql = "SELECT item_id, stock FROM items";
$result = mysqli_query($conn, $sql);
if (!empty($amount_add)) {
$x = 0;
while ($row = mysqli_fetch_assoc($result)) {
if (is_int((int)$amount_add[$x])) {
$stock = $row['stock'] + $amount_add[$x];
$sql = "UPDATE items SET stock = '$stock' WHERE item_id = '$row[item_id]'";
mysqli_query($conn, $sql);
}
$x++;
}
}
}
else if (isset($_POST['add_new'])) {
$stock = $_POST['stock'];
if (empty($stock))
$stock = 0;
if (is_int((int)$stock)) {
$sql = "INSERT INTO items (item_name, prize, stock) VALUES ('$_POST[item_name]', '$_POST[prize]', '$stock')";
mysqli_query($conn, $sql);
}
}
else if (isset($_POST['item_delete'])) {
$item_delete = $_POST['item_delete'];
$x = 0;
while (!isset($item_delete[$x]))
$x++;
$sql = "DELETE FROM items WHERE item_id = '$item_delete[$x]'";
mysqli_query($conn, $sql);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Add Items</title>
</head>
<body>
<h1>Restaurant Management Information System</h1>
<h3>Restock some items or add a new one...</h3>
<div>
<button>Menu</button>
<button>View Activity Log</button>
<button>Logout</button>
<button>Delete an Account</button>
</div>
<div>
<form action="items.php" method="post">
<div>
<input type="submit" name="add_stocks" value="Add Stocks" onclick="return confirm('Add new stock. Apply?')" />
</div>
<table border="1">
<?php
$sql = "SELECT * FROM items";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
?>
<tr>
<th>ID</th>
<th>Name</th>
<th>Prize</th>
<th>Stock</th>
<th>Add Stocks</th>
<th>Action</th>
</tr>
<?php
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['item_id'] . "</td>";
echo "<td>" . $row['item_name'] . "</td>";
echo "<td>" . $row['prize'] . "</td>";
echo "<td>" . $row['stock'] . "</td>";
echo "<td><input type='text' name='amount_add[]' /></td>";
echo "<td><button type='submit' name='item_delete[]' value='" . $row['item_id'] . "' onclick='return confirm(\"You are about to delete < " . $row['item_name'] . " >. Are you sure?\")' />Delete</button></td>";
echo "</tr>";
}
}
else {
echo "<tr>";
echo "<td>No item existed...</td>";
echo "</tr>";
}
?>
</table>
</form>
<form action="items.php" method="post" id="item_table">
<label>Item Name:</label>
<input type="text" name="item_name" placeholder="Name" required="required" />
<label>Item Prize</label>
<input type="text" name="prize" placeholder="Prize" required="required" />
<label>Stock</label>
<input type="text" name="stock" />
<input type="submit" name="add_new" value="Add New Item" onclick="return confirm('Add new item. Apply?')" />
</form>
</div>
</body>
</html>
<?php
}
else {
header("Location: home.php");
exit();
}
mysqli_close($conn);
?>
The 2 codes above works in PHP posting the < tagname name=" name[ ] >
Now the real code: Image (the Total Prize didn't display, why? Sorry, I'm not really good at javascript)
<?php
session_start();
if (isset($_SESSION['login_user'])) {
include("dbconn.php");
/*{
code here
}*/
?>
<!DOCTYPE html>
<html>
<head>
<title>Order</title>
</head>
<body>
<h1>Restaurant Management Information System</h1>
<h3>Choose an amount of items to stock for order...</h3>
<div>
<button>Add New Item</button>
<button>View Activity Log</button>
<button>Logout</button>
<button>Delete an Account</button>
</div>
<div>
<form action="order.php" method="post">
<table border="1">
<div>
<input type="submit" name="order_items" value="Order" onclick="return confirm('Confirm order?')" />
</div>
<?php
$sql = "SELECT * FROM items";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
?>
<tr>
<th>ID</th>
<th>Name</th>
<th>Prize</th>
<th>Stock</th>
<th>Order Qty</th>
<th>Total Prize</th>
</tr>
<?php
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['item_id'] . "</td>";
echo "<td>" . $row['item_name'] . "</td>";
echo "<td>" . $row['prize'] . "<input type='hidden' name='prize[]' value='" . $row['prize'] . "' /></td>";
echo "<td>" . $row['stock'] . "</td>";
echo "<td><input type='text' name='quantity[]' onchange='total_prize()' value='0' /></td>"; //not working!
echo "<td><input type='text' name='total[]' readonly /></td>";
echo "</tr>";
}
}
else
echo "No item to display...";
?>
</table>
</form>
<script>
int x = 0; //initialize var only once
function total_prize() { //this function will be call many times
document.getElementByName("total")[x].value = document.getElementByName("prize")[x].value * document.getElementByName("quantity")[x].value;
x++;
}
</script>
</div>
</body>
</html>
<?php
}
else {
header("Location: home.php");
exit();
}
mysqli_close($conn);
?>
Just focus on:
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['item_id'] . "</td>";
echo "<td>" . $row['item_name'] . "</td>";
echo "<td>" . $row['prize'] . "<input type='hidden' name='prize[]' value='" . $row['prize'] . "' /></td>";
echo "<td>" . $row['stock'] . "</td>";
echo "<td><input type='text' name='quantity[]' onchange='total_prize()' value='0' /></td>"; //not working!
echo "<td><input type='text' name='total[]' readonly /></td>";
echo "</tr>";
...
<script>
int x = 0; //initialize var only once
function total_prize() { //this function will be call many times
document.getElementByName("total")[x].value = document.getElementByName("prize")[x].value * document.getElementByName("quantity")[x].value;
x++;
}
</script>
Thank you!
In your code, whenever a quantity[] is changed, it'll update only 1 item, means when you change the value on 4th quantity[](which is quantity[3]), it still get prize[0] and quantity[0]'s value, and then put the compute result to total[0], and if you now want to change that, you will end up change the value to total[1] and so on...
The problem is that you only want to change nth elements that you're currently playing with, but a static number x can't help you with that, as it only able to ref to 1 index, and it changes when function calls, it won't have a desired behavior.
Solution:
Give some index information to your total_prize function, like give it the current index.
function total_prize(index) {
var prize = document.getElementsByName('prize[]').item(index);
var quanity = document.getElementsByName('quanity[]').item(index);
var total = document.getElementsByName('total[]').item(index);
// If any of the dom element not exist, do nothing.
if (prize === null || quanity === null || total === null) {
return;
}
total.value = strToNumber(prize.value) * strToNumber(quanity.value);
}
function strToNumber(str) {
var num = parseInt(str, 10);
return isNaN(num) ? 0 : num;
}
<input type="hidden" name="prize[]" value="1"/>
<input type="hidden" name="prize[]" value="2"/>
<input type="hidden" name="prize[]" value="3"/>
<input type="hidden" name="prize[]" value="4"/>
Q1<input type="text" name="quanity[]" onchange="total_prize(0)"/><br/>
Q2<input type="text" name="quanity[]" onchange="total_prize(1)"/><br/>
Q3<input type="text" name="quanity[]" onchange="total_prize(2)"/><br/>
Q4<input type="text" name="quanity[]" onchange="total_prize(3)"/><br/>
T1<input type="text" name="total[]" /><br/>
T2<input type="text" name="total[]" /><br/>
T3<input type="text" name="total[]" /><br/>
T4<input type="text" name="total[]" /><br/>
Or use the same function without any given info, but loop through all elements and update all of their value each time the function is called.
function total_prize() {
var prizes = document.getElementsByName('prize[]');
var quanities = document.getElementsByName('quanity[]');
var totals = document.getElementsByName('total[]');
// Use the length that is shortest.
var length = Math.min(prizes.length, quanities.length, totals.length);
var index, price, quanity;
for (index = 0; index < length; index += 1) {
if (quanities[index].value.length === 0) {
totals[index].value = '';
} else {
price = strToNumber(prizes[index].value);
quanity = strToNumber(quanities[index].value);
totals[index].value = price * quanity;
}
}
}
function strToNumber(str) {
var num = parseInt(str, 10);
return isNaN(num) ? 0 : num;
}
<input type="hidden" name="prize[]" value="1"/>
<input type="hidden" name="prize[]" value="2"/>
<input type="hidden" name="prize[]" value="3"/>
<input type="hidden" name="prize[]" value="4"/>
Q1<input type="text" name="quanity[]" onchange="total_prize()"/><br/>
Q2<input type="text" name="quanity[]" onchange="total_prize()"/><br/>
Q3<input type="text" name="quanity[]" onchange="total_prize()"/><br/>
Q4<input type="text" name="quanity[]" onchange="total_prize()"/><br/>
T1<input type="text" name="total[]" /><br/>
T2<input type="text" name="total[]" /><br/>
T3<input type="text" name="total[]" /><br/>
T4<input type="text" name="total[]" /><br/>
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 a checkbox with input field quantity to calculate price automatically. Now it work fine if I check the checkbox option and input quantity.
I would like delete the checkbox and insert only input quantity with calculate price automatically.
<div>
<input type="checkbox" name="option[<?php echo $pt; ?>][]"
value="<?php echo $option_value['product_option_value_id']; ?>|1" id="option-value-<?php echo $option_value['product_option_value_id']; ?>"
price_prefix="<?php echo $option_value['price_prefix']; ?>" price="<?php printf("%.5f", $option_value['price_value_tax']); ?>"
onchange="calc_<?php echo $pt; ?>();"/>
</div>
<div>
<input type="text" name="" id="opt-qty-<?php echo $option_value['product_option_value_id']; ?>" value="1" oninput="
$('#option-value-<?php echo $option_value['product_option_value_id']; ?>').val( <?php echo $option_value['product_option_value_id']; ?> + '|' + Number($(this).attr('value')) );
$('#option-value-<?php echo $option_value['product_option_value_id']; ?>').attr('price', Number($(this).attr('price')) * Number($(this).attr('value')) );
calc_<?php echo $pt; ?>();
"size="4" price="<?php echo $option_value['price_value_tax']; ?>" style="width: 20px;" />
</div>
<span id="total-<?php echo $pt; ?>">
Here the script
function calc_<?php echo $pt; ?>() {
var price = 0;
$('#option-<?php echo $pt; ?> input:checked').each(function() {
if ($(this).attr('price_prefix') == '+') {
price += Number($(this).attr('price'));
} else if ($(this).attr('price_prefix') == '-') {
price -= Number($(this).attr('price'));
}
});
$('#total-<?php echo $pt; ?>').html( price_format_sign(price) );
}
How can work it only with input text (input quantity) and without checkbox input?