I am trying to make an order page to add multiple products through javascript. PHP/HTML code is:
<?php $module_row = 0; ?>
<tbody id="module-row<?php echo $module_row; ?>">
<tr>
<td><select name="prod[]"><?php foreach ($products as $product) { ?><option value="<?php echo $product['ID']; ?>"><?php echo $product['name']; ?></option></td><td><a onClick="addProd();">Add New</a></td>
</tr></tbody>
<?php $module_row++; ?>
And my Javascript code is:
var module_row = <?php echo $module_row; ?>;
function addProd() {
html = '<tbody id="module-row' + module_row + '">';
html += ' <tr>';
html += ' <td class="left"><select name="prod[]">';
<?php foreach ($products as $product) { ?>
html += ' <option value="<?php echo $product['ID']; ?>"><?php echo $product['name']; ?></option>';
<?php } ?>
html += ' </select></td></tr></tbody>';
$('#module tfoot').before(html);
module_row++;
Now I want to to show products array in later added rows to show products dropdown with out the products selected above. I have checked a lot of forums but could not find a solution to get the above selected value of dropdown. Any idea about this?
For enabling and disabling select options, something like this JS fiddle could help. Notice that it disables the possibility of selecting the same thing in multiple select boxes.
As for the code sample you posted, you're missing a PHP closing bracket (}) after your foreach.
Also, you can use <?= $var ?> as a more readable shortcut for <?php echo $var ?>
Try this:
<?php
$products = [
[
'ID' => 123,
'name' => 'product1',
],
[
'ID' => 456,
'name' => 'product2',
],
];
$module_row = 0;
?>
<tbody id="module-row <?= $module_row; ?>">
<tr>
<td>
<select name="prod[]">
<?php foreach ($products as $product) { ?>
<option value="<?= $product['ID']; ?>">
<?= $product['name']; ?>
</option>
<?php } ?>
</td>
<td>
<a onClick="addProd();">Add New</a>]
</td>
</tr>
</tbody>
<?php $module_row++; ?>
Related
I'm making something for a stocktake.
I have a form that generates with 4 fields. item_code, item_name, packing, quantity
<form action="goods.php" method="post">
<table>
<!-- Headers -->
<tr>
<td><b>Item Code</b></td>
<td><b>Description</b></td>
<td><b>Packing</b></td>
<td><b>Quantity</b></td>
</tr>
<?php
for ($i = 0; $i <= 50; $i++) {
?>
<tr>
<td>
<INPUT TYPE="TEXT" NAME="item_code[<?php echo $i; ?>]" SIZE="6" VALUE="
<?php
if (!empty($_POST["item_code"][($i)])) {
echo $_POST["item_code"][($i)];
}
?>"></td>
<td><?php
if (!empty($_POST["item_code"][($i)])) {
$result = FetchData($_POST["item_code"][($i)]);
echo $result['category'];
}
?>
</td>
<td>
<?php
if (!empty($_POST["item_code"][($i)])) {
echo $result['item_name'];
}
?></td>
<td>
<?php
if (!empty($_POST["item_code"][($i)])) {
echo $result['packing'];
}
?></td>
<td><INPUT TYPE="TEXT" NAME="quantity[<?php echo $i; ?>]" SIZE="5" VALUE="
<?php
if (!empty($_POST["quantity"][($i)])) {
echo $_POST["quantity"][($i)];
} else {
echo "";
}
?>"></td>
A js function for the button
<script>
function fillForm(value) {
document.getElementById('value').innerHTML = value;
}
</script>
and a list that is generated with 3 fields. item_code, item_name, packing
<?php
$dbh = dbh_get();
$sql = 'SELECT * FROM goods as goods(item_code, sort) order by human_sort(goods.item_code)';
$v = array();
$stmt = $dbh->prepare($sql);
$stmt->execute();
while (true) {
$r = $stmt->fetch();
if (is_bool($r)) break;
print '
<tr>
<td class="buttonL" id="<php ' . $r['item_code'] . ' ?>" onclick="fillForm()">' . $r['item_code'] . '</td>
<td>' . $r['item_name'] . '</td>
<td>' . $r['packing'] . '</td>
</tr>' . "\n";
}
dbh_free($dbh);
?>
}
I want to put a button on each list row and when it's clicked it populates the first three fields in the form, leaving quantity to be filled out. Then when another is clicked it populates the next form row etc,. It's working fine manually entering from the list, but the list is nearly 5000 items so it's a hassle to keep searching then scrolling up and entering the values.
I don't see how to do this with PHP so I assume I need a javascript function, which is where I'm lost. Let me know if you need more info.
I want to display the data selection based on the retrieval of the foreach using chosen jquery, I have added remove () and chosen () and the chosen format does not work, how do I remove and update the chosen, is my syntax wrong? please help ... sorry if something unclear can be asked to me ..
This is my syntax for chosen
$('#acctAccessed').change(function(){
$('#acctAccessed').find('option').remove();
var opt = '';
<?php foreach ($data_from_ctr['account'] as $data) :?>
$('#acctAccessed').append('<option value="'<?php echo $data['idMst'] ?>'">'<?php echo $data['giroOB']; ?>'</option>');
<?php //endforeach ?>;
$(".chosen-select").trigger("chosen:updated");
});
This is a table format that I want to make chosen
<tr class="odd">
<td width="150"><strong>Account Access</strong></td>
<td width="10">:</td>
<td colspan="2">
<select id="acctAccessed" name="acctAccessed[]" multiple="multiple" class="chosen-select" style="width:350px;" data-placeholder="Select account">
</select>
<input id="chkall" type="checkbox" >Select All</input>
</td>
$('#acctAccessed').change(function(){
$('#acctAccessed').empty()
var opt = '';
<?php foreach ($data_from_ctr['account'] as $data) :?>
$('#acctAccessed').append('<option value="'<?php echo $data['idMst'] ?>'">'<?php echo $data['giroOB']; ?>'</option>');
<?php //endforeach ?>;
//$(".chosen-select").trigger("chosen:updated");
$("#acctAccessed").val($("#acctAccessed option:first").val());
});
try it
Please try this. I have rearrange your code and do some changes.
$('#acctAccessed').change(function(){
var opt = '';
<?php foreach ($data_from_ctr['account'] as $data) :?>
opt += '<option value="'<?php echo $data['idMst'] ?>'">'<?php echo $data['giroOB']; ?>'</option>';
<?php endforeach; ?>;
$('#acctAccessed').html(opt);
$(".chosen-select").trigger("chosen:updated");
});
I use this code to hide attributes when they do not have any data:
<?php foreach ($_additional[ 'items'] as $_data): ?>
<?php $_attribute=$ _product->getResource()->getAttribute($_data['code']); if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>
<tr>
<th class="label">
<?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data">
<?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
This works great, but not for attributes type dropdown.
How can I also hide dropdown attributes, that do not have any value?
If an attribute with the type dropdown isn't set, it usually displays as N/A on the front-end of your website, so you can simply add && $_data['value'] != 'N/A' to the if-statement
So the code would look something like this
<?php foreach ($_additional[ 'items'] as $_data): ?>
<?php $_attribute = $_product->getResource()->getAttribute($_data['code']); if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '' && $_data['value'] != 'N/A')) { ?>
<tr>
<th class="label">
<?php echo $this->htmlEscape($this->__($_data['label'])) ?>
</th>
<td class="data">
<?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
I have been toying with this for a while now and i can not get each row to send its specific values that are displayed to a javascript function only the 1st rows values are sent no matter which row is clicked?
I need to send the values for each specific row dependng on the results of the mysql result.
Below is the code that i have which only sends the values of the 1st row.
<?php
require 'core/init.php';
$records = array();
$result = ("(SELECT * FROM message ORDER BY id DESC LIMIT 25)ORDER BY id ASC");
$results = ($db->query($result));
if($results->num_rows){
while($row = $results->fetch_object()){
$records[] = $row;
}
$results->free();
}
if(!count($records)){
echo 'no records';
}else{
?>
<table>
<?php
foreach ($records as $r){
?>
<tr>
<td><div id="modOptions" onclick="modOptions()"><?php echo escape($r->sender); ?></div></td>
<td><?php echo escape($r->message); ?></td>
<input type="hidden" id="modOptionsIp" value="<?php echo escape($r->ip); ?>"/>
<input type="hidden" id="modOptionsSender" value="<?php echo escape($r->sender); ?>"/>
<input type="hidden" id="modOptionsMessage" value="<?php echo escape($r->message); ?>"/>
</tr>
<?php
}
?>
</table>
<?php
}
?>
It displays everything ok just doesnt give each row its specific values
Any pointers are much appreciated.
Change the js modOptions function so it can take parameters
function modOptions(ip, sender, message) {
//ip sender and message are from the row you clicked on
}
and render the onclick like this:
onclick="modOptions('<?php echo escape($r->ip); ?>', '<?php echo escape($r->sender); ?>', '<?php echo escape($r->message); ?>' );"
those hidden fields you have now are useless
Why does this form not update the values that the items in my session? The session seems to keep track of the value fine, before the form tries to allow users to edit the value. Here's what I wrote out form submit:
<?php
if(isset($_POST['submit'])){
foreach($_POST['quantity'] as $key => $val) {
if($val==0) {
unset($_SESSION['Cart'][$key]);
}else{
$_SESSION['Cart'][$key]['quantity']=$val;
}
}
}
?>
And here's the form:
<?php
$sql="SELECT * FROM products where Product_ID IN (";
foreach($_SESSION['Cart'] as $id => $value){
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY Category ASC";
$query=mysql_query($sql);
$totalquantity=0;
while($row=mysql_fetch_array($query)){
$subtotal=$_SESSION['Cart'][$row['Product_ID']['quantity']]['quantity'];
$totalquantity+=$subtotal;
?>
<tr>
<td><?php echo $row['Name'] ?></td>
<td><input = type="text" name="Quantity [<?php echo $row['Product_ID'] ?>]" size="5" value="<?php echo $_SESSION['Cart'][$row['Product_ID']['quantity']]['quantity'] ?>"/> </td>
</tr>
<?php
}
?>
And of course, the submit button is just
<button type="Submit" name="Submit">Update selection</button>
It looks like it should all work out properly, but it doesn't update.
Submit should be "submit" most probably.Change it either in the input filed or in the $_POST["Submit"]