bradgrafelman;10998134 wrote:

Use [man]print_r/man or [man]var_dump/man on $POST['tape'] to see how the data gets submitted. You're going to end up with data like:

$_POST['tape'][0] = 'requesting from DACC';

EDIT: Also note that if you want to convert the array $_POST['tape'] into a comma-separated list of values stored as a string, take a look at the [man]implode/man function.

I don't think I need the implode function, but where should I put the [man]print_r/man or [man]var_dump/man on $_POST['tape'] ? I feel dumb to admit I have never used it before.

    Ok I did go and try the implode function but it threw an error at me. the interesting thing is it did send an email and it did put the bolded area TAPE recorder in the email but the values were not there from the check boxes.

    this is the error I got

    PHP Warning: implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in C:\Inetpub\wwwroot\DACC\ssc\accommodations3.php on line 292

    Line 292 is the line with the implode command

    $subject = "Request for Accommodations";
    			$headers  ='MIME-Version: 1.0' .PHP_EOL;
    			$headers .='Content-type: text/html; charset=iso-8859-1' . PHP_EOL;
    			$headers .= "From: noreply@dacc.edu\r\n" . "X-Mailer: php";
    			$greet = "The following was submitted on " . date("F j, Y, g:i a") . "<p><p>";
    			$tape = implode(', ', $_POST['tape']);
    

    and this is what I did in the area where it sends the email

    } elseif($efield == "tape" ) {
    							$body.= "<em>For Class Notes:</em><p><strong>". "Tape recorder: "."</strong>&nbsp;&nbsp;" . $tape.",<p>";
    
      Inky1231;10998149 wrote:

      Ok I did go and try the implode function but it threw an error at me. the interesting thing is it did send an email and it did put the bolded area TAPE recorder in the email but the values were not there from the check boxes.

      Btw this is what I did with the check box field

      	<tr>
      							<td style="width: 124px" class="style15">Tape Recorder<?php if(isset($problems['tape'])) {?>		<font color="red">*</font><?php } ?></td>
      							<td class="style9">	
      							<input name="tape[]"  type="checkbox" value="used before" <?php if(isset($_POST['tape']) && $_POST['tape'] == 'used before') { echo 'checked'; } ?>><span class="style15">
      							</span>					
      </td>
      							<td class="style9">	
      							<input name="tape[]"  type="checkbox" class="style15" value="helpful in past" <?php if(isset($_POST['tape']) && $_POST['tape'] == 'helpful in past') { echo 'checked'; } ?>> <span class="style15">
      							</span>					
      							<td class="style9">	
      							<input name="tape[]" class="style15" type="checkbox" value="requesting from DACC" <?php if(isset($_POST['tape']) && $_POST['tape'] == 'requesting from DACC') { echo 'checked'; } ?>><span class="style15">
      							</span>					
      							<td class="style9">	
      							<input name="tape[]"   type="checkbox" value="NA" class="style15"<?php if(isset($_POST['tape']) && $_POST['tape'] == 'NA') { echo 'checked'; } ?>><span class="style15">
      							</span>					
      						</tr>
      
        <input name="tape[]" class="style15" type="checkbox" value="requesting from DACC" <?php if(isset($_POST['tape']) && $_POST['tape'] == 'requesting from DACC') { echo 'checked'; } ?>><span class="style15">
        

        These lines should use in_array() to check for the current value, like so:

        <input name="tape[]" class="style15" type="checkbox" value="requesting from DACC" <?php if(isset($_POST['tape']) && in_array('requesting from DACC',$_POST['tape'])) { echo 'checked'; } ?>><span class="style15">

        As for your issue at hand, are you sure you are submitting this form to this script at this current version, or are you refreshing from before you made changes (submitting the old name="tape" checkboxes instead of the new name="tape[]" checkboxes)?

          Derokorian;10998152 wrote:
          <input name="tape[]" class="style15" type="checkbox" value="requesting from DACC" <?php if(isset($_POST['tape']) && $_POST['tape'] == 'requesting from DACC') { echo 'checked'; } ?>><span class="style15">
          

          These lines should use in_array() to check for the current value, like so:

          <input name="tape[]" class="style15" type="checkbox" value="requesting from DACC" <?php if(isset($_POST['tape']) && in_array('requesting from DACC',$_POST['tape'])) { echo 'checked'; } ?>><span class="style15">

          As for your issue at hand, are you sure you are submitting this form to this script at this current version, or are you refreshing from before you made changes (submitting the old name="tape" checkboxes instead of the new name="tape[]" checkboxes)?

          Derokorian,
          That worked! thank you! Now I just have one last question I have a LOT of checkboxes on this form and I don't think it would be the best way to do it to do the implode command for each checkbox set, can I just do something like the following

          $tape = implode(', ', $_POST['$efield']);

          and have it go through the $efield array?

            Not if you use single quotes, because PHP won't try to parse the contents which are the variable. Instead don't use quotes at all for the key. Secondly, it will give you problems if they aren't all arrays.

              Inky1231;10998146 wrote:

              I don't think I need the implode function, but where should I put the [man]print_r/man or [man]var_dump/man on $_POST['tape'] ? I feel dumb to admit I have never used it before.

              It's just a debugging technique you can use to dump an array out to the screen so that you can visually inspect it and make sure it contains what you think it contains. As such, it really doesn't matter where you put either of those function calls - just use them somewhere and look in the source code of the page in your browser and find the output.

              Inky1231;10998149 wrote:

              this is the error I got

              PHP Warning: implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in C:\Inetpub\wwwroot\DACC\ssc\accommodations3.php on line 292

              That would seem to suggest a problem with the $_POST['tape'] variable, which makes the request for a print_r() or var_dump() output all the more relevant and/or needed in order for us to determine the problem.

              Inky1231;10998156 wrote:

              Now I just have one last question I have a LOT of checkboxes on this form and I don't think it would be the best way to do it to do the implode command for each checkbox set

              Why not? Aren't you going to list them separately in the e-mail? Or are you just going to combine the values selected from all of the "sets" into one single list of values in the e-mail?

              If the latter is true, then you could indeed do some intelligent looping with only a few statements to cover all of the "sets." The same could be set for keeping them separate (e.g. $tape would be a list of values for the 'tape' set of checkboxes, $foo for the 'foo' set of boxes, etc. etc. for all sets), but we probably need to get more information about the structure of this form as well as how you intend to represent the values selected in the e-mail message you're constructing.

              Inky1231;10998156 wrote:

              can I just do something like the following

              $tape = implode(', ', $_POST['$efield']);

              and have it go through the $efield array?

              No, because '$efield' is literally a dollar sign followed by the word/characters "efield" (since you used single quotes; recall that variable interpolation does not occur within strings delimited with single quotes).

              Even if you had used double quotes, the index of an associative array must be a string, not another array, so $POST["$efield"] wouldn't make any sense either. Instead, you'll need to implement the logic that does what you're trying to get PHP to do automatically, e.g. looping through the $efield array and accessing the appropriate $POST indexes one-by-one in the body of that loop.

                I have 18 different check box sets, and then the rest are text fields. and they are all listed separately in the email so I guess I have to do do the

                	$tape = implode(', ', $_POST['tape']);
                

                for each checkbox field. Am I correct in that Summation?

                I am sorry for being so dense with this but I hadn't done much with Forms and Emailing forms in PHP before.

                  $checkboxes = array('tape','cd','book'); /// I have no idea what your fields are you haven't shown us
                  
                  $email .= "Checkboxes:\n";
                  foreach( $checkboxes as $name ) {
                     if( isset($_POST[$name]) && is_array($_POST[$name]) ) {
                        $email .= "$name: ". implode(', '$_POST[$name]) ."\n";
                     }
                  }
                    Derokorian;10998162 wrote:
                    $checkboxes = array('tape','cd','book'); /// I have no idea what your fields are you haven't shown us
                    
                    $email .= "Checkboxes:\n";
                    foreach( $checkboxes as $name ) {
                       if( isset($_POST[$name]) && is_array($_POST[$name]) ) {
                          $email .= "$name: ". implode(', '$_POST[$name]) ."\n";
                       }
                    }

                    Thanks Derokorian,
                    YEah there are so many of them it would put me over the posting limit 🙂 This sis a request for accommodations form for a Community college... Did one form for this one department and now they are giving me the tough things! LOL I will give this a try but if nothing else I can do the individual implodes for now 🙂

                      If you want to separate the form processing logic from the message construction, you could instead create the variables like $tape using a 'variable variables' approach, e.g.:

                      $checkboxes = array('tape','cd','book'); // I have no idea what your fields are you haven't shown us 
                      
                      foreach( $checkboxes as $name ) { 
                         if( isset($_POST[$name]) && is_array($_POST[$name]) ) { 
                            $$name = implode(', ', $_POST[$name]);
                         } else {
                            $$name = '';
                      }
                      
                      // assuming everything was POST'ed correctly, you now can use:
                      //         $tape, $cd, and $book
                      

                        Thanks for the reply bradgrafelman,
                        I think now I am starting to get a little overwhelmed here, but I will definitely try these suggestions.

                          Write a Reply...