没有从responseText获得字符串响应

时间:2014-03-18 20:55:03

标签: javascript php

我为网站设置了一个简单的程序,可以更新产品的价格,因为从下拉列表中选择了不同的产品和数量。我没有把我的字符串响应回到我的responseText中。这是我在3个不同文件中使用的代码示例 这是主页面代码:

    <script type="text/javascript" src="templates/js/price.js"></script>
        <script type="text/javascript" src="templates/js/ajax.js"> </script>
<?php

require_once(DATA.'dbOrders.php');
require_once(DATA.'dbCustomer.php');
require_once(DATA.'dbProducts.php');

$username = $_SESSION["loggedInUser"];
$email = getEmailByUsername($username);
$orders = getAllCustomerOrders($email);
if ($orders != null){
                $x = 1;?>
<table>

    <tr> 
      <td width="20px">Orders</td>
      <td width="85px">Date</td>
      <td width="10px">Qty</td>
      <td width="175px">Item</td>
      <td width="45px">Total</td>
      <td width="75px">Status</td>

    </tr>
    <?php foreach ($orders as $order){?>
    <tr> 
      <td width="20px"><?php print $x.".";?></td>
      <td width="85px"><?php print $order->getDate();?></td>
      <td width="10px"><?php print $order->getQty();?></td>
      <td width="175px"><?php print $order->getItem();?></td>
      <td width="45px">$<?php print $order->getTotal();?></td>
      <td width="75px"> - <?php print $order->getStatus(); $x++;}?></td>

    </tr>
</table><?php }


 $errors=array();
   if(!isset($_POST['qty'])){//Check if form has posted yet
        $_POST['qty'] = "100";$_FILES["file"]="";
        $errors['qty']="";$errors['img1']="";
        display_form();
        }

        if(isset($_POST['submit'])){ //collect the information from the form 
            validate_input();
            if($errors['qty']==""
                    && $errors['img1']==""){
                place_order();}

             else{display_form();}
        }

        function validate_input(){ //check input for valid data
            global $errors;

            if($_POST['qty'] == "" || !(is_numeric($_POST['qty']))){
                $errors['qty']="<font color='red'>
                        ***Enter valid quantity***</font>";            
            }
            else{$errors['qty']="";}

            if($_FILES["file"]["name"]==""){
                $errors["img1"]="<font color='red'>
                        ***Please select an image***</font>";
            }
            else{$errors["img"]="";}
        } 

        function place_order(){
            if($_POST['back']== 0){
            $back = "1";}
            else{$back=2;}
            $item = "PC".$back."-46-".$_POST['qty'];
            $image1 = $_FILES['file']["name"];
            $image2 = $_FILES['file2']["name"];
            move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"]);
            move_uploaded_file($_FILES["file2"]["tmp_name"],"images/" . $_FILES["file2"]["name"]);
            $username = $_SESSION["loggedInUser"];
            $email = getEmailByUsername($username);            
            $qty = $_POST['qty'];            
            $date = date("Y-m-d");        
            $customer= selectCustomerIDByUser($username);
            $total = getPrice($item);
            $status = "Processing";          
            $order = new Order($date,$customer,$email,$item,$qty,$total,$status,$image1,$image2);           
            addOrder($date,$customer,$email,$item,$qty,$total,$status,$image1,$image2);            
            $order->sendConfirmation(); ?>
            <h3>Order has been placed. Thank you.</h3>
<script type="text/javascript">setTimeout(function(){window.location.href='index.php';},2000);</script>
       <?php  }

function display_form(){

    global $errors;
     ?>

<div> 

        <form name="newOrderForm" method="post" enctype="multipart/form-data" >
            <table>
                <tr>&nbsp;</tr>
                <tr><td colspan="3"><h3>4x6 Postcards</h3></td></tr>
                <tr>&nbsp;</tr>
                <tr>
                    <td><font color="red">Front Side</td>
                    <td><font color="red">Back Side</td>
                </tr>
                <tr>
                    <td>Full Printing</td>
                    <td><select name="back" id="back" onChange="OnSelectedIndexChange();">
                    <option value="2">Full Color</option>
                    <option value="0">No Printing</option>
                    <option value="1">Black or Gray scale</option>
                    </select></td>
                </tr>
                <tr>
                    <td>Quantity</td>
                    <td><select name="qty" id="qty" onChange="OnSelectedIndexChange();">
                    <option value="<?php echo $_POST['qty']; ?>"><?php echo $_POST['qty']; ?></option>      

                    <option value="500">500</option><option value="1000">1000</option>
                    <option value="2000">2000</option><option value="2500">2500</option>
                    <option value="3000">3000</option><option value="4000">4000</option>
                    <option value="5000">5000</option><option value="6000">6000</option>
                    </select></td>
                    <td><?php echo $errors['qty']; ?></td>

                </tr>

                <tr>
                    <td>Price:<textarea id="price"></textarea></td>
                </tr>

                <tr><td>
                    <label for="file">Font image:</label>                
                <input type="file" name="file" id="file"/> </td> 
                    <td><?php echo $errors['img1']; ?></td>
                </tr>
                <tr><td>
                    <label for="file2">Back image:</label>                
                <input type="file" name="file2" id="file2"/> </td> 
                    <td><?php echo $errors['img2']; ?></td>
                </tr>
                <tr>
                    <td colspan="1"><input name="submit" type="submit" value="Submit Order" /></td>                    
                    <td colspan="1"><input name="reset" type="reset" value="Reset" /></td>
                    <td></td>
                </tr>
            </table>
        </form> 
        <?php } ?>
</div> 

当主页上发生onChange事件时,此代码启动进程: price.js:

    function OnSelectedIndexChange()
{
    var qty = document.getElementById('qty').value;
    var back = document.getElementById('back').value;

    var url = "includes/getPrice.php?qty=" + escape(qty) +
                             "&back=" + escape(back);                  

    request.open("GET", url, true);
    request.onreadystatechange = displayPrice; 
    request.send(null);

}

function displayPrice() {

    if (request.readyState === 4) {
    if (request.status === 200) {

      var price = request.responseText;

      /* Update the HTML web form */
      document.getElementById("price").value = price;

    }    
  }  
}

这是我的ajax代码 ajax.js

    var request = null;
try {
  request = new XMLHttpRequest();
} catch (trymicrosoft) {
  try {
    request = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
    try {
      request = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {
      request = null;
    }
  }
}

if (request === null)
  alert("Error creating request object!");

然后是这个php文件 getPrice.php

<?php
require(DATA.'dbProducts.php');

$qty = $_REQUEST['qty'];
$back = $_REQUEST['back'];


echo "hi";

?>

当在其中一个下拉列表中进行更改时,&#34; hi&#34;应出现在&#39;价格&#39;文本框(现在编码),但没有显示。

1 个答案:

答案 0 :(得分:-1)

request1是一个全局变量吗?很难通过这些功能来完成它,你分享了所有的js吗?

编辑:

你可以试试以下:

function loadPrice(url)
{
    var xmlhttp;
    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)
            {
                alert(xmlhttp.responseText);
            }
        }
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
}

function OnSelectedIndexChange()
{
    loadPrice('includes/getPrice.php')
}

你现在从你的php文件得到任何回复吗?

编辑2

请查看本教程,看看您的ajax请求中是否缺少某些内容http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_suggest