0

PHP登録システムを構築しています。私のウェブサイトに新しいユーザー登録があると、通知メールが送信されます。しかし問題は、ユーザーが何も入力しないと、私にもメールが送信されることです。この問題を解決するにはどうすればよいですか?

これは検証部分です。

 /*Validation Begins*/
if(empty($_POST) === false) {
        $required_fields = array('school_name', 'mailing_address', 'postcode', 'courier_address', 'courier_postcode', 'courier_postcode', 'phonenumber', 'faxnumber', 'email', 'website', 'principal_fullname', 'principal_phonenumber', 'principal_email');
        foreach($_POST as $key=>$value){
            if(empty($value) && in_array($key, $required_fields) === true){
                mysql_close();
                ?>
                <script type="text/javascript">
                alert("Fields marked with an asterisk are required");
                history.back();
                </script>
                <?php
            }
    }

以下は、挿入機能とメール機能です。

function register_school($register_data){
$fields = '`' . implode('`, `', array_keys($register_data)) . '`';
$data = '\'' . implode('\', \'', $register_data) . '\'';

mysql_query("INSERT INTO `schools_info` ($fields) VALUES ($data)");

    ?>
    <script type='text/javascript'>
    alert("Registration Successful!"); window.location.href = '/registration-success/';
    </script>
    <?php
    $schoolname = $_POST['school_name'];
    $mailing_address  = $_POST['mailing_address'];
    $postcode = $_POST['postcode'];
    $courier_address = $_POST['courier_address'];
    $phonenumber = $_POST['courier_postcode'];
    $faxnumber = $_POST['faxnumber'];
    $email = $_POST['email'];
    $website = $_POST['website'];
    $principal_fullname = $_POST['principal_fullname'];
    $principal_phonenumber = $_POST['principal_phonenumber'];
    $principal_email = $_POST['principal_email'];

    $to = "example@hotmail.com";
    $subject = "New Registered School";
    $message = "School Name: $schoolname\r\nSchool Address: $mailing_address\r\nPostcode: $postcode\r\nCourier Address: $courier_address\r\nCourier Postcode: $courier_postcode\r\nPhone Number: $phonenumber\r\nFax Number: $faxnumber\r\nEmail: $email\r\nWebsite: $website\r\nPrincipal Name: $principal_fullname\r\nPrincipal Phone Number: $principal_phonenumber\r\nPrincipal Email: $principal_email";
    $from = "testing.com";
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);

}

学校登録

if(empty($_POST) === false){
$register_data = array(
    'school_name'           => $_POST['school_name'],
    'mailing_address'       => $_POST['mailing_address'],
    'postcode'              => $_POST['postcode'],
    'courier_address'       => $_POST['courier_address'],
    'courier_postcode'      => $_POST['courier_postcode'],
    'phonenumber'           => $_POST['phonenumber'],
    'faxnumber'             => $_POST['faxnumber'],
    'email'                 => $_POST['email'],
    'website'               => $_POST['website'],
    'principal_fullname'    => $_POST['principal_fullname'],
    'principal_phonenumber' => $_POST['principal_phonenumber'],
    'principal_email'       => $_POST['principal_email'],
    'CScoor'                => $_POST['CScoor'],
    'CS_email'              => $_POST['CS_email'],
    'CS_phone'              => $_POST['CS_phone'],
    'Engcoor'               => $_POST['Engcoor'],
    'Eng_email'             => $_POST['Eng_email'],
    'Eng_phone'             => $_POST['Eng_phone'],
    'Mcoor'                 => $_POST['Mcoor'],
    'M_email'               => $_POST['M_email'],
    'M_phone'               => $_POST['M_phone'],
    'Sccoor'                => $_POST['Sccoor'],
    'Sc_email'              => $_POST['Sc_email'],
    'Sc_phone'              => $_POST['Sc_phone']

);
register_school($register_data);

mysql_close();

}

4

2 に答える 2

0
    if(empty($_POST) === false) {
            $required_fields = array('school_name', 'mailing_address', 'postcode', 'courier_address', 'courier_postcode', 'courier_postcode', 'phonenumber', 'faxnumber', 'email', 'website', 'principal_fullname', 'principal_phonenumber', 'principal_email');
            $flag = TRUE;
            foreach($_POST as $key=>$value){
                if(empty($value) && in_array($key, $required_fields) === true){
                    mysql_close();
                    $flag = FALSE;
                    ?>
                    <script type="text/javascript">
                    alert("Fields marked with an asterisk are required");
                    history.back();
                    </script>
                    <?php
                } // end second if
        } //end foreach
       if ($flag == TRUE){
         $register_data = array(.....); //all $_POST value assign here
         register_school($register_data);}
} //end first if
于 2013-04-23T08:01:40.497 に答える
0

部品の前:

    $to = "example@hotmail.com";
    $subject = "New Registered School";
    $message = "School Name: $schoolname\r\nSchool Address: $mailing_address\r\nPostcode: $postcode\r\nCourier Address: $courier_address\r\nCourier Postcode: $courier_postcode\r\nPhone Number: $phonenumber\r\nFax Number: $faxnumber\r\nEmail: $email\r\nWebsite: $website\r\nPrincipal Name: $principal_fullname\r\nPrincipal Phone Number: $principal_phonenumber\r\nPrincipal Email: $principal_email";
    $from = "testing.com";
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);

始まります。すべての変数が空かどうかを確認します。

// 編集: 申し訳ありませんが、値に関する配列があることがわかりませんでした。次に、次のようにすることができます。

$success = true;
foreach($register_data as $data) {
    if (empty($_POST[$data])) {
       $success = false;
        break;
    }
}

if ($success == true) {
    //then mail
}
于 2013-04-23T07:37:12.727 に答える