2011-12-09 2 views
0

OK]をクリックしてPHPのフィードバックフォームのチェックボックスエラー

<?php 
//please fill this in at least! 
$myemail = ""; 
$title = "Feedback Form"; 
if(isset($_POST['submit'])) { //form has been submitted 
//set variables with filters 
$cont_name = filter_var($_POST['cont_name'], FILTER_SANITIZE_STRING); 
$email = filter_var($_POST['cont_email'], FILTER_SANITIZE_STRING); 
$phone = filter_var($_POST['cont_phone'], FILTER_SANITIZE_STRING); 
$first_time = filter_var($_POST['first_time'], FILTER_SANITIZE_STRING); 
$hear_about = filter_var($_POST['hear_about'], FILTER_SANITIZE_STRING); 

function valid_email($str){ 
    return (! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;} 

    $errors = 0; //by default there are no errors 

    $trimcont_name = trim($cont_name); 
    if(empty($trimcont_name)){ 
     //the name field is empty 
     $errors = 1; //tips off the error messages below 
     $errorcont_name = "The name field is empty"; //this error is displayed next to the label 
    } 
    if(!valid_email($email)) { 
     //email is invalid or empty 
     $errors = 1; 
     $erroremail = "The email address was not valid"; 
    } 
    $trimphone = trim($phone); 
    if(empty($trimphone)){ 
     //the phone field is empty 
     $errors = 1; 
     $errorphone = "The phone field is empty"; 
    } 
    $trimfirst_time = trim($first_time); 
    if(empty($trimfirst_time)){ 
     //the first_time field is empty 
     $errors = 1; 
     $errorfirst_time = "This field is empty"; 
    } 
    $trimhear_about = trim($hear_about); 
    if(empty($trimhear_about)){ 
     //the hear_about field is empty 
     $errors = 1; 
     $errorhear_about = "This field is empty"; 
    } 
    if($spam != "") { 
     //spam was filled in 
     $errors = 1; 
     $errorspam = "The Spam box was filled in"; 
    } 

    if($errors == 0) { 
     $sendto = $myemail; 
     $message = <<<DATA 
DETAILS 

Name: $cont_name 
Email: $email 
Phone: $phone 

Was this the first time you have been to us? 
$first_time 
How did you hear about us? 
$hear_about 

DATA; 
     $headers = 'From: ' . $name . '<' . $email . '>'; 
      if(mail($sendto, $title, $message, $headers)) { 
       //this is where it sends, using the php mail function 
       $success = true; 
       //set all the variables to blank to prevent re-submitting. 
       $cont_name = ""; 
       $email = ""; 
       $phone = ""; 
       $hear_about = ""; 
       $first_time = ""; 
} else { 
       $success = false; 
      } 

    } else { 
     $success = false; 
    } 
} 

?> 

(チェックボックスが正しくを介して送信されていない)、私の連絡先フォームのPHPの短縮バージョンであり、正常に機能していない領域が

<fieldset> 
    <legend>How did you hear about us? <span class="phpformerror"><?php echo $errorhear_about; ?></span></legend> 
    <div><input type="checkbox" name="hear_about[]" value="Web" /> Web</div> 
    <div><input type="checkbox" name="hear_about[]" value="Newspaper" /> Newspaper</div> 
    <div><input type="checkbox" name="hear_about[]" value="Radio" /> Radio</div> 
    <div><input type="checkbox" name="hear_about[]" value="Driving" /> Driving Past</div> 
    <div><input type="checkbox" name="hear_about[]" value="Referal" /> Referal</div> 
    <div><input type="checkbox" name="hear_about[]" value="Other" /> Other</div> 
</fieldset> 
です

現在、複数の変数が選択されている場合は、変数の1つが表示されます。

答えて

2

hear_aboutは配列であり、filter_var()は配列を正しく処理しません。代わりにfilter_var_array()を使用します。

$hear_about = filter_var_array($_POST['hear_about'], FILTER_SANITIZE_STRING); 

$hear_aboutが配列され、そしてあなたのコード全体で1(例えば、単に$hear_aboutを使用すると、それは$hear_about[0]$hear_about[1]、などであることが必要であり、機能しません)のように扱われなければならないことに注意してください。配列を扱うの利点を保持します

foreach($hear_about as $key => $value) { 
$trimhear_about[$key] = trim($value); 
    if(empty($trimhear_about[$key])){ 
     //the hear_about field is empty 
     $errors = 1; 
     $errorhear_about[$key] = "This field is empty"; 
    } 
} 

この:

だからあなたのトリムラインで、たとえば次のようなものが必要でしょう。

+1

これは正しいです、これは配列に含まれる文字列を消毒するために機能します。しかし、スクリプトの残りの部分では '$ hear_about'を文字列のように使用するので、ユーザの問題を解決することはできません。 –

+0

+1良いキャッチ、上記のユーザーに宛てたメモを – themerlinproject

+0

に投稿しました。フィードバックにはお世話になりました。これは、フォームに記入してチェックボックスを完全に逃してしまうまで完全に機能します。警告:filter_var_array()は、パラメータ1が配列になることを期待しています。...、&Warning:あなたがアドバイスしたコードに関連するforeach()に無効な引数が指定されています。 – user966834

0

$_POST['hear_about']は、値の配列です。あなたは簡単な文字列として扱っています!

私はあなたは、単に行を置き換える解決することができると思う:

$hear_about = filter_var($_POST['hear_about'], FILTER_SANITIZE_STRING); 

付:

$hear_about = filter_var(implode(', ', $_POST['hear_about']), FILTER_SANITIZE_STRING); 

implode function (doc)配列を連結することで文字列配列を "変換"与えられた接着剤の値。だから、選択した「どのように私たちについて聞いたか」を連結することができます。オプションをカンマで置き換えて、結果の文字列を他のデータとして使用します。

+0

使いやすい 'filter_var_array'これは正確なシナリオ用に設計されています – themerlinproject

関連する問題