I am trying to display 2 totals calculated in a php function using a mouseover event with Ajax. How do I do this? -
this school project. have far. totally new , totally lost. appreciate help.
ajaxfunctions.js
function getxmlhttp() { var xmlhttp try { //firefox, opera 8.0+, safari xmlhttp = new xmlhttprequest(); } catch(e) { //internet explorer try { xmlhttp = new activexobject("msxml2.xmlhttp"); } catch(e) { try { xmlhttp = new activexobject("microsoft.xmlhttp"); } catch(e) { alert("your browser not support ajax!") return false; } } } return xmlhttp; } function makerequest(product) { var xmlhttp = getxmlhttp(); xmlhttp.onreadystatechange = function() { if(xmlhttp.readystate == 4){ if(xmlhttp.status == 200){ handleresponse(xmlhttp.responsetext); } } } xmlhttp.open("get", "prodtotal.php?product=+_product", true); xmlhttp.send(null); } function handleresponse(response) { document.getelementbyid('totalqty').innerhtml = response; document.getelementbyid('totaldol').innerhtml = response; }
this php class prodtotal function , mouseover events in table.
<?php class carsclass { private $user = 'php06'; private $pwd = 'php06'; private $dbconn; function __construct($db='classicmodels') { //create connection mysql database requested, if blank connect up. $this->dbconn = new mysqli('localhost', $this->user, $this->pwd, $db); if (mysqli_connect_errno()) { echo 'error: not connect database. please try again later.'; exit; } $query = "select count(*) 'custcount' customers"; $result = $this->dbconn->query($query); $row = $result->fetch_assoc(); $custcount = $row['custcount']; print "connected db $db user $this->user<br><br> number of rows $custcount<br><br>"; } function __destruct(){ mysqli_close(); print "db closed user <br><br>"; } function header(){ echo "midterm exam script 2 header<br><br>"; } function display(){ $totqty = 0; $totamt = 0; //get row wb_resident table $query = "select productcode,productname,productdescription,quantityinstock,buyprice,msrp products"; $result = $this->dbconn->query($query); ?> <table id="midterm2"> <tr> <th colspan="13">product database table</th> </tr> <tr> <th width="2%">product code</th> <th width="10%">product name</th> <th width="10%">product description</th> <th width="10%">quantity in stock</th> <th width="10%">buy price</th> <th width="2%">msrp</th> <th width="10%">total quantity</th> </tr> <tr> <th></th> <th></th> <th></th> <th></th> <th></th> <th></th> <th width="10%">total dollars</th> </tr> <?php while($row = $result->fetch_assoc()): $producta = $row["productcode"]; //list($totqty, $totamt) = $this->prodtotal($producta); ?> <tr> <td> <?php echo $row["productcode"]; ?> </td> <td> <?php echo $row["productname"];?> </td> <td> <?php echo $row["productdescription"]; ?> </td> <td> <?php echo $row["quantityinstock"]; ?> </td> <td> <?php echo $row["buyprice"]; ?> </td> <td> <?php echo $row["msrp"]; ?> </td> <td> <div id ="totalqty" onmouseover="makerequest($producta)"></div> <div id ="totaldol" onmouseover="makerequest($producta)"></div> </td> </tr> <?php endwhile; ?> </table> <?php } function footer(){ echo "midterm exam script 3 footer<br><br>"; } function prodtotal($product){ $query = "select rtrim(productcode) productt, quantityordered, priceeach orderdetails order productt"; $result = $this->dbconn->query($query); while($row = $result->fetch_assoc()){ if ($row["productt"] == $product){ $total = $row["quantityordered"] * $row["priceeach"]; $totqty = $totqty + $row["quantityordered"]; $totamt = $totamt + $total; } } return array($totqty, $totamt); } } ?>
this calls class.
<html> <head> <title>midterm2 script 4</title> <link rel="stylesheet" type="text/css" href="midterm2.css" /> <script src="ajax_functions.js" type="text/javascript"></script> </head> <body> <?php require 'carsclass4.php'; $obj1= new carsclass('classicmodels'); $obj1->header(); $obj1->display(); $obj1->footer(); ?> </body> </html>
right, have few minor errors, , missing file.
i going answer here on original question, try not post duplicates - if question not answered there reason. in case because long, answerers tend find problem is, rather having wade through code. other major issue mid-term, , people not answering homework questions defeats object of doing them slightly, unless phrase question in suggestion style. anyhow, it's first question here , not put off @ all, end of rant.
right, firstly in javascript file ajaxfunctions.js
, makerequest(product)
function has error in it. line:
xmlhttp.open("get", "prodtotal.php?product=+_product", true);
should be:
xmlhttp.open("get", "prodtotal.php?product=" + product, true);
you need pass variable php script, before passing " _product" product id, rather variable given.
then in main carsclass.php
file, have same error in different language, passing string rather actual variable in display()
method. find lines onmouseover
bindings.
<td> <div id ="totalqty" onmouseover="makerequest($producta)"></div> <div id ="totaldol" onmouseover="makerequest($producta)"></div> </td>
the $producta
here gets sent text "$producta", need variable , not in php mode, can not mention it, need echo out php has been done in previous table cells.
<td> <div id ="totalqty" onmouseover="makerequest(<?php echo $producta; ?>)"></div> <div id ="totaldol" onmouseover="makerequest(<?php echo $producta; ?>)"></div> </td>
the last thing need actual ajax request file prodtotal.php
(if name of last file put in question, different approach should taken).
prodtotal.php
<html> <head> <title>midterm2 ajax response file</title> </head> <body> <?php require 'carsclass4.php'; $product = (isset($_get['product']) ? trim($_get['product']) : ''); if (!empty($product)) { $classicmodels = new carsclass('classicmodels'); list ($totqty, $totamt) = $classicmodels->prodtotal($product); ?> <div id ="totalqty"><?php echo $totqty; ?></div> <div id ="totaldol"><?php echo $totamt; ?></div> <?php } ?> </body> </html>
the ajax request has go off , values, see how php file looks @ requested value, calls function calculate , outputs divs matching ids code in javascript handleresponse()
routine looking for, containing values. js routine takes out content .innerhtml
, replaces in elements.
note ajax routines take time, hover on not going instant. there may more errors in code elsewhere, hard part dealt with.
Comments
Post a Comment