<?php
$pages = get_pages( array( 'parent' => 27, 'post_status' => 'publish,draft' ) );
//if the form is posted, has all the mandatory fields and captcha matches
if ( !empty($_POST['services-submit'] ) ) {
if ( $_SERVER["REQUEST_METHOD"] == "POST" ) {
?>

				<?php
				foreach( $pages as $page ) {
					if( array_key_exists( $page->ID, $_POST ) && ( isset( $_POST[$page->ID] ) || $_POST[$page->ID] == 'checked' ) ) {
						wp_update_post( array('ID' => $page->ID, 'post_status' => 'publish' ) );
					} else {
						wp_update_post( array('ID' => $page->ID, 'post_status' => 'draft' ) );
					}
				}
				unset($_POST);

echo '<div class="notice notice-success is-dismissible custom-notice"><p><strong>Successfully updated.</strong></p></div>';	

} }
//if the form has not been posted	
?>
<form id="form-services" method="post" action="<?php echo get_permalink(); ?>#services-pages">
	<?php
	$pages = get_pages( array( 'parent' => 27, 'post_status' => 'publish,draft' ) );
	foreach( $pages as $page ) {
		if( $page->post_status == 'publish' ) {
			$checked = 'checked';
		} else {
			$checked = NULL;
		}
		echo '<p class="services-item"><input type="checkbox" id="check-'.$page->ID.'" name="'.$page->ID.'" value="'.$checked.'" '.$checked.'/><label for="check-'.$page->ID.'">'.$page->post_title.'</label></p>';
	}
	?>
	<div class="update-wrap">
		<p class="alert"><strong>Changing the options above will take time to update, please be patient.</strong></p>
		<p><input type="checkbox" id="confirm-update-services" required/><label for="confirm-update-services">Are you sure you want to update the services pages selected?</label></p>
		<p><input type="submit" name="services-submit" value="Update Services Pages" class="button button-primary"/></p>
	</div>
	<i class="clearfix"></i>
</form>