2013-05-11 90 views
2

所以我做簡單的編輯/刪除/新的MySQL數據庫更新。我使用AJAX,因爲我需要它在一個單一的頁面。 因此,當我檢查新按鈕,一個新的窗體顯示(從ajax調用到PHP文件)。我得到的HTML表單。我試圖驗證這一點。 但我不能。代碼似乎是對的。我在這裏的代碼簡單的AJAX調用不工作

trail.php

<?php include('connect.php'); ?> 
<html> 

<head> 
    <script src="http://code.jquery.com/jquery-latest.js"></script> 
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script> 
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script> 
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.js"></script> 
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.min.js"></script> 
    <script> 
    $(document).ready(function() { 
    $("#form1").validate({ 
     debug: false, 
     rules: { 

     plid: "required", 
     }, 
     messages: { 

     plid: "Please select a pack list id..", 
     }, 
     submitHandler: function(form) { 

     $.ajax({ 
      type: "POST", 
      url: "aa.php", 
      data: $('#form1').serialize(), 
      cache: false, 

      success: function(response) { 
      $('#result1').html(response); 

      } 
     }); 
     } 
    }); 

    }); 
    </script> 
</head> 

<body> 
    <div id="result1"></div>Packing List</br> 
    <form id="form1" name="form1" action="" method="post"> 
     <?php echo '<select name="plid" id="plid">'; echo '<option value="" selected="selected">--Select the Pack List Id--</option>'; $tempholder=a rray(); $sql="SELECT `pl_id` 
     FROM (
      SELECT `pl_id` 
      FROM packlist 
      ORDER BY `pl_id` DESC 
      LIMIT 30 
     ) AS t" ; $query=m ysql_query($sql) or die(mysql_error()); $nr=m ysql_num_rows($query); for ($i=0; $i<$nr; $i++){ $r=m ysql_fetch_array($query); if (!in_array($r[ 'pl_id'], $tempholder)){ $tempholder[$i]=$ r[ 'pl_id']; echo "<option>".$r[ "pl_id"]. "</option>"; } } echo '</select>'; ?> 
     <br/> 
     <input type="submit" name="new" id="new" value="New" /> 
     <br/> 
     <input type="submit" name="delete" value="Delete" /> 
     <br/> 
     <input type="submit" name="edit" id="edit" value="Edit" /> 
     <br/> 
    </form> 
</body> 

我的AJAX稱爲PHP文件

<head> 
    <script src="http://code.jquery.com/jquery-latest.js"></script> 
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script> 
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script> 
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.js"></script> 
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.min.js"></script> 
    <script> 
     $(document).ready(function() { 
      $("#form2").validate({ 
       debug: false, 
       rules: { 

        plidnew: "required", 

       }, 
       messages: { 

        plidnew: "Please select a pack list id..", 
       } 
      }); 
     }); 
    </script> 
</head> 

<body> 
    <?php $a=isset($_POST[ 'plid']) && $_POST[ 'plid']; $b=isset($_POST[ 'new']) && $_POST[ 'new']; if($a&&$b) { ?> 
    <form name="form2" id="form2" method="post" action=""> 
     <P> 
      <LABEL for="plidnew">PackList No 
       <INPUT type="text" id="plidnew"> 
      </LABEL> 
      <BR> 
      <BR> 
      <LABEL for="itemidnew">Item Id 
       <INPUT type="text" id="itemidnew"> 
      </LABEL> 
      <BR> 
      <BR> 
      <LABEL for="quannew">Quantity 
       <INPUT type="text" id="quannew"> 
      </LABEL> 
      <BR> 
      <BR> 
      <LABEL for="potnew">Potency 
       <INPUT type="text" id="potnew"> 
      </LABEL> 
      <BR> 
      <BR> 
      <LABEL for="sizenew">Size 
       <INPUT type="text" id="sizenew"> 
      </LABEL> 
      <BR> 
      <BR> 
      <INPUT type="submit" id="newsubmit" name="newsubmit" value="Submit"> 
      <INPUT type="reset"> 
     </P> 
    </form> 
    <?php } 
$c=isset($_POST[ 'plid']) && $_POST[ 'plid']; 
$d=isset($_POST[ 'delete']) && $_POST[ 'delete']; 
if($c&&$d) { 
    echo "delete!!"; 
} 
$e=isset($_POST[ 'plid']) && $_POST[ 'plid']; 
$f=isset($_POST[ 'edit']) && $_POST[ 'edit']; 
if($e&&$f) { 
?> 
    <form name="form3" id="form3" method="post" action="aa.php"> 
     <P> 
      <LABEL for="plidedit">PackList No 
       <INPUT type="text" id="plidedit"> 
      </LABEL> 
      <BR> 
      <BR> 
      <LABEL for="itemidedit">Item Id 
       <INPUT type="text" id="itemidedit"> 
      </LABEL> 
      <BR> 
      <BR> 
      <LABEL for="quanedit">Quantity 
       <INPUT type="text" id="quanedit"> 
      </LABEL> 
      <BR> 
      <BR> 
      <LABEL for="potedit">Potency 
       <INPUT type="text" id="potedit"> 
      </LABEL> 
      <BR> 
      <BR> 
      <LABEL for="sizeedit">Size 
       <INPUT type="text" id="sizeedit"> 
      </LABEL> 
      <BR> 
      <BR> 
      <INPUT type="submit" id="editsubmit" name="editsubmit" value="Submit"> 
      <INPUT type="reset"> 
     </P> 
    </form> 
    <?php } ?> 
</body> 

我對驗證表單疑慮。我在這兩個頁面都嘗試了驗證碼。 但是沒有像firebug中顯示的ajax效果。同時採用ajaxtype「後」任何幫助appreciated..Thanks很多..

+2

將'debug:false'設置爲'true',看看裏面是否有任何信息。 – itachi 2013-05-11 08:50:22

+0

您的AJAX操作是否根本無法啓動?如果不是,則添加'alert'操作以跟蹤代碼執行的位置。 – halfer 2013-05-11 08:55:50

+0

@itachi謝謝..我沒有使用名稱屬性爲我的表單域..讓我!!再次感謝:) – user2234992 2013-05-11 08:56:46

回答

0

,你應該小心在傳遞數據 datadata1是將WebMethod或服務的參數。試試這個

var d1="asd"; 
$.ajax({ 
type: "POST", 
url: "aa.php", 
data: "{data:'"+d1+"',data1:'"+d2+"'}", 
cache: false, 
success: function(response) { 
    $('#result1').html(response); 
} 
});