2012-09-18 64 views
1

即時通訊嘗試使用php驗證表單,因爲它有一個需要驗證的文件輸入。使用cURL進行php驗證

基本上,我的表單的動作將其發送到validate.php,驗證表單域,要麼無效數據發送回在捲曲使用POST形式:

$data = array(
    'nameError' => $nameError, 
    'name' => $name, 
    'description' => $desc, 
    'descError' => $descError, 
    'spriceError' => $spriceError, 
    'sprice' => $sprice, 
    'iprice' => $iprice, 
    'incprice' => $incprice, 
    'incprice' => $incprice, 
    'durError' => $durError, 
    'location' => $location, 
    'img' => $imgError 
); 

foreach($data as $key=>$value){ 
    $field_string .= $key.'='.$value.'&'; 

} 
rtrim($field_string, '&'); 


$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, 'sellanitem.php'); 
curl_setopt($ch, CURLOPT_POST, count($data)); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $field_string); 

curl_exec($ch); 

curl_close($ch); 
header("Location: sellanitem.php"); 
在我的表單頁面

,我檢查以查看是否交數據被設定:

if(isset($_POST['name'])){ 
    $name = $_POST['name']; 

}else{ 
    $name = ""; 
} 

if(isset($_POST['nameError'])){ 
    $nameError = $_POST['nameError']; 
}else{ 
    $nameError = ""; 
} 

if(isset($_POST['durError'])){ 
    $durError = $_POST['durError']; 

}else{ 
    $durError = ""; 
} 

if(isset($_POST['description'])){ 
    $desc = $_POST['description']; 

}else{ 
    $desc = ""; 
} 

然後我顯示基於該變量的值的消息:

  <td> 
       <textarea id="name" name="name" title="name" rows="2" cols ="60" style="resize:none; float:right; font-family: Arial;" value=""><?php echo $name;?></textarea> 
<?php 
if($nameError != ""){ 
    echo $nameError; 
} 

我在谷歌查找「提交表單自動php」後來到這個方法。我的想法是,如果我可以自動提交表單,我可以簡單地將錯誤消息作爲數據字段添加到我的sellanitem.php中。我不知道該怎麼想,這個捲曲的東西讓我感到困惑,到目前爲止,我提交了帶有無效輸入的表單字段來測試它,它只是加載一個空白頁面。我也嘗試把標題(「位置:sellanitem.php」),但沒有顯示錯誤消息。

對可能出錯的建議/將實現相同結果的備用方法?

歡呼聲, 邦迪

+0

您可以在參數數組傳遞給'CURLOPT_POSTFIELDS'選擇,它會正確轉義參數(您目前缺少)。 – complex857

回答

1

,所以你需要給exec分配給一個變量你不設置輸出緩衝:

$data = curl_exec($ch); 

數據是什麼,它返回。如果你發回一串錯誤等,你是金。

+0

它仍然加載一個空白頁面。我是否需要將頭部包含回curl_exec頁面? – Bundy

+0

如果您正在重定向,則只需進入該頁面即可。從你的代碼中,沒有任何東西被帶到頁面上。 – wesside

+0

什麼是輸出緩衝區的意義。如果我想用curl發回到頁面的發佈數據,我只需使用$ _POST ['name']等? – Bundy

3

如果要分析來自卷邊要求退貨,您需要:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

除了

$output = curl_exec($ch); 

然而,看起來這是一個本地文件......爲什麼不能你只需include 'sellanitem.php'而不是通過cURL傳遞發佈數據?

另外值得注意的一個Location頭應該使用Absolute URI

+0

啊,很好的電話。 +1 – wesside

+0

我想在提交表單後驗證表單。會包括('sellanitem.php')這樣做? – Bundy

+0

如果你正在提交'validate.php',你可以在發佈之後加入'sellanitem.php'。 – Martin