2016-05-12 4 views
0

私はPHPで処理しているフォーム提出に入力されたデータを検証しようとしています。まず、フィールドが埋め込まれていることを確認した後、何らかの理由ですべてが無視されているように見え、データは私が何をしようとしてもDBに提出されています。フォームのバックエンド検証が無視されます

// example of validation (this is just duplicated for 4 fields) 
if (isset($_POST["fullname"]) && !empty($_POST["fullname"])) { 
    header('Location: http://mywebsite.com/error'); 
} else { 
    // Trying to get it to redirect at all 
    header('Location: http://mywebsite.com/error'); 
} 

$email = $_POST['email']; 
// using google code email validation 
include('EmailAddressValidator.php'); 
$validator = new EmailAddressValidator; 
if ($validator->check_email_address($email)) { 
    // Email address is technically valid 
} else { 
    header('Location: http://mywebsite.com/error'); 
} 

// If this line is reached, submit the data to the DB but there's no way  this line should be reached because the very first if & else statement should send it to the error page. 

ご協力いただければ幸いです。この時点でこれを数時間修正しようとしていました。下の例を参照してくださいheader.pleaseにリダイレクトした後exit;を追加

+3

ヘッダーでリダイレクトしてもプロセスが停止するわけではなく、単にヘッダーを送信し、プロセスを終了させる場合は 'exit();'を追加してください。 –

+0

'isset'と'!empty'は必要ありません。 '!empty'で十分です。 – chris85

+0

@JonStirlingが指を置いています。 header()文は実行を停止しません。あなたのコードは、直前のheader()を実行し続けます。 header()の後で実行を止めたい場合は、そこで停止させる必要があります。 –

答えて

0

試してみてください。

以下
header('Location: http://mywebsite.com/error');exit; 

はあなたのフォームを送信する方法の一例です:

// example of validation (this is just duplicated for 4 fields) 
if (isset($_POST["fullname"]) && isset($_POST["email"])) { 
    // using google code email validation 
    include('EmailAddressValidator.php'); 
    $validator = new EmailAddressValidator; 

    $email = $_POST['email']; 

    if (empty($_POST["fullname"]) && !$validator->check_email_address($email)) { 
     header('Location: http://mywebsite.com/error');exit; 
    } else { 
     // enter into data here 
    } 
} 
+0

ありがとうございました! – ShaneC

+0

'exit'が意図的に欠けていましたか? –

+0

@JonStirlingが追加されました: – Ali

-1

最初の場合は第1のORではないにし、 ($ _ POST ["fullname"])) 2番目にあなたが言う必要があります。 DB内

+0

なぜそれを「または」にしますか? – chris85

+0

私はあなたが空のユーザー名を必要としないと思うから...とスペース値 ""を持つユーザー名を設定することができます – makoulis

+0

条件が書かれているので、私は逆を仮定します。 – chris85

関連する問題