I am very new to php and I am trying to show some specific records based on a input (from drop down).
From Customer.php page I am selecting a customer which will send a request to analysis.php which will perform DB operations and displays results in a tabular format. Now I am trying to draw a graph using the results from DB.
I donno where to start, any help would be much appreciated.
Below is my code.
customer.php
<html>
<head>
<title>Customer</title>
<script type="text/javascript">
function validateBox(){
var customer_name = document.getElementById('customer_name').value;
if(customer_name.length == 0){
alert("Please select customer name");
return false;
//handle validation response here
} else {
//document.getElementById("form").submit();//submit form, or whatever the button is supposed to do...
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","analysis.php?q="+customer_name,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form class="form" id="form" method="post">
<table align="center">
<tr>
<td>
<label>Customer Name :</label></td>
<td><select name="customer_name" id="customer_name" required>
<option value="">Select</option>
<option value="Dell">Dell</option>
<option value="HP">HP</option>
<option value="Lenovo">Lenovo</option>
<option value="Compaq">Compaq</option>
</select></td></tr>
<td><input onclick="validateBox()" type="button" name="addrecord" value="Begin Analysis"></td></table>
</form>
</body>
</html>
and analysis.php looks like
<?php
$connection = mysql_connect("localhost", "root", "");
$db = mysql_select_db("db", $connection);
$q = $_GET['q'];
$resource=array();
$z=0;
$SQL = mysql_query("select resources from users ", $connection);
while ($db_field = mysql_fetch_assoc($SQL)) {
$a = $db_field['resources'];
$resource[$z] = $a;
$z++;
}
$resourcelength = count($resource);
?>
<table border = "2" width = "30%" align='center'>
<tr align ="center">
<th >resource</th>
<th ># Count</th>
<?php
for($x = 0; $x < $resourcelength; $x++) {
print("<tr>");
print("<td align = 'center'>$resource[$x]</td>");
$y=$resource[$x];
$SQL = mysql_query("SELECT count(distinct(ticket_id)) as total FROM tickets WHERE resource_name='".$y."' ", $connection);
while ($db_field = mysql_fetch_assoc($SQL)) {
$a = $db_field['total'];
print("<td align = 'center'>$a</td>");
}
print("</tr>");
}
?>
</table>
and the output will be like
Resource Total
A 30
B 12
C 15
D 0
X 13
I want to convert this to a graph, I tried to include google charts, but no luck.
Thanks in advance. :)
I would suggest you to use pChart. It's a free PHP library, really easy to use and it contains a lot of basic examples.
Related
I'm doing a program that would ask user to search from Mysql table based on onKeyup. But it seems that the response time is very slow while I don't have that much of data ~ 2873 records.
Here is my PHP code :
<?php
session_start();
if(isset($_SESSION['master'])){
require_once('connect_db.php');
require_once('output_fns.php');
<div id="openModal6" class="modalDialog6">
<div>
X
<h2><center>Search Item</center></h2>
<p>
<table>
<tr><td>
<b>Item No. :</b><br />
//User input and onKeyup
<input type="text" value="" id="item_s" autofocus required onKeyUp="related_item(document.getElementById('item_s').value,document.getElementById('store_s').value);">
<td><b>Stor</b><br />
//User select Store by default usually its first store
<select name='store_s' id='store_s'>
<?php
$query = "SELECT * from stores_list";
$result = mysql_query($query);
if(mysql_num_rows($result)>0){
$num = mysql_num_rows($result);
for($i=0;$i<$num;$i++){
$row = mysql_fetch_assoc($result);
echo "<option value='{$row['short']}'>{$row['short']}</option>";
}
echo "</select>";
}
else{
//No Stores Available
echo "No Stores Found !";
}
?>
</td>
</tr>
<tr><td align="center" colspan="3">
//Response Output Here
<div id="txtHint4">Waiting...</div>
</td></tr>
</table>
</p>
</div>
</div>
<?php
}
Of course this is a Modal dialogue so it basically opens up when user Click on |Search Button| and using Onclick=
As for Ajax get_item.php
<?php
require_once('connect_db.php');
$q=$_GET['q'];
$s=$_GET['s'];
if($_GET['q']=="---"){
return true;
exit();
}
//Select Result From table where item Like
$query="SELECT * FROM store_items WHERE item_no LIKE '".$q."%' AND store = '".$s."' ORDER by sub Asc";
$result = mysql_query($query);
if(mysql_num_rows($result)>0){
echo "<br />";
echo "<table width=100% height=% align=center border=1 bgcolor=white cellpadding=5 cellspacing=0 style='font-size:12px;'>";
echo "<tr><td align=center><b>Item Number</b><td align=center><b>QTY</b><td align=center><b>Unit Cost</b></td><td align=center><b>Unit Sell</b></td><td align=center><b>Cat</b></td><td align=center><b>Sub</b></td><td align=center><b>Description</b></td><td align=center><b>Store</b></td></tr>";
while($row = mysql_fetch_assoc($result)){
echo "<tr><td align=center>{$row['item_no']}</td><td align=center>{$row['qty']}<td align=center>{$row['actual_price']}</td><td align=center>{$row['selling_price']}</b></td><td align=center>{$row['cat']}</td><td align=center>{$row['sub']}</td><td align=center>{$row['short']}</td><td align=center>{$row['store']}</td></tr>";
//End of While loop
}
}
else{
echo "<br /><center><font color=red>Sorry Item Number Not Found !</font></center>";
}
?>
And finally the Function related_item
function related_item(str,str2)
{
if (str=="")
{
document.getElementById("txtHint4").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint4").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","get_item.php?q="+str+"&s="+str2,true);
xmlhttp.send();
}
What have I done wrong this far? it should fetch data quickly from the table, I don't understand like around 4-5 seconds waiting time to see data coming up inside <div id='txtHint4'>Response</div>
Please Note the items are only numbers , example (001200201000)
UPDATE :
I've checked chrome developer tool network to test out the delay part and it seems the delay goes on get_item.php file Here are the results
Query 0.36
Stalled 0.5
Request Sent 0.14ms
Waiting (TTFB) 77.555ms
Content Download 3.40s
So does it mean that there is something wrong with my PHP file? what other or better way to enhance the code? Its straight forward reading from table. Maybe I'm missing something here
i am in dire need here. i have gone to every person i know that knows php/mysql/ajax, but no one can help.
i am trying to get an input field populated with data from my dbase that is chosen from two different selects. here is the situation:
building a golf scoring page on my website. the user will choose a course (select #1), then the tee they played (select #2), which then the disabled text inputs will populate with the rating and slope. the rating and slope are very important b/c they help figure out the handicap for the user. i am able to get everything to populate fine, but i can't figure out the correct WHERE clause in my query on the get_rating.php page. can somebody help me with that query?
here is my code:
dbase setup:
this is pulling from 2 tables, one is the courses table (course_id, c_id, name) and the other is the course_tees table (tee_id, course_name, c_id, t_id, color, rating, slope). the c_id's on both tables are the same.
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" class='new_score'>
<div class='form-group'>
<select class="form-control" name="course_name" onchange="get_tees(this.value)">
<option value="">select a tee</option>
<?php get_courses() ?>
</select>
</div>
<div class='form-group'>
<select class='form-control' name='tee_played' onchange="get_rating(this.value)" id='txtHint'>
<option value="">select a tee</option>
</select>
</div>
<div class="form-group" id="getRating">
</div>
the first select uses the get_tees.php code (listed below) and the second one uses the get_rating.php code (listed 2 below) which is the one i'm having trouble with.
get_tees.php
$con = mysqli_connect("***","***","***","***") or die("connection was not established");
$q = intval($_GET['q']);
mysqli_select_db($con,"course_tess");
$sql="SELECT * FROM course_tees WHERE c_id = '".$q."'";
$result = mysqli_query($con,$sql);
while($row=mysqli_fetch_array($result)) {
$tee_id = $row['tee_id'];
$c_id = $row['c_id'];
$t_id = $row['t_id'];
$tee_color = $row['color'];
$cor_rating = $row['rating'];
$cor_slope = $row['slope']; ?>
<option value='<?php echo $tee_id ?>'><?php echo $tee_color ?></option>
get_rating.php
$con = mysqli_connect("***","***","***","***") or die("connection was not established");
$q = intval($_GET['q']);
mysqli_select_db($con,"course_tees");
$sql="SELECT * FROM course_tees";
$result = mysqli_query($con,$sql);
while($row=mysqli_fetch_array($result)) {
$c_id = $row['c_id'];
$t_id = $row['t_id'];
$cor_rating = $row['rating'];
$cor_slope = $row['slope']; ?>
<input type='text' name='cor_rating' class='form-control' value='<?php echo $row['rating']; ?>' disabled>
<input type='text' name='cor_slope' class='form-control' value='<?php echo $row['slope']; ?>' disabled>
and here's my ajax for both selects:
function get_tees(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","get_tees.php?q="+str,true);
xmlhttp.send();
}
}
function get_rating(str) {
if (str == "") {
document.getElementById("getRating").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("getRating").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","get_rating.php?q="+str,true);
xmlhttp.send();
}
}
am i not passing something correctly in my ajax? what am i doing wrong?!?! PLEASE help!!
#chris85 this is essentially the bandaid that i have put over the issue for right now ... instead of trying to populate the text fields automatically, i am listing them in the selects and manually putting in the rating/slope ... this is my code:
php:
mysqli_select_db($con,"course_tess");
$sql="SELECT * FROM course_tees WHERE c_id = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<option value=''>select a tee</option>";
while($row=mysqli_fetch_array($result)) {
$tee_id = $row['tee_id'];
$c_id = $row['c_id'];
$t_id = $row['t_id'];
$tee_color = $row['color'];
$cor_rating = $row['rating'];
$cor_slope = $row['slope']; ?>
<option value='<?php echo $tee_color ?>'><?php echo $tee_color ?> - <?php echo $cor_rating ?> : <?php echo $cor_slope ?></option>
and the form:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" class='new_score'>
<div class='form-group'>
<select class="form-control" name="course_name" onchange="get_tees(this.value)">
<option value="">select a tee</option>
<?php get_courses() ?>
</select>
</div>
<div class='form-group'>
<select class='form-control' name='tee_played' onchange="get_rating(this.value)" id='txtHint'>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" name="cor_rating" placeholder="enter rating from above">
<input type="text" class="form-control" name="cor_slope" placeholder="enter slope from above">
</div>
so, like you can see, it's just a bandaid. if you want to see it working, go to the http://www.havikmarketing.com and sign in with these credentials:
EM: test#test.com
PW: testing123
then go up to the settings (gears) and choose 'new round'. go through and enter a score ... you'll see how it pulls everything through. now mind you, i just have it set up as pretty much a skeleton right now ... so no judging on the design :)
thanks again
i have one dropdown list which has data for "subjects" loaded from database. when i clicked on one subject what it should do is load related "subject_id" value inside textbox which is just below dropdown list option. i dont know how to bring value from getbook.php and show in book_ID input text.
show_bookid(str) {
var xmlhttp;
if (str.length == 0) {
document.getElementById("bookid").innerHTML = "";
return;
}
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
strong text
xmlhttp = new ActiveXOjbject("Microsoft.XMLHttpRequest");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("bookid").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "getbook.php?q=" + str, true);
xmlhttp.send();
}
getbook.php
<?php
<?php
$b = $_GET['q'];
include('includes/security.php');
include('includes/dbconnect.php');
$database = new MySQLDatabase();
$sql = "select * from tbl_bkcat where book_id='" . $b . "'";
$result = mysql_query($sql);
?>
?>
below is the File where i need to bring value
<form name="bookadd" action="" class="jNice" method="post">
<p>
<label>Subject</label>
<select name="subject" onChange="show_bookid(this.value);">
<?php while($sel_rows=mysql_fetch_array($subresult)) { ?>
<option value="<?php echo $sel_rows['book_id'];?>">
<?php echo $sel_rows[ 'subject']?>
</option>
<?php } ?>
</select>
</p>
<p>
<label>Book_Id</label>
<input type="text" id="bookid" class="text-small" />//where i need to load subject id</p>
The mistake was done at ajax part document.getElementById("bookid").innerHTML and must be replaced with document.getElementById().value since I had to put data to Html Element that cotain value i.e Textbox(as textbox contain value attribute).
InnerHTML is used to manipulate the html elements that does not contain value,
** div,h1, ** etc. for details see below link.
http://www.verious.com/qa/what-39-s-the-difference-between-document-get-element-by-id-quot-test-quot-value-and-document-get-element-by-id-quot-tes/
ajax code
function show_bookid(str)
{
var xmlhttp;
if(str.length==0)
{
document.getElementById("bookid").value="";
return;
}
if(window.XMLHttpRequest)
{
xmlhttp= new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXOjbject("Microsoft.XMLHttpRequest");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("bookid").value=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getbook.php?q="+str,true);
xmlhttp.send();
}
getbook.php
<?php
$b=$_GET['q'];
include('includes/security.php');
include('includes/dbconnect.php');
$database=new MySQLDatabase();
$sql="select * from tbl_bkcat where book_id='".$b."'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
echo $row['Book_id'];
?>
addbook.php
<form name="bookadd" action="" class="jNice" method="post">
<fieldset>
<p><label>Subject</label>
<select name="subject" onChange="show_bookid(this.value);">
<?php
while($sel_rows=mysql_fetch_array($subresult))
{
?>
<option value="<?php echo $sel_rows['book_id'];?>">
<?php echo $sel_rows['subject']?>
</option>
<?php
}
?>
</select>
</p>
<p>
<label >Book ID</label>
<input type="text" id="bookid" name="book"/>
</p>
Your subject_id is not displaying because you have not printed your book_id after fetching results from database in getbook.php
After this $result=mysql_query($sql);
Write echo $result['your_book_id_field_name'];
I have this code in my find.php file:
<select name="EmpName" required="required" id='empn'>
<option value="" style="display:none;"></option>
<?php
//get the employee id
$employeeId = $_POST['country'];
//make connection to database, bail if no connection
$connection = odbc_pconnect('db','','');
if (!$connection) { exit("Connection Failed: " . $connection); }
//retrieve usernames and passwords
$sql = "SELECT EName FROM LoginTable WHERE EmployeeID='$employeeId'";
$rs = odbc_exec($connection, $sql);
while(odbc_fetch_row($rs)) {
$rowJobNum = odbc_result($rs, 'EName');
printf("<option value='%s'>%s</option>", $rowJobNum, $rowJobNum);
}
?></select>
I have this code in my index.php file:
<script>
function getXMLHTTP() { //function to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getCity(strURL) {
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('citydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
</head>
<body>
<form method="post" action="" name="form1">
<table width="60%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="150">Country</td>
<td width="150"><select name="country" onChange="getCity('find.php?country='+this.value)">
<option value="">Select Country</option>
<option value="1">mg05</option>
<option value="2">Canada</option>
</select></td>
</tr>
<tr style="">
<td>City</td>
<td ><div id="citydiv"><select name="city">
</select></div></td>
</tr>
</table>
</form>
</body>
</html>
Here's the issue... I can't access the selection from the first drop down menu, the selection from the drop down menu is never going through to find.php. For reference, mg05 in the drop down list is an employee ID, I am working off an example here that was originally formatted for cities/states etc.
Here's my big issue, if I go to:
$sql = "SELECT EName FROM LoginTable WHERE EmployeeID = '$employeeId'";
and change it to
$sql = "SELECT EName FROM LoginTable WHERE EmployeeID= 'mg05'";
It works perfectly, and returns the correct result. So... obviously the post of data from index.php to find.php isn't working and I can't figure out why. I have $_GET and $_POST working in other areas of my site, but not this one.
Any insight as to where I may have gone wrong? I've tried using both get and post and nothing has worked.
The problem is that you are expecting the displayed text in the select to be post. This will not occur if the selected option has a value attribute, the value attribute will be used
<select name="country" onChange="getCity('find.php?country='+this.value)">
<option value="">Select Country</option>
<option value="mg05">mg05</option>
<option value="Canada">Canada</option>
</select>
I have my form like this:
Product dropdown
Quantity text field
UnitPrice display
TotalPrice display
I have created a function to display the unitprice by querying the db when the dropdown is selected. Now i wanted to be able to display the TotalPrice when the quantity is typed in by multiplying the quantity and the UnitPrice. Understand that the below is the way to multiply but how do i apply it in my query. Included my code too.
<script>
function showUP(str)
{
if (str=="")
{
document.getElementById("UP").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("UP").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getunitprice.php?q="+str,true);
xmlhttp.send();
}
function multiply(Quantity)
{
var totalPrice= parseFloat(document.getElementById("UP"))*Quantity;
document.getElementById("TP").innerHTML= totalPrice;
}
</script>
<table><tr>
<th width-"18%>Quantity:</th>
<td width="60%">
<input type-"text" name="Quantity" value="" onkeyup= "multiply (this.value)" size="60" />
</td>
</tr></table>
<p></p><div id="UP"><b>UnitPrice: 0.00 </b></div><p>
<p></p><div id="TP"><b>TotalPrice: 0.00 </b></div><p>
getunitprice.php
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', '', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$sql="SELECT CostPrice FROM Product WHERE ProductCode = '".$q."'";
$result2 = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
while($row2 = mysql_fetch_array($result2)) {
echo "<b>UnitPrice: ".$row2['CostPrice']."";
}
mysql_close($con);
?>
Use onBlur instead of onchange event. You can also use onkeyup event on quantity textbox.
<script>
function multiply(qty)
{
var totalPrice= parseFloat(document.getElementById("UP").innerHTML)*qty;
document.getElementById("TP").innerHTML= totalPrice;
}
</script>
<tr>
<th width-"18%>Quantity:</th>
<td width="60%">
<input type-"text" name="Quantity" value="" onkeyup= "multiply (this.value)" size="60" />
</td>
</tr>
<p></p><div><b>UnitPrice: <span id="UP">0.00</span> </b></div><p>
<p></p><div id="TP"><b>TotalPrice: 0.00 </b></div><p>