0

ユーザーがチェックボックス1を選択すると、2つのチェックボックスを持つフォーム(page2)があり、3ページ目のスクリプトは指示1を電子メールアドレスに送信します。チェックボックス 2 が選択されると、指示が送信されます2。

これは機能していますが、チェックボックスが1つしかチェックされていない場合、ファイル名を空にできないなどのエラーも発生します。

これはページ2のコードです

    <form name="form" method="POST" action="instructies_verzenden2.php"> 
    <p></p>
    <input type="checkbox" name="i_allinonewebsolution" value="file_1.txt"><span       class="info-image information">All-in-One Websolution</span>
    <input type="checkbox" name="i_custommade" value="file_2.txt"><span class="info-image information">Custom Made</span>
    <input type="hidden" name="keyfield" value="<?php echo $domeinnaam?>"><input type="hidden" name="emailadres" value="<?php echo $data2[emailadres]?>"><input type="submit"  class="sub-small sub-opslaan green" value="Update gegevens">
    <p></p>
    </form>

これはページ3のコードです

    // array with filenames to be sent as attachment
    $i_allinonewebsolution=$_POST['i_allinonewebsolution'];
    $i_custommade=$_POST['i_custommade'];
    $files = array("$i_allinonewebsolution","$i_custommade");

    // preparing attachments
    for($x=0;$x<count($files);$x++){
    $file = fopen($files[$x],"rb");
    $data = fread($file,filesize($files[$x]));
    fclose($file);
    $data = chunk_split(base64_encode($data));
    $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    $message .= "--{$mime_boundary}\n";
    }

問題はここにあると思います:

    // array with filenames to be sent as attachment
    $i_allinonewebsolution=$_POST['i_allinonewebsolution'];
    $i_custommade=$_POST['i_custommade'];
    $files = array("$i_allinonewebsolution","$i_custommade");

implode と array_filter を使用しようとしましたが、間違っていると思います。助けてくれてありがとう。

アップデート

フォームページをjsfiddleに追加しました。最初のjsfiddleに3ページのリンクがあります

http://jsfiddle.net/BNDfg/3/

4

3 に答える 3

1

このようなことを試してください

$files = array();
if(isset($_POST['i_allinonewebsolution'])) {
   array_push($files, $_POST['i_allinonewebsolution']);
}
if(isset($_POST['i_custommade'])) {
   array_push($files, $_POST['i_custommade']);
}

これをスケーリングしてチェックボックスを増やしたい場合は、次のような配列に配置できます

$checkboxes = array('i_allinonewebsolution',
                    'i_custommade',
                    'i_checkbox3',
                    'i_checkbox4');

$files = array();

foreach($checkboxes as $checkbox){
    if(isset($_POST[$checkbox])) {
       array_push($files, $_POST[$checkbox]);
    }
}
于 2013-03-12T14:16:21.733 に答える
0

POST値が空でないことを確認し、2ページの実際のロジックに進む前に、if(empty($ _ POST ['i_allinone'])&& empty($ _ POST ['custommade']))のような単純なif条件を試してください。

nullis_setなどを使用して、POSTまたはREQUESTの値が空かどうかを確認することもできます

 $i_allinonewebsolution=$_POST['i_allinonewebsolution'];
$i_custommade=$_POST['i_custommade'];
if(!empty($i_allinonewebsolution))
{
$files = array("$i_allinonewebsolution");
}
if(!empty($i_custommade))
{
$files = array("$i_custommade");
}
if(!empty($i_allinonewebsolution) && !empty($i_custommade))
{
$files = array("$i_allinonewebsolution","$i_custommade");
}

試してみてください!拡張コードでごめんなさい。

于 2013-03-12T14:23:46.580 に答える
0

DB クエリで同様のことを行う場合 (Select クエリでは結果が通常 NULL になる可能性があります)、変数が設定されているかどうかをテストします。

isset を使用して、データが実際に投稿されたかどうかをテストします。

例えば

if(isset($var))
//Do Action

http://php.net/manual/en/function.isset.php

于 2013-03-12T14:18:19.473 に答える