Ok I have a form with multiple check boxes, these are not all required. When the form is submitted the checkboxes which have an implode set for them and were not checked come back with the PHP warning and the Notice that implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed .

How can I get rid of this when the checkbox is left empty?

Here is an example of my form areas

<td>
							<strong> Threat of Harm to Self or Others:</strong><br>

							<input name="harm[]"  type="checkbox" value="Student/Student" <?php if(isset($_POST['harm']) && in_array('Student/Student',$_POST['harm'])) { echo 'checked'; } ?>  >
						<label  >Student/Student </label>&nbsp;<span lang="en-us">&nbsp;&nbsp;&nbsp;
							<font size="-1" face="Arial, Helvetica, sans-serif" align="left">
							<input name="harm[]"  type="checkbox" value="Self" <?php if(isset($_POST['harm']) && in_array('Self',$_POST['harm'])) { echo 'checked'; } ?>>
						<label  >Self </label>

							<br>
							<input name="harm[]"  type="checkbox" value="Student/Faculty-Staff" <?php if(isset($_POST['harm']) && in_array('Student/Faculty-Staff',$_POST['harm'])) { echo 'checked'; } ?>>							
						<label>Student/Faculty-Staff </label>



			&nbsp;&nbsp;
		</font></span></td>
	</tr>

Here is the PHP

$fields = array('title', 'name','date', 'time', 'building', 'room', 'location', 'title1','person',  'phone1', 'title2', 'person2', 'phone2','title3','person3',  'phone3','title4','person4',  'phone4','occurrence','obscene','altercation1', 'altercation2', 'sexharass', 'property', 'harm', 'drugs', 'other',  'explanation', 'security', 'police', 'witness','wtitle1', 'wperson',  'wphone1', 'wtitle2', 'wperson2', 'wphone2','wtitle3','wperson3',  'wphone3', 'acttaken', 'followact');

		//$to = "mhoover@dacc.edu";
		$to2 = "aabdelzaher@dacc.edu";
	//	$to3 ="voliver@dacc.edu";
		$subject = "Incident / Unusual Occurrence Report";
		$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>";
		$sexharass = implode(', ', $_POST['sexharass']);
		$altercation1 = implode(', ', $_POST['altercation1']);
		$altercation2 = implode(', ', $_POST['altercation2']);
		$harm = implode(', ', $_POST['harm']);
		$property = implode(', ', $_POST['property']);	
		$drugs = implode(', ', $_POST['drugs']);
		$other = implode(', ', $_POST['other']);
		$body =   $greet  ; 




		$cn = 1;


		foreach($fields as $efield) {


			if(isset($_POST[$efield])) {

				if($efield == "occurrence") {
				$body.= "<strong>". " Type of Occurrence: "."</strong><p>";

					} elseif($efield == "obscene" ) {
						$body.=  $_POST[$efield] . "&nbsp; <P>";
					} elseif($efield == "altercation1" && $_POST[$efield] != '') {
						$body.= "<em>". " Altercation:&nbsp;"."</em>" . $altercation1 . "&nbsp;- &nbsp;";
					} elseif($efield == "altercation2" && $_POST[$efield] != '') {
						$body.= "<strong>". " "."</strong>" . $altercation2 . " &nbsp; <p> ";
					} elseif($efield == "sexharass" && $_POST[$efield] != '') {
						$body.= "<em>". " Sexual Harrasment: &nbsp;"."</em>" . $sexharass . "&nbsp; <p>";
					} elseif($efield == "property" && $_POST[$efield] != '') {
						$body.=   "<em>". "Damage to Property:&nbsp; "."</em>" . $property . "&nbsp;<p> 	";
					} elseif($efield == "harm" && $_POST[$efield] != '') {
						$body.=   "<em>". " Threat of Harm to Self or Others:&nbsp; "."</em>" . $harm 													."&nbsp;<p> ";
					} elseif($efield == "drugs" && $_POST[$efield] != '') {
						$body.=   "<em>". " Drugs or Alcohol:&nbsp; "."</em>" . $drugs ."&nbsp;<p> ";
					} elseif($efield == "other" && $_POST[$efield] != '') {
						$body.=   "<em>". " Other types of Occurrences: &nbsp;"."</em>" . $other ."&nbsp;<p> ";

    Inky1231;10998482 wrote:

    How can I get rid of this when the checkbox is left empty?

    Use [man]empty/man (or [man]isset[/man]) to first check if the external data exists at all, and also use [man]is_array/man to verify that the data is an array as expected.

      bradgrafelman;10998483 wrote:

      Use [man]empty/man (or [man]isset[/man]) to first check if the external data exists at all, and also use [man]is_array/man to verify that the data is an array as expected.

      Thanks for replying to me yet again bradgrafelman, you must be getting tired of seeing my posts 🙂

      I tried this

      	if( is_array($_POST['sexharass']) )
      {
      			$sexharass = implode(', ', $_POST['sexharass']);}
      			if( is_array($_POST['altercation1']) )
      {
      $altercation1 = implode(', ', $_POST['altercation1']);}
      			if( is_array($_POST['altercation2']) )
      {
      $altercation2 = implode(', ', $_POST['altercation2']);}
      

      and that got rid of the implode warning but I still get an PHP Notice: Undefined index: warning. and that is what I am stuck at right now

        Again, you should first and foremost always be using [man]isset/man or [man]empty/man to check if external data exists before attempting to access it. None of your three if() statements above are doing any such checking before attempting to access external data.

          bradgrafelman;10998486 wrote:

          Again, you should first and foremost always be using [man]isset/man or [man]empty/man to check if external data exists before attempting to access it. None of your three if() statements above are doing any such checking before attempting to access external data.

          Right after I posted previously I did try doing an isset, I have obviously missed something because I got this error with it : PHP Parse error: syntax error, unexpected T_IF, expecting ')'

          	if( is_array(if(isset( $_POST['sexharass'])) )
          			{
          			$sexharass = implode(', ',$_POST['sexharass']));}
          

          I also tried it this way

          if( is_array($_POST['sexharass']) )
          			{
          			$sexharass = implode(', ',if(isset( $_POST['sexharass'])));}
          

          and got this error : PHP Parse error: syntax error, unexpected T_IF

            Neither of those makes any sense since [man]if/man statements don't return any values. Thus, even if your syntax above could somehow be parsed, it would end up looking like:

            if( is_array( )

            (note you're also missing a ')' to close the if() statement)
            or:

            $sexharass = implode(', ', );

            All you need to do is add the isset()/empty() check to your existing if() statements, AND'ed with your current conditions. Example:

            if(isset($_POST['foo']) && is_array($_POST['foo']))
            {
                // if $_POST['foo'] didn't even exist, PHP would never bother calling is_array()
                // or, obviously, entering this if() block.
            }

              Got it, thanks for the replies again, If it weren't for you and a few others on this forum I would be putting my head through a wall about now!

              I am learning that what I thought was a pretty good amount of instruction in PHP barely scratched the surface!

                bradgrafelman;10998497 wrote:

                Looks fine to me... why do you ask?

                hehe I hadn't realized I also needed the && array thing up by the implode. I swear PHP is going to be the death of me!

                  Write a Reply...