Call php function when click a button - php

I have a code :
<?php
getPriceListHeader();
function getPriceListDetail($PriceListCode)
{
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = "SELECT * FROM pricelisdetail where pricelist_code='".$PriceListCode."'";
$results = $readConnection->fetchAll($query);
echo "<table id='tbdata'>
<thead>
<tr>
<th>Price List Code</th>
<th>Price List Name</th>
<th>Effective From</th>
<th>Effective To</th>
</tr>
</thead>
<tbody> ";
foreach ($results as $row)
{
echo "<tr>";
echo "<td> ".$row[entity_id];
echo "<td> ".$row[sku];
echo "<td> ".$row[sku];
echo "<td> ".$row[sku];
};
echo " </tbody>
</table> ";
}
function getPriceListHeader()
{
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = 'SELECT * FROM pricelistheader';
$results = $readConnection->fetchAll($query);
echo "
<h2>Price List</h2>
<div>
<h3>Please select Price List</h3>
<div>
<select class='element select large' id='pricelist' name='element_2'>";
foreach ($results as $row)
{
echo '<option value="' . $row[entity_id]. '">' . $row[sku] . '</option>';
}
echo "</select>
</div>
<input type='button' class='button' name='insert' value='Get Data' onclick='getPriceListDetail(pricelist.value)'/>
";
getPriceListDetail('');
}
?>
I have a dropdown list, a button, a table
When I select a value from dropdown list, then I click button , table will be filled data again. There are 2 method, getPriceListHeader(): load data header when load the page, getPriceListDetail : load data detail when click a button. I try to put event getPriceListDetail(value) to button, but when I click, nothing happens
Please help me how to do this.

Yes, you can call php via ajax request to server like this (very simple):
Note that the following code uses jQuery
jQuery.ajax({
type: "POST",
url: 'my_php_function.php',
dataType: 'name_the_data_type',
success: function (data) {
// here you will get the response your function
}
});
and my_php_function.php like this:
<?php
// here is your php code or function
?>
from the source How can I call PHP functions by JavaScript?

You can't attach PHP function to HTML. PHP is server-side language, so after it's displayed in users browser you can't refer to PHP again, unless you reload page.
There are 3 options I think you can do:
Reload page after changing select page and using GET prepare new data.
Send all data to user browser and than show only part of it related to selected option.
Use AJAX and ask server for new data in background.

Finally, I found out the way.It works for me. Now, I do not understand the code , I am trying to read and get it clearly.
Thanks all for your comments.
$to="";
$from="";
$show_order_statuses = 0;
$orserstatus = "";
$result_order = 0;
//var_dump($results);
//return;
if(!empty($_REQUEST['filter_type']))
{
$orders_row = array();
$filter_type = $_REQUEST['filter_type'];
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = " SELECT * FROM pricelistitem where pricelist_code='".$filter_type."' ";
// $query = 'SELECT * FROM pricelistheader1';
$results = $readConnection->fetchAll($query);
foreach ($results as $rowid)
{
$result_order=10;
$orders_row[]=array($rowid['pricelist_code'],$rowid['product_code'],number_format( $rowid['unitprice'],2),$rowid['UOM']);
// $orders_row[]=array(1,1,1,1,1);
}
}
?>
<div id="anchor-content" class="middle">
<div id="page:main-container">
<div class="content-header">
<table cellspacing="0">
<tbody>
<tr>
<td style="width:50%;"><h3 class="icon-head head-report-sales-sales"><?php echo $this->__("Price List");?></h3></td>
<td class="form-buttons"><button style="" onclick="filterFormSubmit.submit()" class="scalable " type="button" id="id_<?php echo Mage::getSingleton('core/session')->getFormKey() ?>"><span>Show Report</span></button></td>
</tr>
</tbody>
</table>
</div>
<div>
<div class="entry-edit">
<form method="get" action="<?php echo Mage::helper('core/url')->getCurrentUrl();?>" id="filter_form">
<?php /*?><input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" /><?php */?>
<div class="entry-edit-head">
<h4 class="icon-head head-edit-form fieldset-legend">Filter</h4>
<div class="form-buttons"></div>
</div>
<div id="sales_report_base_fieldset" class="fieldset">
<div class="hor-scroll">
<table cellspacing="0" class="form-list">
<tbody>
<tr>
<td class="label"><label for="sales_report_filter_type">Filter By <span class="required">*</span></label></td>
<td class="value">
<select class="required-entry select" name="filter_type" id="sales_report_filter_type" onchange="myFunction();">
<?php
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = " SELECT * FROM pricelistheader ";
$results = $readConnection->fetchAll($query);
$so=1;
foreach ($results as $row)
{
$selected='';
if ($filter_type==$row[pricelist_code])
$selected= ' selected=selected';
else
$selected='';
echo '<option value="' . $row[pricelist_code]. '" '.$selected.' >' . $row[pricelist_name].' '. $row[pricelist_code] . '</option>';
}
?>
</select>
</tr>
<tr>
<td class="label"><label for="effect_from">Effect From </label></td>
<td class="value">
<?php
foreach ($results as $row)
{
if ($filter_type==$row[pricelist_code])
echo $row[pricelist_fromdate];
}
?>
</td>
</tr>
<tr>
<td class="label"><label for="effect_from">Effect To </label></td>
<td class="value">
<?php
foreach ($results as $row)
{
if ($filter_type==$row[pricelist_code])
echo $row[pricelist_todate];
}
?>
</td>
</tr>
</tbody>
<script>
function myFunction() {
// document.getElementById("tbdata").deleteRow(1);
var rowCount = tbdata.rows.length;
for (var i = rowCount - 1; i > 0; i--) {
tbdata.deleteRow(i);
}
srt.value=pricelist.options[pricelist.selectedIndex].value;
";
//$('#tbdata').empty();
}
</script>
</table>
</div>
</div>
</form>
</div>
<script type="text/javascript">
//<![CDATA[
var filterFormSubmit = new varienForm('filter_form');
//]]>
</script>
<script type="text/javascript"> new FormElementDependenceController({"sales_report_order_statuses":{"sales_report_show_order_statuses":"1"}}); </script>
<style type="text/css">
.no-display{display:none;}
</style>
</div>
<div>
<?php if($result_order>0){?>
<table cellspacing="0" class="actions">
<tbody>
<tr>
<td class="pager"> </td>
<td class="export a-right">
<form method="post" action="<?php echo $this->getUrl('*/*/exportCsv')?>" id="csv_form_customer">
<input name="form_key_customer" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />
</form>
<script type="text/javascript">
//<![CDATA[
var csvFormSubmitcustomer = new varienForm('csv_form_customer');
//]]>
</script>
</td>
<td class="filter-actions a-right">
<img class="v-middle" alt="" src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);?>skin/adminhtml/default/default/images/icon_export.gif"> Export to:
<select style="width:8em;" id="sales_order_grid_export_customer" name="sales_order_grid_export_customer">
<option value="<?php echo $this->getUrl('*/*/exportCsv')?>">CSV</option>
</select>
<button onclick="csvFormSubmitcustomer.submit()" class="scalable task" type="button"><span>Export</span></button>
</td>
</tr>
</tbody>
</table>
<?php } ?>
<div id="id_customer<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" class="print_customer<?php echo Mage::getSingleton('core/session')->getFormKey() ?>">
<div class="grid">
<div class="hor-scroll">
<table cellspacing="0" id="id_customer<?php echo Mage::getSingleton('core/session')->getFormKey() ?>_table" class="data">
<colgroup>
<col>
<col>
</colgroup>
<thead>
<tr class="headings">
<th class=" no-link"><span class="nobr">Price List Code</span></th>
<th class=" no-link"><span class="nobr">Cust Code</span></th>
<th class=" no-link"><span class="nobr">Cust Name</span></th>
</tr>
</thead>
<tbody id="">
<?php
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = " SELECT * FROM " . $resource->getTableName('catalog/product');;
$customercount=0;
$customerresults = $readConnection->fetchAll($query);
$customerresults = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('erp_pricelistcode_1', $filter_type)
// ->addFieldToSelect (array('created_at','customer_id','increment_id','updated_at','status','entity_id','state'))
;
// ->addAttributeToFilter('erp_pricelistcode_1','00')->load();
$so=1;
foreach ($customerresults as $row) {
$customercount++;
}
// var_dump(#$customerresults);
if($customercount>0){
foreach($customerresults as $singlerows){
$cot=0;
{
echo "<tr>";
{
$cot++;
?>
<td>
<?php
echo $singlerows->getData('erp_pricelistcode_1');
?>
</td>
<td>
<?php
echo $singlerows->getFirstname();
?>
</td>
<td>
<?php
echo $singlerows->getName();
?>
</td>
<?php
}
echo "</tr>";
}
}
}else{
?>
<tr class="even">
<td colspan="13" class="empty-text a-center">No records found.</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div>
<?php if($result_order>0){?>
<table cellspacing="0" class="actions">
<tbody>
<tr>
<td class="pager"> </td>
<td class="export a-right">
<form method="post" action="<?php echo $this->getUrl('*/*/exportCsv')?>" id="csv_form">
<input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />
</form>
<script type="text/javascript">
//<![CDATA[
var csvFormSubmit = new varienForm('csv_form');
//]]>
</script>
</td>
<td class="filter-actions a-right">
<img class="v-middle" alt="" src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);?>skin/adminhtml/default/default/images/icon_export.gif"> Export to:
<select style="width:8em;" id="sales_order_grid_export" name="sales_order_grid_export">
<option value="<?php echo $this->getUrl('*/*/exportCsv')?>">CSV</option>
</select>
<button onclick="csvFormSubmit.submit()" class="scalable task" type="button"><span>Export</span></button>
</td>
</tr>
</tbody>
</table>
<?php } ?>
<div id="id_<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" class="print_<?php echo Mage::getSingleton('core/session')->getFormKey() ?>">
<div class="grid">
<div class="hor-scroll">
<table cellspacing="0" id="id_<?php echo Mage::getSingleton('core/session')->getFormKey() ?>_table" class="data">
<colgroup>
<col>
<col>
<col>
<col>
</colgroup>
<thead>
<tr class="headings">
<th class=" no-link"><span class="nobr">Price List Code</span></th>
<th class=" no-link"><span class="nobr">Product Code</span></th>
<th class=" no-link"><span class="nobr">Unit Price</span></th>
<th class=" no-link"><span class="nobr">UOM</span></th>
</tr>
</thead>
<tbody id="">
<?php
$cot=0;
if(count($orders_row)>0){
foreach($orders_row as $singlerows){
$cot=0;
if(!empty($singlerows)){
echo "<tr>";
foreach($singlerows as $value){
$cot++;
?>
<td>
<?php
if ($cot==3 )
echo "<div style='float:right;width:30%;'>";
echo $value;
echo "</div>";
?>
</td>
<?php
}
echo "</tr>";
}
}
}else{
?>
<tr class="even">
<td colspan="13" class="empty-text a-center">No records found.</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>

Related

How do I stop user from leaving quantity empty?

So this is my order page and I want to add an option which prevents the user from leaving the quantity option empty. I cannot seem to use the required option as it compels the user to fill every box instead of the one just selected.
Here is the code
<body>
<?php include('navbar.php'); ?>
<div class="container">
<h1 class="page-header text-center">ORDER</h1>
<form method="POST" action="purchase.php">
<table class="table table-striped table-bordered">
<thead>
<th class="text-center"><input type="checkbox" id="checkAll"></th>
<th class="productheading">Category</th>
<th class="productheading">Product Image
<th class="productheading">Product Name</th>
<th class="productheading">Price</th>
<th class="productheading">Quantity</th>
</thead>
<tbody>
<?php
$sql = "select * from product left join category on category.categoryid=product.categoryid order by product.categoryid asc, productname asc";
$query = $conn->query($sql);
$iterate = 0;
while ($row = $query->fetch_array()) {
?>
<tr>
<td class="text-center"><input type="checkbox" value="<?php echo $row['productid']; ?>||<?php echo $iterate; ?>" name="productid[]" style=""></td>
<td><?php echo $row['catname']; ?></td>
<td><a href="<?php if (empty($row['photo'])) {
echo "upload/noimage.jpg";
} else {
echo $row['photo'];
} ?>"><img src="<?php if (empty($row['photo'])) {
echo "upload/noimage.jpg";
} else {
echo $row['photo'];
} ?>" height="170px" width="80%"></a></td>
<td class="productname1"><?php echo $row['productname']; ?></td>
<td class="price">Rs <?php echo number_format($row['price'], 2); ?></td>
<!-->**HERE IS THE CODE THAT NEEDS TO BE FIXED**--> <td><input type="number" class="form-control" name="quantity<?php echo $iterate; ?>"></td>
</tr>
<?php
$iterate++;
}
?>
</tbody>
</table>
<div class="row">
<div class="col-md-3">
<input type="text" name="customer" class="form-control" placeholder="Customer Name" required>
</div>
<div class="col-md-3">
<input type="number" name="number" class="form-control" placeholder="Contact Number" required>
</div>
<div class="col-md-2" style="margin-left:-20px;">
<button type="submit" onclick="myFunction()" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Order</button>
<br />
<br />
<br />
</div>
</div>
</form>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#checkAll").click(function() {
$('input:checkbox').not(this).prop('checked', this.checked);
});
});
</script>
</body>
</html>
You can target those fields with jQuery/JavaScript and make it required by focusing on it and then prevent the form from submitting. Try this
$(document).ready(function() {
$('#order-form').submit(function(e){
let $quantities = $(this).find('.table input[type="number"]').filter(function(){
return $(this).closest('tr').find('input[type="checkbox"]').is(':checked') && $(this).val() === '';
})
if( $quantities.length > 0 ){
e.preventDefault();
$quantities.first().focus()
}
})
});
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="container">
<form method="POST" action="purchase.php" id="order-form">
<table class="table table-striped table-bordered">
<thead>
<th class="text-center"><input type="checkbox" id="checkAll"></th>
<th class="productheading">Category</th>
<th class="productheading">Product Image
<th class="productheading">Product Name</th>
<th class="productheading">Price</th>
<th class="productheading">Quantity</th>
</thead>
<tbody>
<tr>
<td class="text-center"><input type="checkbox" value="1" name="productid[]" checked></td>
<td>Dishes</td>
<td><img src="https://via.placeholder.com/150/0000FF/FFFFFF?text=Bara" height="170px" width="80%"></td>
<td class="productname1">Bara</td>
<td class="price">Rs 79.00</td>
<td><input type="number" class="form-control" name="quantity[]"></td>
</tr>
<tr>
<td class="text-center"><input type="checkbox" value="1" name="productid[]"></td>
<td>Dishes</td>
<td><img src="https://via.placeholder.com/150/FF0000/FFFFFF?text=Chwela" height="170px" width="80%"></td>
<td class="productname1">Chwela</td>
<td class="price">Rs 120.00</td>
<td><input type="number" class="form-control" name="quantity[]"></td>
</tr>
</tbody>
</table>
<button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Order</button>
</form>
</div>
Answer in format requested by the OP
<body>
<?php include('navbar.php'); ?>
<div class="container">
<h1 class="page-header text-center">ORDER</h1>
<form method="POST" action="purchase.php" id="order-form">
<table class="table table-striped table-bordered">
<thead>
<th class="text-center"><input type="checkbox" id="checkAll"></th>
<th class="productheading">Category</th>
<th class="productheading">Product Image
<th class="productheading">Product Name</th>
<th class="productheading">Price</th>
<th class="productheading">Quantity</th>
</thead>
<tbody>
<?php
$sql = "select * from product left join category on category.categoryid=product.categoryid order by product.categoryid asc, productname asc";
$query = $conn->query($sql);
$iterate = 0;
while ($row = $query->fetch_array()) {
?>
<tr>
<td class="text-center"><input type="checkbox" value="<?php echo $row['productid']; ?>||<?php echo $iterate; ?>" name="productid[]" style=""></td>
<td><?php echo $row['catname']; ?></td>
<td><a href="<?php if (empty($row['photo'])) {
echo "upload/noimage.jpg";
} else {
echo $row['photo'];
} ?>"><img src="<?php if (empty($row['photo'])) {
echo "upload/noimage.jpg";
} else {
echo $row['photo'];
} ?>" height="170px" width="80%"></a></td>
<td class="productname1"><?php echo $row['productname']; ?></td>
<td class="price">Rs <?php echo number_format($row['price'], 2); ?></td>
<td><input type="number" class="form-control" name="quantity<?php echo $iterate; ?>"></td>
</tr>
<?php
$iterate++;
}
?>
</tbody>
</table>
<div class="row">
<div class="col-md-3">
<input type="text" name="customer" class="form-control" placeholder="Customer Name" required>
</div>
<div class="col-md-3">
<input type="number" name="number" class="form-control" placeholder="Contact Number" required>
</div>
<div class="col-md-2" style="margin-left:-20px;">
<button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Order</button>
<br />
<br />
<br />
</div>
</div>
</form>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#checkAll").click(function() {
$('input:checkbox').not(this).prop('checked', this.checked);
});
$('#order-form').submit(function(e){
let $quantities = $(this).find('.table input[type="number"]').filter(function(){
return $(this).closest('tr').find('input[type="checkbox"]').is(':checked') && $(this).val() === '';
})
if( $quantities.length > 0 ){
e.preventDefault();
alert("Quantity is required")
$quantities.first().focus()
}
})
});
</script>
</body>
In case of users who have disabled Javascript, you should also check on the server for missing quantities:
// purchase.php
if (isset($_POST['productid'])) {
for ( $i=0; $i < sizeof($_POST['productid']); $i++ ) {
if ( $_POST['productid'][$i] && ! $_POST['quantity'][$i] ) {
echo "Missing quantity for " . $_POST['productid'][$i] . "<br>";
}
}
}

when i do form the popup window displays in just a second then hide again

I have popup it works normal i need to use method post to get my value from the input balise but the problem is when i do form the popup window displays in just a second then hide again and I need to click two time becausethe first time i got just the last result
<div class="List_modify">
<table class="list">
<thead class="topBar">
<tr>
<th>Prenom</th>
<th>Nom</th>
<th>CIN</th>
<th>Emails</th>
<th>Références</th>
<th>Photos</th>
<th>Niveau</th>
<th class="button_editer" style="vertical-align:middle"><span>Editer</span></th>
</tr>
</thead>
<?php $requet = "select * from Etudiants";
$resultat = mysqli_query($connecte, $requet);
while ($r = mysqli_fetch_array($resultat)) {
?>
<tbody class="container">
<tr>
<td>
<?php echo $r['Prenom']; ?>
</td>
<td>
<?php echo $r['Nom']; ?>
</td>
<td>
<?php echo $r['CIN']; ?>
</td>
<td>
<?php echo $r['Emails']; ?>
</td>
<td>
<?php echo $r['Matricule']; ?>
</td>
<td> <img src="image/<?php echo $r['Photo']; ?> " style="width: 150px; height:150px;"></td>
<td>
<?php echo $r['Niveau']; ?> </td>
<td>
<form method="POST" action="">
<input type="hidden" name="id" value="<?php echo $r['Id_Etudiant']; ?>">
<button class=" button" onclick="show()" style="vertical-align:middle" type="submit" name="submit"><span>Editer<i class=" fas fa-user-edit"></i></span>
</button>
</form>
</td>
</tr>
</tbody>
<?php } ?>
</table>
</div>
<div class="background_page_modify" id="page_modify" onclick="hide()">
<div class="page_etudiant" onclick="event.stopPropagation()">
<div class="closebtn" id="closebtn" onclick="hide()">
<div class="circle"> +</div>
</div>
<div class="etudiant_box">
<?php
if (isset($_REQUEST['submit'])) {
$checkid = $_REQUEST['id'];
echo $checkid;
}
?>
</div>
</div>
</div>

Get php checkbox data from multiple tables

First I will tell what I want to achieve, and then I will explain how I was trying to do it.
I have two tables, one stores type of vessels with a description and their own Id. Then I have another table in wich the vessel type has been stored as text. My goal is to select each one (both records in both tables) with a checkbox in both and store in the table 2 the Id from the table 1.
I will introduce data, to help.
Table 1
1|Balandra|Some description
2|Bergantin|Some description
3|Whatever |Whatever.....
Table2
Balandra
Bergantin
Whatever
Then, I have created a php page that shows both tables with the checkbox I mentioned above. Checkboxes store the Table1 Id and Table2 vesseltypename.
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th class="hidden-xs">idtiponavio</th>
<th>Tipo de Navío</th>
<th>Descripción</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosdyncoop as $key => $navio) { ?>
<tr>
<td align="center">
<a href=<?php echo '../vista/modificar.php?id=' .
$navio['idtiponavio']; ?> class="btn btn-default"><em class="fa fa-
pencil"></em></a>
<a href=<?php echo '../datos/borrar.php?id=' .
$navio['idtiponavio']; ?> class="btn btn-default"><em class="fa fa-
trash"></em></a>
</td>
<td class="hidden-xs"><?php echo
$navio['idtiponavio']; ?></td>
<td><?php echo $navio['tiponavio']; ?></td>
<td><?php echo $navio['descripcion']; ?></td>
<td><input type="checkbox" name="agruparid" value=<?
php echo $navio['idtiponavio']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="col-md-6">
<div class="panel-body paneltodo">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th>Tipo de Navío</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosforsea as $key => $navio) { ?>
<tr>
<td align="center">
<em class="fa fa-arrow-circle-o-left"></em>
</td>
<td><?php echo $navio['typevessel']; ?></td>
<td><input type="checkbox" name="agruparvessel" value=<?php echo $navio['typevessel']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
So, I want to check both table records and store the table1 Id in the table2 idtypevessel field.
I thought that a php file could store both items and call the update function with those parameters, like this:
<?php
require './modelo.php';
$idnavio = $_GET['agruparid'];
$vessel = $_GET['agruparvessel'];
Any suggestions, because I think I have to do a button to submit this parameters, but it must be working on both tables, and I don't know how to access both foreach loop at the same time.
Thanks in advance.
E. Salas
review bellow reference code to submit multi selected checkbox values
for multi selected checkbox submission you must use [] operator after name attribute in html
index.php
<form action="/checkbox.php" method="post">
<strong>Cars:</strong><br>
<?php
$cars = array("Volvo", "BMW", "Toyota");
$colors = array("Red", "Green", "Black");
foreach($cars as $single){
?>
<input type="checkbox" name="cars[]" value="<?php echo $single; ?>">
<?php
}
<br>
<strong>colors:</strong><br>
foreach($colors as $single){
?>
<input type="checkbox" name="colors[]" value="<?php echo $single; ?>">
<?php
}
?>
<br>
<input type="submit" value="Submit!">
</form>
checkbox.php
<?php
echo "<pre>";
var_dump($_POST);
exit;
In your case:
<form action="/checkbox.php" method="post">
<div class="col-md-6">
<div class="panel-body">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th class="hidden-xs">idtiponavio</th>
<th>Tipo de Navío</th>
<th>Descripción</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosdyncoop as $key => $navio) { ?>
<tr>
<td align="center">
<em class="fa fa-pencil"></em>
<em class="fa fa-trash"></em>
</td>
<td class="hidden-xs"><?php echo $navio['idtiponavio']; ?></td>
<td><?php echo $navio['tiponavio']; ?></td>
<td><?php echo $navio['descripcion']; ?></td>
<td><input type="checkbox" name="agruparid[]" value=<?php echo $navio['idtiponavio']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="col-md-6">
<div class="panel-body">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th>Tipo de Navío</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosforsea as $key => $navio) { ?>
<tr>
<td align="center">
<em class="fa fa-arrow-circle-o-left"></em>
</td>
<td><?php echo $navio['typevessel']; ?></td>
<td><input type="checkbox" name="agruparvessel[]" value=<?php echo $navio['typevessel']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<input type="submit" value="Submit!">
</form>
Finally I solved this with Javascript. One of my partners helped me, I post this to help people in my situation.
I created a script, here is the code:
<script type="text/javascript">
function cogeNavioDyncoopnet(){
var checkedValueNavioD = null;
var inputElements = document.getElementsByClassName('checknaviod');
for(var i=0; inputElements[i]; ++i){
if(inputElements[i].checked){
checkedValueNavioD = inputElements[i].value;
break;
}
}//return checkedValueNavioD;
var input_nav_dyn = document.getElementById("nav_dyn");
input_nav_dyn.value = checkedValueNavioD;
}
function cogeNavioForSea(){
var checkedValueNavioFs = null;
var inputElements = document.getElementsByClassName('checknaviofs');
for(var i=0; inputElements[i]; ++i){
if(inputElements[i].checked){
checkedValueNavioFs = inputElements[i].value;
break;
}
}//return checkedValueNavioFs;
var input_nav_fs = document.getElementById("nav_fs");
input_nav_fs.value = checkedValueNavioFs;
}
</script>
Then I created a Form below, that collects values and sends them to my .php control file.
<div class="hidden-xs">
<form class="hidden-xs" method="POST"
action="../datos/actualizarnavios.php">
<input id="nav_dyn" type="text" name="idnaviodyncoop">
<input id="nav_fs" type="text" name="navioforsea" >
<input id="botonasignar" type="submit" name="enviardatosdynfs">
</form>
</div>
I hope this helps. Thanks for the feedback, as always.

downloading Mp3 file form mysql DB and Increasing number of downloads per click

I want to desing an e-music php web base application and i want to achive the forllowing:
1. on cliking on the download button the file should be downloaded
2. The number of downloads of that file should be incremented by 1
The following code show list of music i.e new releases and top downloads
<?php
$hostname_conn = "localhost";
$database_conn = "e-music";
$username_conn = "root";
$password_conn = "";
$conn = mysqli_connect($hostname_conn, $username_conn, $password_conn, $database_conn) or trigger_error(mysqli_error());
$sql="SELECT * from music INNER JOIN artist on music.a_id=artist.a_id INNER JOIN category on music.cat_id=category.cat_id order by upload_date Desc limit 0,3";
$Result= mysqli_query($conn,$sql) or die('Cannot Retrive Record' . mysqli_error());
$sql1="SELECT * from music INNER JOIN artist on music.a_id=artist.a_id INNER JOIN category on music.cat_id=category.cat_id order by downloads Desc limit 0,3";
$Result1= mysqli_query($conn,$sql1) or die('Cannot Retrive Record' . mysqli_error());
?>
<table align="center" class="table table-striped table-hover ">
<thead>
<tr>
<th colspan="3" align="center"> <h3>New Releases </h3></th>
</tr>
</thead>
<tbody>
<?php while ($row=mysqli_fetch_assoc($Result)) { ?>
<tr>
<td class="col-md-6"> <div class="col-md-2"><img src="uploads/<?php echo $row['artist_image']; ?>" class="img-responsive ims" alt="Image" >
</div>
<label> Artist: <?php echo $row['artist_name'] ?></label> <br/>
<label> Title: <?php echo $row['m_title'] ?></label> <br/>
<label>Category: Wakoin <?php echo $row['cat_name'] ?></label>
</td>
<td>
<a class="btn btn-xs btn-primary" href="download.php?did=<?php echo $row['mid'];?>"> Downloader</a>
</td>
<td class="col-md-2">
Format:<?php echo $row['m_format']; ?>
</td>
<td class="col-md-2">
File Size: <?php echo round($row['size']/1048576,2); ?> MB
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<table align="center" class="table table-striped table-hover">
<thead>
<tr>
<th colspan="3" align="center"> <h3>Top Downloads </h3></th>
</tr>
</thead>
<tbody>
<?php while ($row=mysqli_fetch_assoc($Result1)) { ?>
<tr>
<td class="col-md-6"> <div class="col-md-2"><img src="uploads/<?php echo $row['artist_image']; ?>" class="img-responsive ims" alt="Image" >
</div>
<label> Artist: <?php echo $row['artist_name'] ?></label> <br/>
<label> Title: <?php echo $row['m_title'] ?></label> <br/>
<label>Category: Wakoin <?php echo $row['cat_name'] ?></label>
</td>
<td class="col-md-2">
<a class="btn btn-xs btn-primary" href="download.php?did=<?php echo $row['mid'];?>"> Downloader</a>
</td>
<td class="col-md-2">
Format: <?php echo $row['m_format']; ?>
</td>
<td>
File Size :<?php echo round($row['size']/1048576,2); ?> MB
</td>
</tr>
<?php
}
?>
</tbody>
</table>
The code i tried for the downloads on cliking the "downloader" Button which redirects to download.php file is below:
$hostname_conn = "localhost";
$database_conn = "e-music";
$username_conn = "root";
$password_conn = "";
$conn = mysqli_connect($hostname_conn, $username_conn, $password_conn, $database_conn) or trigger_error(mysqli_error());
$did=$_GET['did'];
$sql="SELECT target from music where mid='$did'";
$Result= mysqli_query($conn,$sql) or die('Cannot Retrive Record' . mysqli_error());
$row = mysqli_fetch_assoc($Result);
// the target filed contains the directory and file name of the file to be downloaded e.g uploads/Akon_Sorry Blame.mp3
echo "<a href=". $row['target'] ." > download</a>";
$sql1="update music set download=download+1 where mid='$did";
$Result= mysqli_query($conn,$sql1) or die('Cannot Increment number of downloads' . mysqli_error());
After research on my project i am able to solve my problem using ajax. Here is the code i used to achieve my objectives
//index page
<!DOCTYPE html>
<html>
<head>
<title>E-music</title>
<link href="includes/Style.css" rel="stylesheet" type="text/css"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<script src="jQuery/jquery-1.11.1.min.js" type="text/javascript"></script>
<script src="bootstrap-3.3.7-dist/js/bootstrap.min.js" type="text/javascript"></script>
<script src="bootstrap-3.3.7-dist/js/site.js" type="text/javascript"></script>
<?php
require_once("includes/connection.php");
$sql="SELECT * from music INNER JOIN artist on music.a_id=artist.a_id INNER JOIN category on music.cat_id=category.cat_id order by upload_date Desc limit 0,3";
$Result= mysqli_query($conn,$sql) or die('Cannot Retrive Record' . mysqli_error());
$sql1="SELECT * from music INNER JOIN artist on music.a_id=artist.a_id INNER JOIN category on music.cat_id=category.cat_id order by downloads Desc limit 0,3";
$Result1= mysqli_query($conn,$sql1) or die('Cannot Retrive Record' . mysqli_error());
?>
<div id="nav">
Search For Music Audio
<p>Wakokin Hausa Na Gargajiya
</p>
</div>
<div id="section">
<table align="center" class="table table-striped table-hover ">
<thead>
<tr>
<th colspan="3" align="center"> <h3>New Releases </h3></th>
</tr>
</thead>
<tbody>
<?php while ($row=mysqli_fetch_assoc($Result)) { ?>
<tr>
<td class="col-md-6"> <div class="col-md-2"><img src="uploads/<?php echo $row['artist_image']; ?>" class="img-responsive ims" alt="Image" >
</div>
<label> Artist: <?php echo $row['artist_name'] ?></label> <br/>
<label> Title: <?php echo $row['m_title'] ?></label> <br/>
<label>Category: Wakoin <?php echo $row['cat_name'] ?></label>
</td>
<td>
<a href="<?php echo $row['target'] ?>" d="<?php echo $row['mid'] ?>" class="dload btn btn-xs btn-primary" download>Download</a>
</td>
<td class="col-md-2">
Format:<?php echo $row['m_format']; ?> <br>
Total Download: <?php echo $row['downloads']; ?>
</td>
<td class="col-md-2">
File Size: <?php echo round($row['size']/1048576,2); ?> MB
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<table align="center" class="table table-striped table-hover">
<thead>
<tr>
<th colspan="3" align="center"> <h3>Top Downloads </h3></th>
</tr>
</thead>
<tbody>
<?php while ($row=mysqli_fetch_assoc($Result1)) { ?>
<tr>
<td class="col-md-6"> <div class="col-md-2"><img src="uploads/<?php echo $row['artist_image']; ?>" class="img-responsive ims" alt="Image" >
</div>
<label> Artist: <?php echo $row['artist_name'] ?></label> <br/>
<label> Title: <?php echo $row['m_title'] ?></label> <br/>
<label>Category: Wakoin <?php echo $row['cat_name'] ?></label>
</td>
<td class="col-md-2">
<a href="<?php echo $row['target'] ?>" d="<?php echo $row['mid'] ?>" class="dload btn btn-xs btn-primary" download>Download</a>
</td>
<td class="col-md-2">
Format: <?php echo $row['m_format']; ?>
Total Download: <?php echo $row['downloads']; ?>
</td>
<td>
File Size :<?php echo round($row['size']/1048576,2); ?> MB
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
//Site.js an ajax file to handle download incrementation
$(function() {
$(".dload").click(function(){
var id = $(this).attr("d");
$.ajax({
type: "POST",
url: "count.php",
data: {id:id},
success: function(info)
{
},
error: function(){
}
});
});
});
//conut.php to handle the databae update
<?php
require_once("includes/connection.php");
$id = $_POST["id"];
$Result= mysqli_query($conn,"SELECT downloads as total FROM music WHERE mid='$id'");
$Res = mysqli_fetch_assoc($Result);
$re = $Res['total'] + 1;
$updateCount = mysqli_query($conn, "UPDATE music SET downloads= '$re' WHERE mid=$id ");
?>

hide jQuery table row

I've got a search done with jQuery..
<script type="text/javascript" src="ajax.js"></script>
<div style="width:400px; margin:auto;">
<form action="clientes.php" method="post">
<legend>Procurar cliente</legend>
<input type="text" id="nome" name="nome" value="<?php echo $nome; ?>" style="width: 280px; height: 23px;" /><input type="button" name="btnPesquisar" value="Pesquisar" onclick="getDados();"/></form>
</div>
<div id="resultado"></div>
and getDados returns the results from another page, which is:
<?php
header('Content-Type: text/html; charset=utf-8');
include "conexao.php";
if ($_GET['nome']) {
$nome = $_GET['nome'];
$result = mysql_query("SELECT * FROM cliente WHERE nome LIKE '%".$nome."%' OR cpf LIKE '%".$nome."%' ORDER BY nome ASC") or die('Invalid query: ' . mysql_error());
$num_rows = mysql_num_rows($result);
}
if ($num_rows != 0) {
?>
<head><script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"> </script></head>
<script type="text/javascript">
$(document).ready(function() {
$('.hideme td').hide();
});
</script>
<table border=4 width="800px" align="center">
<tr><th>Cliente</th> <th>CPF</th> <th>Telefone</th> <th> Aniversário </th><th> Endereço </th></tr>
<?php
$i = "1";
while ($row = mysql_fetch_assoc($result)) {
?>
<TR>
<td width="220px" align="center"> <?php echo $row['nome']; ?> </td>
<td width="80px" align="center"> <?php echo $row['cpf']; ?> </td>
<td width="120px" align="center"><?php echo $row['telefone'] ?> </td>
<td width="120px" align="center"><?php echo $row['dataNascimento']; ?></a></td>
<td width="270px" align="center"><?php echo $row['endereco']; ?></td>
</TR>
<tr class="hideme"><td><form id="nomepost<?php echo $i; ?>" action="agrupaclienteteste.php" method="post">
<input type="hidden" name="nome" value="<?php echo $row['nome']; ?>"></form></td</tr>
<?php
$i++;
}
}
else{
if ($nome) { echo "Não há dados cadastrados em nosso sistema."; }
}
?>
Now.. the thing is, if I run the page of the results by itself.. lets say
result.php?nome=I
it hides the tr class=hideme...
if I access the search page and search for nome = I, it shows the results but it doesn't hide the tr class=hideme ...
Can anyone help me?
Why don't just use style="display:none;" as attribute value, since you are trying to hide onload itself

Categories