2012-05-09 74 views
0

我正在緩慢地加載一個類,並需要通過該類運行表單字段以驗證代碼與W3C's HTML Validator's API的窗體上工作。表單域將包含url。我還沒有開始驗證輸入的數據。我只是想獲得相同的頁面來返回數據。該form.php的代碼我有幫助從How can I execute a PHP function in a form action?現在是:通過類和顯示的領域數據W3C驗證結果

<?php 
require_once ('api_w3cvalidation.class.php'); 
$validate = new W3cValidateApi; 
if (isset($_POST)) { 
    $uri = $_POST['uri']; // this will get you what was in the 
            // textfield if the form was submitted 
            $a = $validate->validate($uri); 
            if($a){ 
             echo 'Verified!'; 
            } else { 
             echo 'Not verified!<br>'; 
             echo 'Errors found: ' . $validate->ValidErrors; 
            } 
}; 

?> 

<form name="form1" method="post" action="<?php echo($_SERVER['PHP_SELF']) ;?>" > 
    <p>Your uri is: <?php echo $uri;?></p> 
    <label> 
     <input type="text" name="uri" id="textfield"> 
    </label> 
    <label> 
     <input type="submit" name="button" id="button" value="Submit"> 
    </label> 
</form> 

我使用的類是:

<? 
/* 
    Author: Jamie Telin ([email protected]), currently at employed Zebramedia.se 

    Scriptname: W3C Validation Api v1.0 (W3C Markup Validation Service) 

    Use:  
     //Create new object 
      $validate = new W3cValidateApi; 

      //Example 1 
       $validate->setUri('http://google.com/'); //Set URL to check 
       echo $validate->makeValidationCall();  //Will return SOAP 1.2 response 

      //Example 2 
       $a = $validate->validate('http://google.com/'); 
       if($a){ 
        echo 'Verified!'; 
       } else { 
        echo 'Not verified!<br>'; 
        echo 'Errors found: ' . $validate->ValidErrors; 
       } 

      //Example 3 
       $validate->ui_validate('http://google.com/'); //Visual display 

      //Settings 
       $validate->Output  //Set the type of output you want, default = soap12 or web 
       $validate->Uri   //Set url to be checked 
       $validate->setUri($uri) //Set url to be checked and make callUrl, deafault way to set URL 
       $validate->SilentUi  //Set to false to prevent echo the vidual display 
       $validate->Sleep  //Default sleeptime is 1 sec after API call 
*/ 

class W3cValidateApi{ 

    var $BaseUrl = 'http://validator.w3.org/check'; 
    var $Output = 'soap12'; 
    var $Uri = ''; 
    var $Feedback; 
    var $CallUrl = ''; 
    var $ValidResult = false; 
    var $ValidErrors = 0; 
    var $Sleep = 1; 
    var $SilentUi = false; 
    var $Ui = ''; 

    function W3cValidateApi(){ 
     //Nothing... 
    } 

    function makeCallUrl(){ 
     $this->CallUrl = $this->BaseUrl . "?output=" . $this->Output . "&uri=" . $this->Uri; 
    } 

    function setUri($uri){ 
     $this->Uri = $uri; 
     $this->makeCallUrl(); 
    } 

    function makeValidationCall(){ 
     if($this->CallUrl != '' && $this->Uri != '' && $this->Output != ''){ 
      $handle = fopen($this->CallUrl, "rb"); 
      $contents = ''; 
      while (!feof($handle)) { 
       $contents .= fread($handle, 8192); 
      } 
      fclose($handle); 
      $this->Feedback = $contents; 
      sleep($this->Sleep); 
      return $contents; 
     } else { 
      return false; 
     } 
    } 

    function validate($uri){ 
     if($uri != ''){ 
      $this->setUri($uri); 
     } else { 
      $this->makeCallUrl(); 
     } 

     $this->makeValidationCall(); 

     $a = strpos($this->Feedback, '<m:validity>', 0)+12; 
     $b = strpos($this->Feedback, '</m:validity>', $a); 
     $result = substr($this->Feedback, $a, $b-$a); 
     if($result == 'true'){ 
      $result = true; 
     } else { 
      $result = false; 
     } 
     $this->ValidResult = $result; 

     if($result){ 
      return $result; 
     } else { 
      //<m:errorcount>3</m:errorcount> 
      $a = strpos($this->Feedback, '<m:errorcount>', $a)+14; 
      $b = strpos($this->Feedback, '</m:errorcount>', $a); 
      $errors = substr($this->Feedback, $a, $b-$a); 
      $this->ValidErrors = $errors; 
     } 
    } 

    function ui_validate($uri){ 
     $this->validate($uri); 

     if($this->ValidResult){ 
      $msg1 = 'This document was successfully checked'; 
      $color1 = '#00CC00'; 
     } else { 
      $msg1 = 'Errors found while checking this document'; 
      $color1 = '#FF3300'; 
     } 
     $ui = '<div style="background:#FFFFFF; border:1px solid #CCCCCC; padding:2px;"> 
        <h1 style="color:#FFFFFF; border-bottom:1px solid #CCCCCC; margin:0; padding:5px; background:'.$color1.'; font-family:Arial, Helvetica, sans-serif; font-size:16px; font-weight:bold;"> 
        '.$msg1.' 
        </h1> 
        <div> 
         <strong>Errors:</strong><br> 
         '.$this->ValidErrors.' 
        </div> 
       </div>'; 
     $this->Ui = $ui; 
     if($this->SilentUi == false){ 
      echo $ui; 
     } 
     return $ui; 

    } 

} 
?> 

什麼我需要做的,使鏈接添加到文本字段通過課程並顯示驗證或沒有提交網址後?

更新

作了一些修改和驗證似乎工作。只有在實際驗證之前,它仍然顯示網站未經過驗證。

回答

1

您意外測試POST超全局是否已設置。

<?php 
/* ... */ 
if (isset($_POST)) { 
    /* ... */ 
} 

該變量幾乎總是被設置。相反,請嘗試測試其中是否有任何數據。

<?php 
if (count($_POST)) { 
    /* ... */ 
} 

如果你想要這個頁面加載是否是POST(例如,你還是要區別對待的要求,即使沒有值被張貼)的結果忠實地準確的測試,你可以檢查請求的方法直。

<?php 
if ($_SERVER['REQUEST_METHOD'] === "POST") { 
    /* ... */ 
} 
+0

就是這樣。謝謝。現在將工作在它的AJAX版本上,因爲獲取數據似乎花費了相當長的一段時間,我希望在運行中進行加載。 – rhand