-1

私は PHP を学んでいて、簡単な答えが欲しいという質問があります。過去に基本的なフォームを作成し、送信時に対応するメールをユーザーに送り返しました。

今回は、チェックボックスを 3 つにしました。どのチェックボックスが選択されているかに応じて、特定のメールを送信する必要があります。

たとえば、ドキュメント 1 と 3 を受け取ってから送信したい場合、電子メールでこれら 2 つのドキュメントをダウンロードするためのリンクが送信されます。

チェックボックスの選択ごとに1通ずつ、2通のメールを送信してもかまいません。受け取りたいドキュメントを 2 つ選択すると、ドキュメント 1 へのリンクが記載されたメールとドキュメント 3 へのリンクが記載されたメールが 1 通ずつ届きます。

PHPでこれを行うことができるかどうかはわかりません。

4

4 に答える 4

0

ドキュメントのチェックボックスを使用してフォームを作成する

<form method="post" action="test.php">
    Document 1: <input type="checkbox" name="document[]" value="document1" /><br />
    Document 2: <input type="checkbox" name="document[]" value="document2" /><br />
    Document 3: <input type="checkbox" name="document[]" value="document3" /><br />
    <br />
    <input type="hidden" name="send" value="1" />
    <input type="submit" value="Send" />
</form>

そしてそれを処理するためのphpスクリプト

<?php
    if(isset($_POST) && ($_POST['send'] == 1)){

        $documents = array(
                        'document1' => 'http://www.example.com/document1.doc',
                        'document2' => 'http://www.example.com/document2.doc',
                        'document3' => 'http://www.example.com/document3.doc'
                    );

        $to      = 'nobody@example.com';
        $subject = 'the subject';
        $message = "hello\n\n";

        if(isset($_POST['document']) && count($_POST['document']) > 0){
            foreach($_POST['document'] as $doc){
                if(isset($documents[$doc])){
                    $message .= "Here is ".$documents[$doc]."\n";
                }
            }
        }


        $headers = 'From: webmaster@example.com' . "\r\n" .
            'Reply-To: webmaster@example.com' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();

        mail($to, $subject, $message, $headers);
    }
?>

私はあなたが一般的な考えを持っていると思います。

于 2012-08-30T01:01:38.307 に答える
0
// create an array with the checkboxes so you can later easily expand it
// the indexes of the array are the fieldnames of the checkboxes
$checkboxes = array(
    'checkbox1' => 'http://example.com/somelink',
    'checkbox2' => 'http://example.com/somelink2',
    'checkbox3' => 'http://example.com/somelink3',
);

// generate mail body based on checked checkboxes
$mailContents = 'Your links:';
foreach($checkboxes as $name => $link) {
    if (isset($_POST[$name])) {
        $mailContents.= $link . "\n";
    }
}   

に含まれるリンクを文字列としてメール送信できるようになり$mailContentsました。

于 2012-08-30T00:56:29.877 に答える
0

すべてのチェックボックスに値を設定し、POST または GET メソッドを使用して 3 つのチェックボックスの値を送信してから、次のようにします。

if(check1 == "value"){
   //send email 1
}
if(check2 == "value"){
   //send email 2
}
if(check3 == "value"){
   //send email 3
}
于 2012-08-30T00:56:40.073 に答える
0

あなたがする必要があるのは次のとおりです: 次のようなチェックボックスを作成します:

<input type="checkbox" name="sendDoc1" value="Selected" /> Send me Document 1

次に、php がフォームをチェックするときに isset を使用して、入力内容をチェックします。

if(isset($_POST['sendDoc1']) &&
$_POST['sendDoc1'] == 'Selected')
{
  //Code to send email
}

これをドキュメントごとに繰り返すことができます

于 2012-08-30T00:58:51.463 に答える