Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a form(page2) with 2 checkboxes when a user selects checkbox 1 the script on the 3rd page will send instructions 1 to the emailaddress. When chkbox 2 is selected it sends instructions2.

This is working but i also get errors like filename cannot be empty when only 1 chkbox is checked.

this is the code from page 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>

this is the code from page 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";
    }

I think the problem is here:

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

I have tried to use implode and array_filter but i am doing that wrong i guess. Thanks for any help.

UPDATE

I have added the form pages to jsfiddle. there are 3 pages links are in the first jsfiddle

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

share|improve this question
1  
make sure POST values are not empty, try a simple if condition like if(empty($_POST['i_allinone']) && empty($_POST['custommade'])) before you proceed into actual logic on page 2 – Rama raju dantuluri Mar 12 at 14:17
if(isset($_post['i_allinonewebsolution'])) { $i_allinonewebsolution = $_POST["i_allinonewebsolution"]; } if(isset($_post['i_custommade'])) { $i_custommade = $_POST["i_custommade"]; } $files = array("$i_allinonewebsolution","$i_custommade"); this gave me the error twice – hennysmafter Mar 12 at 14:52
Are you testing this in localhost ? – Rama raju dantuluri Mar 12 at 14:55
no on life website but i cant share login details i can send the pages to email – hennysmafter Mar 12 at 15:01
Hey i dont mean about login credentials, Some time while trying on xampp localhost warnings or errors such as if any of the POST values are not assigned can be eliminated. – Rama raju dantuluri Mar 12 at 15:06
show 1 more comment

3 Answers

up vote 1 down vote accepted

Try something like this

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

If you are wanting to scale this and have more check boxes you could put them in an array like

$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]);
    }
}
share|improve this answer
i_allinonewebsolution']) } it gives an error on the } I dont know why? – hennysmafter Mar 12 at 14:43
Missed the semi-colons sorry – Jacob Tomlinson Mar 12 at 15:01
Number one rule of PHP development, if you get an error you probably missed a semi-colon. – Jacob Tomlinson Mar 12 at 15:02
Thats it I will update the description above with your solution many thanks. I assume that when i need to add 30 more checkboxes i just keep adding the IF } for each checkbox. – hennysmafter Mar 12 at 15:20
Yeah you could do that but I'll add some more code for scalability – Jacob Tomlinson Mar 12 at 15:31
show 1 more comment

When I do similar things with DB queries (where results can commonly be NULL in Select queries) I do a test to see if the variable has been set.

Use isset to test if data was actually posted.

e.g.

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

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

share|improve this answer
i have done this. but it still gives errors. $files = array("$i_allinonewebsolution","$i_custommade"); if(isset($files)) – hennysmafter Mar 12 at 14:48

make sure POST values are not empty, try a simple if condition like if(empty($_POST['i_allinone']) && empty($_POST['custommade'])) before you proceed into actual logic on page 2

You can also use null, is_set etc to check if the POST or REQUEST values are empty

 $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");
}

Just give a try ! Sorry for extended code.

share|improve this answer
i dont understand the code. when it is empty it just needs to ignore. I have no idea how to implement the if else into the code i posted. – hennysmafter Mar 12 at 14:47
This wouldn't scale very well if more checkboxes were to be added. – Jacob Tomlinson Mar 12 at 15:34
Yeah i agree with you. I just tried to explian how to overcome the current scenario.. – Rama raju dantuluri Mar 12 at 16:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.