We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23849
    • 223 Posts
    Hi All,

    I’m creating some snippets to interact with a server. I’m basically sending said server some XML. I’m taking some form data, saving it the database, then using some PHP to convert the sql into xml.

    Unfortunately, as soon as I include the "xml header" declaration in my snippet, everytime I save something in modx (a snippet, chunk, or resource) I get these two errors:

    Warning: Unexpected character in input: ’\’ (ASCII=92) state=1 in /home/inter/public_html/manager/processors/cache_sync.class.processor.php on line 343

    Warning: Cannot modify header information - headers already sent by (output started at /home/inter/public_html/manager/processors/cache_sync.class.processor.php:343) in /home/inter/public_html/manager/processors/save_snippet.processor.php on line 151


    Here’s my snippet, it’s not fully functioning yet by any means but it’s causing the issue (the line causing the issue is where I declare the xml version - <?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>):

    <?php
    // Submit Reservation Request
    ############
    ## CONFIG ##
    ############
    global $modx;
    $Path = $modx->config['base_path'];
    $ReservationRequests = $modx->db->config['dbase'].".`reservation_requests`";
    
    //Get IP
    if ($_SERVER['HTTP_X_FORWARD_FOR']) {
    $ip = $_SERVER['HTTP_X_FORWARD_FOR'];
    } else {
    $ip = $_SERVER['REMOTE_ADDR'];
    }
    	
    	//IF THERE'S ANY ERRORS, STOP THEM HERE, ELSE CONTINUE	
    	if($_POST['vc_release'] == '' || $_POST['nights'] == '' || $_POST['adults'] == ''){
    	
    		echo '<h3>There were errors in your reservation request:</h3>
    		<ul class="errors">';		
    		if($_POST['vc_release'] == ''){
    			echo '<li>Please Select a Date.</li>';
    		}		
    		if($_POST['nights'] == ''){
    			echo '<li>Please Enter At Least One Night.</li>';
    		}		
    		if ($_POST['adults'] == ''){
    			echo '<li>Please Enter At Least One Adult.</li>';
    		}		
    		echo '</ul>
    		<h3>Please re-submit your request at the top of the site.</h3>';
    	
    	}else{ 
    		######################################
    		## ADDING RESERVATION REQUEST TO DB ##
    		######################################
    		
    		if($_POST['CHECK_RESERVATION']=="TRUE"){
    			//echo 'Success!';
    			$Date = $_POST['vc_release'];
    			$Nights = $_POST['nights'];
    			$Adults = $_POST['adults'];
    			$Children = $_POST['children'];
    			$Insert = $modx->db->query("INSERT INTO " . $ReservationRequests . " (date_requested, nights_requested, adults, children, user_ip, user_agent) 
    			VALUES ('" .$Date. "', '" .$Nights. "', '" .$Adults. "', '" .$Children. "', '" .$ip. "', '" .$_SERVER['HTTP_USER_AGENT']. "')");			
    			
    			/**
    			 * CONVERT OUR RECENTLY INSERTED SQL DATA TO XML, SETUP THE FOLLOW PARAMETERS FOR THE FUNCTION
    			 * @param mysql_resource - $queryResult - mysql query result
    			 * @param string - $rootElementName - root element name
    			 * @param string - $childElementName - child element name
    			 */
    			 
    			//Set The Query Result to Simply Grab what we just inserted
    			$queryResult = mysql_insert_id();
    			$rootElementName = 'megaweb';
    			$childElementName = 'request';
    			
    			function sqlToXml($result, $rootElementName, $childElementName)
    			{ 
    				$xmlData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"; 
    				$xmlData .= "<" . $rootElementName . ">";
    			 
    				$xmlData .= "<header sysID=\"clientsvc\" procId=\"\" password=\"\" mesgNum=\"01234\" megaSystem=\"prod\" />";
    				
    				while($record = mysql_fetch_object($queryResult))
    				{ 
    					/* Create the first child element */
    					$xmlData .= "<" . $childElementName . " reqCode=\"avalReq\">";
    			 
    					for ($i = 0; $i < mysql_num_fields($queryResult); $i++)
    					{ 
    						$fieldName = mysql_field_name($queryResult, $i); 
    			 
    						/* The child will take the name of the table column */
    						$xmlData .= "<" . $fieldName . ">";
    			 
    						/* We set empty columns with NULL, or you could set 
    							it to '0' or a blank. */
    						if(!empty($record->$fieldName))
    							$xmlData .= $record->$fieldName; 
    						else
    							$xmlData .= "null"; 
    			 
    						$xmlData .= "</" . $fieldName . ">"; 
    					} 
    					$xmlData .= "</" . $childElementName . ">"; 
    				} 
    				$xmlData .= "</" . $rootElementName . ">"; 
    			 
    				return $xmlData; 
    			}//end sqlToXml
    			
    			header("Location: reservation-request.html");
    		
    		}//end CHECK_RESERVATION']=="TRUE"
    	
    	}//end first if
    	
    ?>
    


    It seems to correctly save stuff, but the error is a bit disconcerting. Anyone else run across this issue with declaring some xml in a snippet? Let me know if there’s a fix, thanks!
      Nick Hoag
      Creative Partner
      The FutureForward

      http://thefutureforward.com
    • Try breaking up your <? and ?> into concatenated lines.
      $blah = "<";
      $blah .= "? ... ?";
      $blah .= ">";
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 23849
        • 223 Posts
        Worked like a charm! Thank-you so much Susan.
          Nick Hoag
          Creative Partner
          The FutureForward

          http://thefutureforward.com