我想显示2总计使用鼠标悬停事件与阿贾克斯计算一个PHP函数。我该怎么做呢?我想、鼠标、怎么做、我该

由网友(时间会让你看清每一张脸)分享简介:这是一所学校的项目。这是我有这么远。我完全新的这个和完全丧失。请问AP preciate一些帮助。 Ajaxfunctions.js 函数getXMLHttp(){VAR XMLHTTP尝试{// Firefox,歌剧8.0+,Safari浏览器XMLHTTP =新XMLHtt prequest();}赶上(E){/...

这是一所学校的项目。这是我有这么远。我完全新的这个和完全丧失。请问AP preciate一些帮助。

Ajaxfunctions.js

 函数getXMLHttp()
{
  VAR XMLHTTP

  尝试
  {
    // Firefox,歌剧8.0+,Safari浏览器
    XMLHTTP =新XMLHtt prequest();
  }
  赶上(E)
  {
    //IE浏览器
    尝试
    {
      XMLHTTP =新的ActiveXObject(MSXML2.XMLHTTP);
    }
    赶上(E)
    {
        尝试
            {
                XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
            }
        赶上(E)
            {
                警报(您的浏览器不支持AJAX!)
                返回false;
            }
        }
    }
    返回XMLHTTP;
}


 功能MakeRequest(产品)
{
  VAR XMLHTTP = getXMLHttp();

  xmlHttp.onreadystatechange =功能()
  {
    如果(xmlHttp.readyState == 4){
        如果(xmlHttp.status == 200){
      用handleResponse(xmlHttp.responseText);
    }
    }
  }

  xmlHttp.open(GET,prodTotal.php产品= + _产品?,真正的);
  xmlHttp.send(空);
}

功能用handleResponse(响应)
{
  的document.getElementById('totalqty)的innerHTML =响应。
  的document.getElementById('totaldol)的innerHTML =响应。
}
 

这是PHP类与PRODTOTAL功能表中的鼠标悬停事件。

 < PHP

类CarsClass {
        私人$ USER ='php06';
        私人$ PWD ='php06';
        私人$ dbConn;

功能__construct($ DB ='classicmodels'){
        //创建连接请求,如果空白只需连接了MySQL数据库。

        $这个 - > dbConn =新的mysqli(本地主机,$这个 - >用户,$这个 - > PWD,$分贝);
        如果(mysqli_connect_errno()){
            回声错误:无法连接到数据库。请稍后再试。';
            出口;
         }
        $查询=SELECT COUNT(*)从客户custcount';

        $结果= $这个 - > dbConn->查询($查询);
        $行= $ result-> FETCH_ASSOC();
        $ custCount = $行['custcount'];

        打印连接到数据库$分贝为用户$这个 - >用户< BR>< BR>将行数$ custCount< BR>< BR>中;
    }

功能__destruct(){
        mysqli_close();
        打印DB关闭了用户LT; BR>< BR>中;
    }

函数头(){
    回声期中考试剧本2部< BR>< BR>中;
    }

功能显示(){
    $ totqty = 0;
    $ totamt = 0;
    //从WB_Resident表中获取行
    $查询=选择产品code,产品名称,产品描述,quantityInStock,buyPrice,从产品的建议零售价;
    $结果= $这个 - > dbConn->查询($查询);
?>

<表ID =midterm2>

    &其中; TR>
       百分位合并单元格=13>产品数据库表< /第i个
    < / TR>
    &其中; TR>
       百分位宽度=2%>产品code< /第i个
       百分位宽度=10%>产品名称< /第i个
       百分位宽度=10%>产品描述< /第i个
       百分位宽度=10%>在股票和LT量; /第i个
       百分位宽度=10%>购买价格与LT; /第i个
       百分位宽度=2%>建议零售价< /第i个
       百分位宽度=10%个总数量与所述; /第i个
    < / TR>
    &其中; TR>
    百分位>< /第i个
    百分位>< /第i个
    百分位>< /第i个
    百分位>< /第i个
    百分位>< /第i个
    百分位>< /第i个
    百分位宽度=10%>美元总额和LT; /第i个

    < / TR>

    < PHP
     而($行= $ result-> FETCH_ASSOC()):
     $产品A = $行[产品code];
     //名单($ totqty,$ totamt)= $这个 - > ProdTotal($产品A);

    ?>
    &其中; TR>
      < TD>
         <?PHP的回声$行[产品code]; ?>
         &功放; NBSP;
      < / TD>
      < TD>
        <?PHP的回声$行[产品名称];?>
        &功放; NBSP;
        < / TD>
      < TD>
         <?PHP的回声$行[产品描述​​]; ?>
         &功放; NBSP;
      < / TD>
      < TD>
         <?PHP的回声$行[quantityInStock]; ?>
         &功放; NBSP;
      < / TD>
      < TD>
         <?PHP的回声$行[buyPrice]; ?>
         &功放; NBSP;
      < / TD>
      < TD>
         <?PHP的回声$行[建议零售价]; ?>
         &功放; NBSP;
      < / TD>
      < TD>
      < D​​IV ID =totalqty的onmouseover =MakeRequest($产品A)>< / DIV>
      &功放; NBSP;
      < D​​IV ID =totaldol的onmouseover =MakeRequest($产品A)>< / DIV>
      &功放; NBSP;
      < / TD>
      < / TR>

      < PHP
      ENDWHILE;
      ?>
      < /表>
    < PHP
  }

功能页脚(){
    回声期中考试脚本3尾< BR>< BR>中;
    }

功能ProdTotal($产品){

     $查询=从订单明细以便通过productt选择RTRIM(产品code)根据productt,quantityOrdered,查询个;

        $结果= $这个 - > dbConn->查询($查询);

        而($行= $ result-> FETCH_ASSOC()){
        如果($行[productt] == $产品){
        $总额= $行[quantityOrdered] * $行[查询个]。
        $ totqty = $ totqty + $行[quantityOrdered];
        $ totamt = $ totamt + $总量;
        }
        }
        返回数组($ totqty,$ totamt);
        }


        }
    ?>
 
Matplotlib 中文用户指南 8.2 我们最喜欢的秘籍

这将调用这个类。

 < HTML>
< HEAD>
<冠军> Midterm2脚本4℃/标题>
<链接相对=样式类型=文/ CSS的href =midterm2.css/>
<脚本SRC =ajax_functions.js类型=文/ JavaScript的>< / SCRIPT>
< /头>

<身体GT;

< PHP
需要'CarsClass4.php;

$ OBJ1 =新CarsClass('classicmodels');

$ obj1->标题();
$ obj1->显示器();
$ obj1->尾();

?>

< /身体GT;
< / HTML>
 

解决方案

对了,你有一些小错误和丢失的文件。

我要在这里回答你在你原来的问题,尽量不要重复发帖 - 如果你的问题没有得到回答有可能是一个原因。在这种情况下,因为它是很长的,回答者往往喜欢你找到问题所在,而不必涉水通过code。另一个主要问题是,这显然是一个中期,大多数人不喜欢回答的家庭作业的问题,因为它违背了你做这些对象咯,除非你短语建议风格的问题。总之,这是你的第一个问题在这里,我不把你关好,所以咆哮结束。

右键,首先在你的JavaScript文件 Ajaxfunctions.js ,您的 MakeRequest(产品)函数有一个错误它。该行:

  xmlHttp.open(GET,?prodTotal.php产品= + _产品,真正的);
 

应该是:

  xmlHttp.open(GET,prodTotal.php产品=?+产品,真正的);
 

您需要的通过的变回PHP脚本,然后再进行传递_product作为产品ID,而不是变量给出。

然后在你的主 CarsClass.php 文件,你在一个不同的语言同样的错误,传递一个字符串,而不是实际的变量在显示()方法。查找与的onmouseover 绑定行。

 < TD>
    < D​​IV ID =totalqty的onmouseover =MakeRequest($产品A)>< / DIV>
    &功放; NBSP;
    < D​​IV ID =totaldol的onmouseover =MakeRequest($产品A)>< / DIV>
    &功放; NBSP;
< / TD>
 

$产品A 在这里被发送的文本$产品A,你需要的变量,你是不是在PHP模式,所以你不能只是简单地提到它,你需要回声它从PHP作为已在previous表格单元已经完成。

 < TD>
    < D​​IV ID =totalqty的onmouseover =MakeRequest(小于?PHP的echo $产品A;?>)>< / DIV>
    &功放; NBSP;
    < D​​IV ID =totaldol的onmouseover =MakeRequest(小于?PHP的echo $产品A;?>)>< / DIV>
< / TD>
 

您需要的最后一件事是实际的AJAX请求文件 prodTotal.php (如果这是你把你的问题的最后一个文件,名称则略有不同方法应采取)。

prodTotal.php

 < HTML>
< HEAD>
    <冠军> Midterm2 Ajax响应文件< /标题>
< /头>
<身体GT;

< PHP
需要'CarsClass4.php;

$产品=(使用isset($ _ GET ['产品'])修剪($ _ GET ['产品']):'');

如果(!空($产物)){
    $ classicModels =新CarsClass('classicmodels');

    列表($ totqty,$ totamt)= $ classicModels-> ProdTotal($产品);
?>
    < D​​IV ID =totalqty>< PHP的echo $ totqty; ?>< / DIV>
    < D​​IV ID =totaldol>< PHP的echo $ totamt; ?>< / DIV>
< PHP
}
?>

< /身体GT;
< / HTML>
 

AJAX请求已熄灭,得到的值,看看PHP文件着眼于请求的值,调用函数来计算的话,输出的DIV匹配的ID的code在Javascript的用handleResponse()程序正在寻找,包含值。这JS常规取出含量 .innerHTML 并替换它的元素。

需要注意的是AJAX程序需要时间,所以悬停不会被瞬间。有可能是在$ C $别处C的具体错误,但是这是最困难的部分处理。

This is for a school project. This is what I have so Far. I am totally new to this and totally lost. Would appreciate some 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 does 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 is the PHP Class with the PRODTOTAL function and the mouseover events in the table.

<?php 

Class CarsClass {
        private $user = 'php06'; 
        private $pwd = 'php06';  
        private $dbConn;

function __construct($db='classicmodels') {
        //Create connection to MySQL database requested, if blank just connect up.

        $this->dbConn = new mysqli('localhost', $this->user, $this->pwd, $db);
        if (mysqli_connect_errno()) {
            echo 'Error: Could not connect to database.  Please try again later.';
            exit;
         }
        $query = "select count(*) as 'custcount' from customers";

        $result = $this->dbConn->query($query);
        $row = $result->fetch_assoc();
        $custCount = $row['custcount'];

        print "Connected to DB $db as user $this->user<br><br> Number of Rows $custCount<br><br>";
    }

function __destruct(){
        mysqli_close();
        Print "DB closed by user <br><br>";
    }

function header(){
    echo "Midterm Exam Script 2 Header<br><br>";
    }

function display(){
    $totqty = 0;
    $totamt = 0;
    //get row from WB_Resident Table
    $query = "select productCode,productName,productDescription,quantityInStock,buyPrice,MSRP from 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"]; ?>
         &nbsp;
      </td>
      <td>
        <?php echo $row["productName"];?>
        &nbsp;
        </td>
      <td>
         <?php echo $row["productDescription"]; ?>
         &nbsp;
      </td>
      <td>
         <?php echo $row["quantityInStock"]; ?>
         &nbsp;
      </td>
      <td>
         <?php echo $row["buyPrice"]; ?>
         &nbsp;
      </td>
      <td>
         <?php echo $row["MSRP"]; ?>
         &nbsp;
      </td>
      <td>
      <div id ="totalqty" onmouseover="MakeRequest($producta)"></div>
      &nbsp; 
      <div id ="totaldol" onmouseover="MakeRequest($producta)"></div>  
      &nbsp;  
      </td>       
      </tr>

      <?php 
      endwhile;
      ?>
      </table>
    <?php   
  }

function footer(){
    echo "Midterm Exam Script 3 Footer<br><br>";
    }

function ProdTotal($product){

     $query = "select RTRIM(productCode) as productt, quantityOrdered, priceEach from orderdetails order by 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 the 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, you have a few minor errors, and a missing file.

I am going to answer you here on your original question, try not to post duplicates - if your question does not get answered there is probably a reason. In this case because it was very long, answerers tend to like you to find where the problem is, rather than having to wade through code. The other major issue is that this is obviously a mid-term, and most people do not like answering homework questions as it defeats the object of you doing them slightly, unless you phrase the question in a suggestion style. Anyhow, it's your first question here and I do not to put you off at all, so end of rant.

Right, firstly in your Javascript file Ajaxfunctions.js, your MakeRequest(product) function has an error in it. This line:

xmlHttp.open("GET", "prodTotal.php?product=+_product", true);

Should be:

xmlHttp.open("GET", "prodTotal.php?product=" + product, true);

You need to pass the variable back to the PHP script, before you were passing " _product" as the product ID, rather than the variable given.

Then in your main CarsClass.php file, you have the same error in a different language, passing a string rather than the actual variable in the display() method. Find the lines with the onmouseover bindings.

<td>
    <div id ="totalqty" onmouseover="MakeRequest($producta)"></div>
    &nbsp; 
    <div id ="totaldol" onmouseover="MakeRequest($producta)"></div>  
    &nbsp;  
</td>

The $producta here gets sent as the text "$producta", you need the variable and you are not in PHP mode, so you can not just simply mention it, you need to echo it out from PHP as has been done in previous table cells.

<td>
    <div id ="totalqty" onmouseover="MakeRequest(<?php echo $producta; ?>)"></div>
    &nbsp; 
    <div id ="totaldol" onmouseover="MakeRequest(<?php echo $producta; ?>)"></div>  
</td>

The last thing you need is the actual AJAX request file prodTotal.php (if this is the name of the last file you put in your question, then a slightly different approach should be 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 to go off and get the values, see how the PHP file looks at the requested value, calls the function to calculate it and outputs DIVs matching the IDs your code in the Javascript HandleResponse() routine is looking for, containing the values. That JS routine takes out the content .innerHTML and replaces it in to the elements.

Note that AJAX routines take time, so the hover over is not going to be instant. There may be more errors in your code elsewhere, but this is the hard part dealt with.

阅读全文

相关推荐

最新文章