Page 1 of 1

Form processing- please help debug

Posted: Thu Sep 25, 2003 5:39 pm
by jbatty
The code below was written to convert contents entered into of a html textarea to xml and echo it to the browser. 'mytest' is the name of the textarea and the form method is POST.

I tried to run the code below, i get an error message that
The following tags were not closed: questions. Error processing resource
However if i take my input from a local text file e.g.
$fp = fopen('questiontext.txt', 'r'); instead of
$mytext = $HTTP_POST_VARS["mytext"];
$fp = $mytext;
the script works OK.
Can anyone spot what i am doing wrong.
Any suggestions will be appreciated.

Code: Select all

credit: volka (sept03)
<?php 
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo "<questions>\n"; 

//patterns to describe delimiters 
$patternQuestion = '!^\d+&#1111;a-z]+\.\s+(.+)!'; 
$patternChoice = '!^&#1111;&#1111;:alpha:]]+\.\s+(.+)!'; 

$bInItem = false; // the only element not to be handled in one line 

$mytext = $HTTP_POST_VARS&#1111;"mytext"];
$fp = $mytext; 
while(!feof($fp)) 
&#123; 
   $row = trim(fgets($fp, 2048)); 
   if (strlen($row) > 0) 
   &#123; 
   		 //search for a match to expression given in questionPattern
      if (preg_match($patternQuestion, $row, $matches)) 
      &#123; 
         if ($bInItem) 
            echo "</item>\n"; 
         else 
            $bInItem = true; 
         echo "<item>\n   <question>$matches&#1111;1]</question>\n"; 
      &#125; 
      else if(preg_match($patternChoice, $row, $matches)) 
         echo "   <choice>$matches&#1111;1]</choice>\n"; 
      else 
         echo '# ', $row, "#\n"; 
   &#125; 
&#125; 
if ($bInItem) 
   echo '</item>'; 
echo '</questions>'; 
?>
As far as i can see the question tag is closed!

Posted: Fri Sep 26, 2003 11:12 am
by volka
$fp = fopen('questiontext.txt', 'r');
this opens a file and assigns the file descriptor to $fp (see also: fopen() manual page)
$fp = $mytext;
assigns the value of $mytest to $fp.

feof() and fgets() work on filedescriptors not on strings.

You might split the array and then use a foreach-loop to iterate through the (row-)strings instead of testing for the end-of-file/readline.

Posted: Fri Sep 26, 2003 3:42 pm
by JAM
I also suggest you take the opportunity to rewrite code...

Code: Select all

// from (note the quotes):
$mytext = $HTTP_POST_VARS["mytext"];

// to:
$mytext = $HTTP_POST_VARS['mytext'];
  // possible even (depending on your php version)
$mytext = $_POST['mytext'];