2016-03-28 8 views
0

プロジェクトでPrototype JSフレームワークを実装するにはどうすればよいですか。私はこのシンプルなフォームを持っています。それはユーザーの郵便番号入力によって都市と州を置き換えます。 Ajaxプロトタイプ呼び出しでこの作業を行う必要があり、結果が得られません。これは私のjsとPHPとHTMLコードです:プロトタイプAjaxリクエストのJSフレームワーク

<!DOCTYPE html> 
 
<!-- popcornA.html 
 
    This describes popcorn sales form page which uses 
 
    Ajax and the zip code to fill in the city and state 
 
    of the customer's address 
 
    --> 
 
<html lang = "en"> 
 
    <head> <title> Popcorn Sales Form (Ajax) </title> 
 
    <style type = "text/css"> 
 
     img {position: absolute; left: 400px; top: 50px;} 
 
    </style> 
 
     
 
    <script type = "text/JavaScript" src = "prototype.js" src = "popcornA.js"></script> 
 
    <script type = "text/JavaScript" src = "popcornA.js"></script> 
 
     
 
    <meta charset = "utf-8" /> 
 
    </head> 
 
    <body> 
 
    <h2> Welcome to Millenium Gynmastics Booster Club Popcorn 
 
     Sales 
 
    </h2> 
 

 
    <form action = ""> 
 

 
<!-- A borderless table of text widgets for name and address --> 
 

 
     <table> 
 
     <tr> 
 
      <td> Buyer's Name: </td> 
 
      <td> <input type = "text" name = "name" 
 
         size = "30" /> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td> Street Address: </td> 
 
      <td> <input type = "text" name = "street" 
 
         size = "30" /> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td> Zip code: </td> 
 
      <td> <input type = "text" name = "zip" 
 
         size = "10" 
 
         onblur = "getPlace(this.value)" /> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td> City </td> 
 
      <td> <input type = "text" name = "city" 
 
         id = "city" size = "30" /> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td> State </td> 
 
      <td> <input type = "text" name = "state" 
 
         id = "state" size = "30" /> 
 
      </td> 
 
     </tr> 
 
     </table> 
 
    
 
     <img src = "popcorn.png" 
 
      alt = "picture of popcorn" 
 
      width = "150" height = "150" /> 
 
     <p /> 
 
     
 
<!-- The submit and reset buttons --> 
 

 
     <p> 
 
     <input type = "submit" value = "Submit Order" /> 
 
     <input type = "reset" value = "Clear Order Form" /> 
 
     </p> 
 
    </form> 
 
    </body> 
 
</html>

new Ajax.Request("getCityState.php", { 
 
    method: "get", 
 
    parameters: "zip=" + zip, 
 
    onSuccess: function(request) { 
 
     var place = request.responseText.split(', '); 
 
     $("city").value = place[0]; 
 
     $("state").value = place[1]; 
 
    }, 
 
    onFailure: function(request) { 
 
     alert("Error - request failed"); 
 
    } 
 
});

<?php 
 
// getCityState.php 
 
// Gets the form value from the "zip" widget, looks up the 
 
// city and state for that zip code, and prints it for the 
 
// form 
 
     
 
    $cityState = array("81611" => "Aspen, Colorado", 
 
        "81411" => "Bedrock, Colorado", 
 
        "80908" => "Black Forest, Colorado", 
 
        "80301" => "Boulder, Colorado", 
 
        "81127" => "Chimney Rock, Colorado", 
 
        "80901" => "Colorado Springs, Colorado", 
 
        "81223" => "Cotopaxi, Colorado", 
 
        "80201" => "Denver, Colorado",      
 
        "81657" => "Vail, Colorado", 
 
        "80435" => "Keystone, Colorado", 
 
        "80536" => "Virginia Dale, Colorado" 
 
        ); 
 
    $zip = $_GET["zip"]; 
 
    if (array_key_exists($zip, $cityState)) 
 
    print $cityState[$zip]; 
 
    else 
 
    print " , "; 
 
?>

+0

「Uncaught ReferenceE rror:zipが定義されていません "error on line popcornA.js:37 – SQATube

答えて

0

まず、parametersはこのようなオブジェクトである必要があります。

parameters : {'zip': zip } 

また、パラメータをURL文字列にショートカットして配置することもできますが、実行しようとしているその他の問題に集中できます。

おそらくonblur属性の代わりにオブザーバーを使用する必要があります。これは、ブラウザに扱えるメモリがあるので、多くのイベントハンドラを要素に追加することができます。また、HTMLからjavascriptを分離し、オブザーバーを一緒に編成することもできます。

$()メソッドではidsではなく名前を使用するため、これらの属性を追加する必要があります。このため、zipが定義されていないという別のエラーが発生しています。

ので

...snip... 
<tr> 
     <td> Zip code: </td> 
     <td> <input type="text" name="zip" id="zip" size="10" /> 
     </td> 
</tr> 
<tr> 
     <td> City </td> 
     <td> <input type = "text" name = "city" id="city" 
        id = "city" size = "30" /> 
     </td> 
</tr> 
<tr> 
     <td> State </td> 
     <td> <input type = "text" name = "state" id="state" 
        id = "state" size = "30" /> 
     </td> 
</tr> 
...snip... 

と私はそのあなたがそれらを追加する必要がある場合に複数のプロパティを扱いやすい、と奇妙な分割エラーを起こしにくい

function getPlace(){ 

    //getPlace() is an event handler so `this` points to the element being observed 

    new Ajax.Request("getCityState.php?zip="+this.getValue(), 
     onSuccess: function(request) { 
      var place = request.responseJSON; 
      $("city").value = place.city; 
      $("state").value = place.state; 
     }, 
     onFailure: function(request) { 
      alert("Error - request failed"); 
     } 
} 

document.observe('dom:loaded',function(){ 
    $('zip').observe('blur',getPlace); 
}); 

として代わりにカンマ分割を行うのでJSONを使用しますJSON出力にはバックエンドスクリプトを少し調整する必要があります

<?php 
// getCityState.php 
// Gets the form value from the "zip" widget, looks up the 
// city and state for that zip code, and prints it for the 
// form 

$cityState = array("81611" => array("city" => "Aspen" ,"state" => "Colorado"), 
       "81411" => array("city" => "Bedrock" ,"state" => ", Colorado"), 
       "80908" => array("city" => "Black Forest","state" => "Colorado"), 
       "80301" => array("city" => "Boulder", "state" => "Colorado"), 
       "81127" => array("city" => "Chimney Rock", "state" => "Colorado"), 
       "80901" => array("city" => "Colorado Springs", "state" => "Colorado"), 
       "81223" => array("city" => "Cotopaxi", "state" => "Colorado"), 
       "80201" => array("city" => "Denver", "state" => "Colorado"),      
       "81657" => array("city" => "Vail", "state" => "Colorado"), 
       "80435" => array("city" => "Keystone", "state" => "Colorado"), 
       "80536" => array("city" => "Virginia Dale", "state" => "Colorado") 
       ); 
header("Content-type: application/json"); 
//isset() is a faster test than array_key_exists() 
if(isset($cityState[$_GET['zip']]) 
{ 
    print json_encode($cityState[$_GET['zip']]); 
} 
else 
{ 
    print "false"; 
} 
関連する問題