2014-03-18 1 views
0

다른 제품 및 수량이 드롭 다운 목록에서 선택되면 제품 가격을 업데이트하는 웹 사이트에 간단한 프로그램을 설정하고 있습니다. 내 문자열 응답을 내 responseText로 되돌릴 수 없습니다. price.js을 : 다음 OnChange 이벤트가 메인 페이지에 일어날 때responseText에서 문자열 응답을받지 못합니다.

<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> 

이 코드는 프로세스를 재 기동 : 저는 여기에 3 개 개의 파일을 여기에 에서 사용하고있는 코드의 샘플은 메인 페이지 코드입니다

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.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"; 

?> 

는 "안녕하세요"는 '가격'텍스트 상자에 나타납니다,하지만 아무것도까지 보여줍니다.

+0

에없는 뭔가가 있는지하세요? 일반적으로 이러한 오류는 백엔드 오류 때문에 발생합니다. 디버거와 함께 반환되는 값을 확인 했습니까? –

+0

위 코드를 추가했습니다. – user3434999

답변

-1

은 요청 1 전역 변수입니까? 함수를 통해 그것을 따라 가기가 어렵습니다. 모든 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

당신이 우리에게 백엔드 코드를 보여 주시겠습니까이 튜토리얼을 확인하고 아약스 요청 http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_suggest

+0

내 모든 js 코드가 있습니다. db 액세스 권한이있는 PHP 파일에서만 코드를 가져 왔습니다. – user3434999

+1

이 질문에 대한 답을 제공하지 않습니다. 비평하거나 저자의 설명을 요청하려면 게시물 아래에 의견을 남기십시오. 자신의 게시물에 언제나 댓글을 달 수 있으며 충분한 [평판] (http://stackoverflow.com/help/whats-reputation)을 갖게되면 [모든 게시물에 댓글을 달 수] 있어야합니다 (http://stackoverflow.com/help/privileges/comment). – benka

+0

나는 price.js 파일과 ajax.js 파일에 몇 가지 코드 변경을했다. 위의 업데이트 된 코드는 – user3434999

관련 문제