I have the code below. It runs fine until the last point. It seems to just just off and stop displaying the rest of the page after the // Close Main while loop comment and I cannot seem to work out why.
<?php
//show everything
error_reporting(E_ALL);
//using php.ini and ini_set()
ini_set('error_reporting', E_ALL);
require_once('header.tpl');
$my_id = '19';
mysql_connect("localhost","*********","*********") or die (mysql_error());
mysql_select_db("*********") or die (mysql_error());
?>
<!-- Navbar/popouts/notloggedin end -->
<script language="javascript" type="text/javascript">
function toggleChecks(field) {
if (document.myform.toggleAll.checked == true){
for (i = 0; i < field.length; i++) {
field[i].checked = true;
}
} else {
for (i = 0; i < field.length; i++) {
field[i].checked = false;
}
}
}
$(document).ready(function() {
$(".toggle").click(function () {
if ($(this).next().is(":hidden")) {
$(".hiddenDiv").hide();
$(this).next().slideDown("fast");
} else {
$(this).next().hide();
}
});
});
function markAsRead(msgID) {
$.post("markAsRead.php",{ messageid:msgID, ownerid:<?php echo $my_id; ?> } ,function(data) {
$('#subj_line_'+msgID).addClass('msgRead');
// alert(data); // This line was just for testing returned data from the PHP file, it is not required for marking messages as read
});
}
</script>
<style type="text/css">
.hiddenDiv {
display:none
}
#pmFormProcessGif {
display:none
}
.msgDefault {
font-weight:bold;
}
.msgRead {
font-weight:100;
color:#666;
}
</style>
<div class="container">
<div class="row">
<!-- /Welcome text -->
<div class="span12">
<div class="navbar navbar-inverse" style="position: static;">
<div class="navbar-inner">
<div class="container"><a class="btn btn-navbar" data-toggle="collapse" data-target=".navbar-inverse-collapse"><span class="icon-bar"></span> <span class="icon-bar"></span><span class="icon-bar"></span></a>
<div class="nav-collapse collapse navbar-inverse-collapse">
<ul class="nav">
<li>Inbox</li>
<li>Sent</li>
<li>Deleted</li>
<form class="navbar-search pull-left" action="">
<input type="text" id="inbox_search" class="search-query span2" placeholder="Search Emails" />
</form>
</ul>
<ul class="nav pull-right">
<li><i class="icon-file icon-white"></i></li>
<li class="dropdown"> <i class="icon-wrench icon-white"></i>
<ul class="dropdown-menu">
<li>Create New Folder</li>
<li>Message Templates</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<!-- /navbar-inner -->
</div>
</div>
</div>
<div class="row">
<div class="span12">
<table class="fulltable">
<tr>
<td class="fulltable_topbar" colspan="2"></td>
</tr>
<tr>
<td class="fulltable_title"><div></div></td>
</tr>
<tr>
<td class="fulltable_content" style="padding-top:10px"><div>
<!-- START THE PM FORM AND DISPLAY LIST -->
<form name="myform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<table width="94%" border="0" align="center" cellpadding="4">
<tr>
<td width="3%" align="right" valign="bottom"><img src="images/crookedArrow.png" width="16" height="17" alt="Develop PHP Private Messages" /></td>
<td width="97%" valign="top"><input type="submit" name="deleteBtn" class="btn btn-small btn-invese" id="deleteBtn" value="Delete" />
<span id="jsbox" style="display:none"></span></td>
</tr>
</table>
<table width="96%" border="0" align="center" cellpadding="4" style="background-image:url(images/headerStrip.jpg); background-repeat:repeat-x; border: #999 1px solid;">
<tr>
<td width="4%" valign="top"><input name="toggleAll" id="toggleAll" type="checkbox" onclick="toggleChecks(document.myform.cb)" /></td>
<td width="20%" valign="top">From</td>
<td width="58%" valign="top"><span class="style2">Subject</span></td>
<td width="18%" valign="top">Date</td>
</tr>
</table>
<?php
///////////End take away///////////////////////
// SQL to gather their entire PM list
$sql = mysql_query("SELECT * FROM private_messages WHERE to_id='$my_id' AND recipientDelete='0' ORDER BY id DESC LIMIT 100");
while($row = mysql_fetch_array($sql) **or die(mysql_error())**){ <error was the or die!
$date = strftime("%b %d, %Y",strtotime($row['time_sent']));
if($row['opened'] == "0"){
$textWeight = 'msgDefault';
} else {
$textWeight = 'msgRead';
}
$fr_id = $row['from_id'];
// SQL - Collect username for sender inside loop
$ret = mysql_query("SELECT id, username FROM members WHERE id='$fr_id' LIMIT 1");
while($raw = mysql_fetch_array($ret)){ $Sid = $raw['id']; $Sname = $raw['username']; }
?>
<table width="96%" border="0" align="center" cellpadding="4">
<tr>
<td width="4%" valign="top"><input type="checkbox" name="cb<?php echo $row['id']; ?>" id="cb" value="<?php echo $row['id']; ?>" /></td>
<td width="20%" valign="top"><?php echo $Sname; ?></td>
<td width="58%" valign="top"><span class="toggle" style="padding:3px;"> <a class="<?php echo $textWeight; ?>" id="subj_line_<?php echo $row['id']; ?>" style="cursor:pointer;" onclick="markAsRead(<?php echo $row['id']; ?>)"><?php echo stripslashes($row['subject']); ?></a> </span>
<div class="hiddenDiv"> <br />
<?php echo stripslashes(wordwrap(nl2br($row['message']), 54, "\n", true)); ?> <br />
<br />
REPLY<br />
</div></td>
<td width="18%" valign="top"><span style="font-size:10px;"><?php echo $date; ?></span></td>
</tr>
</table>
<hr style="margin-left:20px; margin-right:20px;" />
<?php
}// Close Main while loop
?>
</form>
<!-- END THE PM FORM AND DISPLAY LIST --> </div></td>
</tr>
<tr>
<td class="fulltable_bottom" colspan="2"></td>
</tr>
</table>
</div>
</div>
</body>
</br>
</br>
<?php require_once('footer.tpl'); ?>
why the ; after } (please remove)
} // Close Main while loop
same error here:
if($row['opened'] == "0"){
$textWeight = 'msgDefault';
} else {
$textWeight = 'msgRead';
};
You added some semicolons ; (which are not needed) after both of your loop closin curly braces }
};
$fr_id = $row['from_id'];
Should be
}
$fr_id = $row['from_id'];
And also
};// Close Main while loop
Has to be
}// Close Main while loop
just a heads up. While your bug seems to have been cleanly solved, I would suggest getting an IDE that can detect these types of issues. This way you won't spend the rest of your career looking for semi-colons. This is a link to Aptana, which is a code editor that has a lot of built in error detecting. Basically red lines pop up under lines that contain basic errors, things like a missing ; or ). Good luck dude.
http://www.aptana.com/
$sql = mysql_query("SELECT * FROM private_messages WHERE to_id='$my_id' AND recipientDelete='0' ORDER BY id DESC LIMIT 100");
while($row = mysql_fetch_array($sql) **or die(mysql_error())**){ <error was the or die!
the error is above :)
Related
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>";
}
}
}
I want to retreive information in MySQL database and display it html table.when i try with this code i get error message.i cant resolve that.i am new to php.help me. here is my code
<?php
session_start();
?>
<?php
$conn = mysqli_connect("localhost","root","","doctor");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST['button'])) {
$sql="select Mid,Mname,Mnic,amount,month,bank from payments ";
}
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Approvals</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<br>
<br>
<div class="table-responsive">
<table id="mytable" class="table table-bordred table-striped" border="1">
<thead style="background-color: sandybrown">
<th><input type="checkbox" id="checkall" /></th>
<th>Doctor ID</th>
<th>Doctor Name</th>
<th>Payment Type</th>
<th>Mobile Number</th>
<th>Email</th>
<th>Pay Date</th>
</thead>
<tbody>
<tr>
<td><input type="checkbox" class="checkthis" /></td>
<?php
while( $row = mysqli_fetch_assoc($sql)): ?>
<td > <?php echo $row['Mid']; ?> </td >
<td > <?php echo $row['Mname']; ?></td >
<td > <?php echo $row['Mnic']; ?> </td >
<td > <?php echo $row['amount']; ?></td >
<td > <?php echo $row['month']; ?> </td >
<td ><?php echo $row['bank']; ?></td >
</tr >
<?php endwhile ?>
</tbody>
</table>
<div class="form-group " align="center">
<a style="box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);border: 2px solid #4CAF50;background-color: white;color: black;width: 15%; float: right" href="#" target="_blank" type="button" id="button" name="button" class="btn btn-primary btn-lg btn-block login-button">Submit</a>
</div>
</div>
</div>
</div>
</body>
</html>
I agree with the other answers but before that you need to actually query.
your $sql="select Mid,Mname,Mnic,amount,month,bank from payments "; is just a string within the if block.
set
$sql="";
outside.
if (isset($_POST['button'])) {
$sql="select Mid,Mname,Mnic,amount,month,bank from payments ";
}
And call
$res = mysqli_query($conn,$sql);
and
while( $row = mysqli_fetch_assoc($res))
You closed the connection too early. The connection must be open while iterating over the items.
Remove this line :
conn->close();
if this do not works specify that error clearly.
and add this line after endwhile after
<?php endwhile;
mysqli_close();
?>
I'm sorry but i kind of find some difficulties while editing my data , the problem is when i click the button edit, it works but when you go back to the previous page, it's like the id isn't stocked anymore so the array stays unknowns of this specific id.
Here is it's code:
<?php
include_once 'dbconfig.php';
$id = $_GET['quotauser'];
if (isset($id)) {
extract($crud->getQUOTA($id));
}
include_once 'headerQ.php';
?>
<div class="clearfix"></div>
<div class="container">
<a href="add-quota.php?add-quota" class="btn btn-large btn-info">
<i class="glyphicon glyphicon-plus"></i> Add Quota
</a>
</div>
<div class="clearfix"></div><br />
<div class="container">
<table class='table table-bordered table-responsive'>
<tr>
<th>id</th>
<th>QTANAME</th>
<th>SRVNAME</th>
<th>QTAVALUE</th>
<th colspan="3" align="center">Actions</th>
</tr>
<?php
$query = "SELECT quota.* FROM quota,user WHERE user.id=quota.id_user AND quota.id_user= '.$id.' ";
$records_per_page = 10;
$newquery = $crud->paging($query, $records_per_page);
$crud->quotaID($newquery);
?>
<tr>
<td colspan="10" align="center">
<div class="pagination-wrap">
<?php $crud->paginglink($query, $records_per_page); ?>
</div>
</td>
</tr>
</table>
</div>
<?php include_once 'footer.php'; ?>
and when the button show quota is clicked, the page that contains quota contain two options edit and delete
and the edit button takes u to this page
Edit's code is :
<?php
include_once 'dbconfig.php';
if (isset($_POST['btn-update'])) {
$id = $_GET['edit_quota'];
$QTANAME = $_POST['QTANAME'];
$SRVNAME = $_POST['SRVNAME'];
$QTAVALUE = $_POST['QTAVALUE'];
if ($crud->updateQ($id, $QTANAME, $SRVNAME, $QTAVALUE)) {
$msg = "<div class='alert alert-info'>
<strong>WOW!</strong> Quota was updated successfully <a href='UserQuota.php'>HOME</a>!
</div>";
} else {
$msg = "<div class='alert alert-warning'>
<strong>SORRY!</strong> ERROR while updating Quota !
</div>";
}
}
if (isset($_GET['edit_quota'])) {
$id = $_GET['edit_quota'];
extract($crud->getQUOTA($id));
}
?>
<?php include_once 'headerQ.php'; ?>
<div class="clearfix"></div>
<div class="container">
<?php
if (isset($msg)) {
echo $msg;
}
?>
</div>
<div class="clearfix"></div><br />
<div class="container">
<form method='post'>
<table class='table table-bordered'>
<tr>
<td>QTANAME</td>
<td><input type='text' name='QTANAME' class='form-control' value="<?php echo $QTANAME; ?>" required></td>
</tr>
<tr>
<td>SRVNAME</td>
<td><input type='text' name='SRVNAME' class='form-control' value="<?php echo $SRVNAME; ?>" required></td>
</tr>
<tr>
<td>QTAVALUE</td>
<td><input type='number' name='QTAVALUE' class='form-control' value="<?php echo $QTAVALUE; ?>" required></td>
</tr>
<tr>
<td colspan="2">
<button type="submit" class="btn btn-primary" name="btn-update">
<span class="glyphicon glyphicon-edit"></span> Update this Record
</button>
<i class="glyphicon glyphicon-backward"></i> CANCEL
</td>
</tr>
</table>
</form>
</div>
<?php include_once 'footer.php'; ?>
<table class="table table-striped table-bordered bootstrap-datatable datatable">
<thead>
<tr>
<th></th>
<th>Business Name</th>
<th>Commencement Date</th>
<th>Registered?</th>
<th>Business Address</th>
<th>Contact Details</th>
<th>Contact Name</th>
<th> </th>
</tr>
</thead>
<tbody>
<?
db_connect();
$result=mysql_query("select * from hbs_participants_register ORDER BY hbs_id DESC");
$num_result=mysql_num_rows($result);
?>
<tr>
<?
for ($i=0; $i<$num_result; $i++) {
$row=mysql_fetch_array($result);
?>
<td width="10"><div align="left">
<?php echo $i+1; ?>
</div>
</td>
<td><div align="left">
<? echo $row['hbs_bus_name'] ?>
</div>
</td>
<td><div align="left">
<? echo $row['hbs_com_date'] ?>
</div>
</td>
<td><div align="left">
<? echo $row['hbs_bus_reg']; ?>
<br /> <strong><?php if (!empty($row['hbs_bus_regnumber'])) {
echo "Reg No. ".$row['hbs_bus_regnumber'];
} else {} ?></strong>
</div>
</td>
<td><div align="left">
<? echo $row['hbs_bus_address']; ?>
<br />
</div>
</td>
<td><div align="left">
<? echo $row['hbs_email']; ?>
<br />
<? echo $row['hbs_phone']; ?>
<br />
<? echo $row['hbs_url']; ?>
<br />
</div>
</td>
<td><div align="left">
<? echo $row['hbs_contact_name']; ?>
</div>
</td>
<td>
<div class="btn-group">
<button class="btn btn-small dropdown-toggle"
data-toggle="dropdown">
<i class="icon-cog"></i> <strong
style="font-size: 14px; padding: 5px 0px"> Manage</strong><span
class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a
href="../../print-busreg.php?hbs_id=<?php echo $row['hbs_id'];?>"
target="_blank"><i class="icon-list"></i> Full Details</a>
</li>
<li><a
href="?w=plugins/HBS%202015/delete-busreg&hbs_id=<?php echo $row['hbs_id'];?>&action=delete"
onclick="return confirm('Are you sure you want to delete?')"><i
class="icon-trash"></i> Delete</a>
</li>
</ul>
</div>
</td>
</tr>
<? } ?>
</tbody>
</table>
I believe this should work, assuming you are storing the data together with the year it was submitted. This is not the whole code you need, just the part which lets you select a year and get it from the Dbase.
<form method="get" action="page.php" enctype="multipart/form-data">
year:
<select name="selectedYear">
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
</select>
<input type="submit" name="submit">
</form>
PHP
<?php
$year = $_GET['selectedYear'];
?>
So then you just get all the columns that contain that year:
mysql_query('"select * from hbs_participants_register WHERE year LIKE" . $year . "ORDER BY hbs_id DESC"');
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>