I have a button inside a table row which changes with a boolean value.
<tr id="<?php echo $zeile['id'] ?>" value="<?php echo $zeile['id'] ?>" class="task">
<td >
<form method="POST" action="main.php" value="<?php echo $zeile['id']; ?>">
<?php if ($done == true): ?>
button type="submit" name="checkbtn" id='<?php echo $zeile['id']; ?>' >
Check
</button>
<input type="hidden" name="id" value="<?php echo $zeile['id']; ?>">
<?php else: ?>
<button type="submit" name="uncheckbtn" id='<?php echo $zeile['id']; ?>'>
UnCheck
</button>
<input type="hidden" name="id" value="<?php echo $zeile['id']; ?>">
<?php endif ?>
</form>
</td>
The boolean value is change after the button is clicked here for ('checkbtn' and 'uncheckbtn')
$done = true;
if(isset($_POST['checkbtn'])){
*sqlvariables*
$done = false;
*sql*
}
with my jquery I want that I can click anywhere on the row to do a button click:
$(".table tr").on('click', function() {
$(this).toggleClass('highlighttask');
$(this).find('button[name="checkbtn"]').click();
});
The Problem is that every button changes to unchecked if I click on one row.
I think that I need to work with id's, but I don't know how.
You already have click event handler on tr so no need to use closest, just use find button
$(".table tr").on('click', function() {
$(this).toggleClass('highlighttask');
$(this).find('button[name="checkbtn"]').click();
});
Related
I have a PHP page that shows me data from the database. I'm inserting a button that, if pressed, deletes the record. The problem is that if I push the button the action is done on all the records, because I can't uniquely identify the click on the button
some code
foreach ( $associati as $associato ) {
echo "<hr />";
echo "Nome associato : ".$associato->Nome."</br>";
echo "Codice Fiscale : ".$associato->CF."</br>";
echo "<hr />";
if(isset($_POST["numerazione"])){
echo "Hello world"; //Query for delete
}
?>
<form method="POST" action="">
<input type="submit"
name="numerazione"
value="Elimina utente"
onclick="return confirm('Are you sure?')"
/>
</form>
<?php
}
How can I do to uniquely identify the button?
You can add a hidden field to each form that contains the unique identifier of the data, that means when you click the button, it will create a POST request, and in that POST request you can get the ID of the clicked record by doing $_POST['unique-id'], also make sure to populate the value of that hidden field using PHP
<?php
foreach ( $associati as $associato ) {
echo "<hr />";
echo "Nome associato : ".$associato->Nome."</br>";
echo "Codice Fiscale : ".$associato->CF."</br>";
echo "<hr />";
if(isset($_POST["numerazione"])){
$numerazione = $_POST["unique-id"];
echo "Unique record is : ".$numerazione."</br>";
}
?>
<form method="POST" action="">
<input type="hidden" name="unique-id" value="<?php echo $associato->CF; ?>" />
<input type="submit"
name="numerazione"
value="Elimina utente"
onclick="return confirm('Are you sure?')"
/>
</form>
<?php
}
?>
Pass the unique information (e.g. $id or $associato->id or whatever the variable which can identify the record) when the form is submitted
<form method="POST" action="">
<input type=hidden name=id value=<?php echo $id; ?>>
<input type="submit"
name="numerazione"
value="Elimina utente"
onclick="return confirm('Are you sure?')"
/>
</form>
Solution for this problem:
if(isset($_POST["mod"]) and $_POST["id"]== $associato->ID ){
echo "Hello world";
}
?>
<form method="POST" action="">
<input type="hidden" name="id" value=<?php echo $associato->ID; ?>>
<input type="submit"
name="mod"
value="valore"
onclick="return confirm('Are you sure?')"
/>
</form>
A form is dynamically created with PHP loop and presents one row from DB. I'm asking a user for input through prompt when he changes qty, but when I send it with AJAX to PHP it's empty. I guess it's because hidden input "new_message" is filled at the same time AJAX is sending values to PHP script so at that moment is blank. For the first row generated I get a message, but not for any other row.
Can you help me how to pass prompt input to PHP script?
//Update qty on article
$('.update_qty').on('change', function() {
var message = prompt("Upišite razlog za izmjenu količine:");
//e.preventDefault();
//Get message value
$('#new_message').val(message);
var data = $('#form1').serializeArray();
if (message != "" || message != NULL) {
$.ajax({
type: 'POST',
url: 'update_qty.php',
data: data,
success: function(data) {
alert(data);
}
});
} else {
alert("Empty");
e.PreventDefault();
return false;
}
});
<tbody>
<?php if (!empty($sales_plan_list)) {
foreach($sales_plan_list as $sales_key => $sales_value) {
?>
<tr class="new">
<td>
<a href="view_product_sales.php?id=<?php echo $sales_value['article_id']; ?>">
<?php echo $sales_value['article_no']; ?>
</a>
</td>
<td><?php echo $sales_value['description']; ?></td>
<td><?php echo myNumberFormat($sales_value['rrp']); ?></td>
<td>
<form name="form1" id="form1" method="POST" action="update_qty.php">
<input type="text" name="update_qty" class="update_qty" id="qty" value="<?php echo $sales_value['qty'] ?>"><?php echo ' Kom'; ?>
<input type="hidden" name="article_id" id="article_id" value="<?php echo $sales_value['article_id']; ?>">
<input type="hidden" name="sales_plan_id" id="sales_plan_id" value="<?php echo $sales_plan_id; ?>">
<input type="hidden" name="product_mix_id" id="product_mix_id" value="<?php echo $product_mix_id; ?>">
<input type="hidden" name="new_message" id="new_message" value="">
</form>
</td>
<td>
</td>
</tr>
<?php
}
}
?>
</tbody>
The problem is that you have multiple elements with the same id. You should NOT do that. You check the value in jQuery and then send different form data.
This is all just quessing, I am unable to check it. But try changing id to class and make sure that you send the right form data.
For example change this
<form name="form1" id="form1" method="POST" action="update_qty.php">
to this
<form name="form1" id="form<?= $sales_key; ?>" method="POST" action="update_qty.php">
I'm pulling an array of objects from my database and iterating through them using PHP to display a table. Each result row of the table has the option of deleting that particular record from the database. Each row is also an object, and the object type has a method that will delete itself from the database.
The problem I'm having is thinking through how to get this to work in the view. Right now if you click the delete button, it sets a POST variable to "delete" and reloads the same page wherein a listener process detects the variable and initiates the delete via a hidden input that contains the records database id.
This is all well and good, but these are all objects. Shouldn't each object be able to access it's own methods? It seems the only barrier to this is the interplay between PHP script and HTML forms. My code is below. Ideally, when the delete button is pressed, that particular instance of the object teamleader can run its method teamleader->delete_team_leader().
<?php foreach($teamleaders as $teamleader) { ?>
<tr>
<td><?php echo $teamleader->first_name; ?></td>
<td><?php echo $teamleader->last_name; ?></td>
<td><img src="<?php echo '../img/' . $teamleader->photo;?>" alt="" class="img-fluid tiny-tl-img"></td>
<td><?php echo nl2br(substr($teamleader->bio, 0, 100));?>...</td>
<td><?php echo $teamleader->url_name;?></td>
<form action="index.php?a=edittl" method="post">
<td>
<input type="hidden" name="id" value="<?php echo $teamleader->id?>">
<input type="submit" class="btn btn-default" name="edit" value="Edit">
</td>
</form>
<form action="index.php" method="post">
<td>
<input type="hidden" value="<?php echo $teamleader->id?>" name="id">
<input type="submit" class="btn btn-danger" name="delete" value="Delete" onclick="return confirm('Are you sure you want to delete this team leader?')">
</td>
</form>
</tr>
<?php } ?>
In your case, the best solution I think you can use ajax/jQuery for editing or deleting action.
You don't need to use many forms as now.
The code in front-end like this:
<head>
<script src="jquery.min.js"></script>
</head>
...
<?php foreach($teamleaders as $teamleader) { ?>
...
<td><?php echo $teamleader->url_name;?></td>
<td>
<input type="submit" class="btn btn-default btn-edit" name="edit" value="Edit" data-teamleaderid="<?php echo $teamleader->id?>">
</td>
<td>
<input type="submit" class="btn btn-danger btn-delete" name="delete" value="Delete" data-teamleaderid="<?php echo $teamleader->id?>">
</td>
</tr>
<?php } ?>
And the code with using ajax/jQuery is:
$(document).ready(function(){
$('.btn-delete').on('click',function(){
var _this = $(this);
var _teamleaderid = $(this).data('teamleaderid');
if (!confirm('Are you sure you want to delete this team leader?')) return false;
$.ajax(
url:'delete.php',
type:'post',
data: {
teamleaderid: _teamleaderid
},
dataType:'json',
success:function(data) {
if (data.return) {
$(_this).parent().parent().remove();/*Remove the current row in table html*/
alert('This teamleader has been deleted');
}
}
);
});
});
The delete.php code in backend is:
<?php
... #connect database
$teamleader_id = filter_input(INPUT_POST,'teamleader');
$check_delete = $dbconn->query("DELETE from teamleader_table WHERE id={$teamleader_id}");
print json_encode('return'=>$check_delete);
die();
?>
I have a form with multiple items and an id attributed to each of these items, on Submit I want to be able to grab the id of the item that was clicked - I have tried using js, something like:
<form method="post" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo '3'; ?>" type="submit" value="Add">
</form>
I want to be able to grab this value: $item_id = $_POST["item_id"]; on add_item_cart.php, but this doesn't seem to be working.
Is this a problem with my js syntax or is my logic not plausible to solve this problem? Is it submitting before changing the value?
EDIT:
Let's see if I can explain myself better, I want to assign that hidden value dynamically, imagine that my form has 3 submit buttons (one for each item displayed). Depending on the one that is clicked, I want to pass the item's id to my hidden field, so if I click button1 - $_POST["item_id"]=1, button2 - $_POST["item_id"]=2... etc
Here is my actual form (non simplified example)
<form method="post" action="add_item_cart.php">
<table style="width:600px">
<tr>
<?php foreach ($items as $item): ?>
<td>
<center>
<span style="font-size:20px"><?php echo $item["item_name"] ?></span><br/>
€<?php echo $item["price"] ?><br/>
Quantidade: <input type="text" value="1" style="width:30px"><br/>
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo $item["id"]; ?>" type="submit" value="Adicionar">
</center>
</td>
<?php endforeach; ?>
</tr>
</table>
</form>
When your form is posted and you want to collect the item_id value on add_item_cart.php first you need to actually assign a value to id as in (Assuming item_id is a php variable). The id is just used for setting the css editing not a value...
<input type="hidden" id="item_id" value="<?php echo $item_id; ?>" name="item_id">
you cannot have id='' for the value because 'value' is value.
Then you can get that value on your other page with:
<?php
if(isset($_POST['item_id'])){
$item_id = $_POST['item_id'];
}
?>
If you want to edit the variable after post depending on which button you hit you can try.
<input name="submit_item1" id="btn_sub" name="button1" type="submit" value="Add">
<input name="submit_item2" id="btn_sub" name="button1" type="submit" value="Add">
<input name="submit_item3" id="btn_sub" name="button1" type="submit" value="Add">
Then on the top of your page you can do.
<?php
if(isset($_POST['submit_item1'])){
$item_id = 1;
}
if(isset($_POST['submit_item2'])){
$item_id = 2;
}
if(isset($_POST['submit_item3'])){
$item_id = 3;
}
?>
If you are creating forms from an array of items you could do something like, (assuming you somehow have an id associated with those items; I would need to know more information about how you are making your item list. But it would generally do like this.
<?php
foreach($item_array as $item){
?>
<form method="post" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id" value="<?php echo $item['id']; ?>">
<input name="submit_item" id="btn_sub" type="submit" value="Add">
</form>
<?php
}
?>
Then on the top of your page you can just get $_POST['item_id'] and since that value is dynamically set you do not need any conditionals you can just get that value and run any query.
UPDATE
Use normal button instead of submit button and use javascript for submitting the form.
<form method="post" name="f1" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo '3'; ?>; document.f1.submit();" type="button" value="Add">
I've tried to figure out how to do this but just can't work it out, I've tried searching on here, Google and through the Virtuemart forums to no success.
I'm trying to display the Add To Cart button that is on the Product Details page on the Category browse page. I did this previously in Virtuemart 1.1 by using the following code:
<?php echo $form_addtocart ?>
However upon trying to use this same code in Virtuemart 2 I am getting no result, even though their guide still states this as far as I can see:
http://virtuemart.net/documentation/Developer_Manual/Modifying_the_Layout.html
I have tried copying the exact code from the products page but that doesn't work either, the code is:
<?php
// Add To Cart Button
if (!VmConfig::get('use_as_catalog', 0) and !empty($this->product->prices)) {
echo $this->loadTemplate('addtocart');
} // Add To Cart Button END
?>
The files I am overriding are located at:
/components/com_virtuemart/views/category/tmpl/default.php (the Category template)
/components/com_virtuemart/views/productdetails/tmpl/default.php (the Product page that the button is from)
=====
Apologies if I am overlooking something simple or if I have left out any necessary information.
Regards,
alexnire.
With VM, it could be a ton of things. If you don't have a debugging tool, just add these lines to see if the conditions are being met (as mentioned by Damien):
echo "vmConfig says: |".VmConfig::get('use_as_catalog', 0)."|";
echo ", Product has prices?: |".$this->product->prices."|";
// Add To Cart Button
if (!VmConfig::get('use_as_catalog', 0) and !empty($this->product->prices)) {
echo $this->loadTemplate('addtocart');
} // Add To Cart Button END
If both come back as true, then it's the template for the addtocart button.
(example: default_products.php) This will add the 'add to cart button'
<form method="post" class="product" action="index.php" id="addtocartproduct<?php echo $product->virtuemart_product_id ?>">
<?php // Product custom_fields
if (!empty($product->customfieldsCart)) { ?>
<div class="product-fields">
<?php foreach ($product->customfieldsCart as $field)
{ ?><div style="display:inline-block;" class="product-field product-field-type-<?php echo $field->field_type ?>">
<span class="product-fields-title" ><b><?php echo JText::_($field->custom_title) ?></b></span>
<?php echo JHTML::tooltip($field->custom_tip, JText::_($field->custom_title), 'tooltip.png'); ?>
<span class="product-field-display"><?php echo $field->display ?></span>
<span class="product-field-desc"><?php echo $field->custom_field_desc ?></span>
</div><br/ >
<?php
}
?>
</div>
<?php }
/* Product custom Childs
* to display a simple link use $field->virtuemart_product_id as link to child product_id
* custom_value is relation value to child
*/
if (!empty($this->product->customsChilds)) { ?>
<div class="product-fields">
<?php foreach ($this->product->customsChilds as $field) { ?>
<div style="display:inline-block;" class="product-field product-field-type-<?php echo $field->field->field_type ?>">
<span class="product-fields-title" ><b><?php echo JText::_($field->field->custom_title) ?></b></span>
<span class="product-field-desc"><?php echo JText::_($field->field->custom_value) ?></span>
<span class="product-field-display"><?php echo $field->display ?></span>
</div><br/ >
<?php
} ?>
</div>
<?php } ?>
<div class="addtocart-bar">
<?php // Display the quantity box ?>
<!-- <label for="quantity<?php echo $this->product->virtuemart_product_id;?>" class="quantity_box"><?php echo JText::_('COM_VIRTUEMART_CART_QUANTITY'); ?>: </label> -->
<span class="quantity-box">
<input type="text" class="quantity-input" name="quantity[]" value="1" />
</span>
<span class="quantity-controls">
<input type="button" class="quantity-controls quantity-plus" />
<input type="button" class="quantity-controls quantity-minus" />
</span>
<?php // Display the quantity box END ?>
<?php // Add the button
$button_lbl = JText::_('COM_VIRTUEMART_CART_ADD_TO');
$button_cls = ''; //$button_cls = 'addtocart_button';
if (VmConfig::get('check_stock') == '1' && !$this->product->product_in_stock) {
$button_lbl = JText::_('COM_VIRTUEMART_CART_NOTIFY');
$button_cls = 'notify-button';
} ?>
<?php // Display the add to cart button ?>
<span class="addtocart-button">
<input type="submit" name="addtocart" class="addtocart-button" value="<?php echo $button_lbl ?>" title="<?php echo $button_lbl ?>" />
</span>
<div class="clear"></div>
</div>
<?php // Display the add to cart button END ?>
<input type="hidden" class="pname" value="<?php echo $product->product_name ?>">
<input type="hidden" name="option" value="com_virtuemart" />
<input type="hidden" name="view" value="cart" />
<noscript><input type="hidden" name="task" value="add" /></noscript>
<input type="hidden" name="virtuemart_product_id[]" value="<?php echo $product->virtuemart_product_id ?>" />
<?php /** #todo Handle the manufacturer view */ ?>
<input type="hidden" name="virtuemart_manufacturer_id" value="<?php echo $product->virtuemart_manufacturer_id ?>" />
<input type="hidden" name="virtuemart_category_id[]" value="<?php echo $product->virtuemart_category_id ?>" />
</form>