Page 1 of 1

input data doesnt compare with the text data

Posted: Thu Jan 07, 2010 10:00 pm
by angelic_devil
the code gives the message as "no match" even when there is a match

Code: Select all

 
<?php
$myheading="";
$mylink="";
$mybody="";
 
if (isset($_POST['submit'])) {
$mylink = $_POST["link"];
$myheading = $_POST["heading"];
 
    $mybody = '<image heading="'.$myheading.'" links="'.$mylink.'"/>';
    
    $file = "article.xml";  
    $handle = fopen($file, "r");  
    
    if ($handle) {
        while (!feof($handle)) {
            $temp = fgets($handle);     
           if ($temp==$mybody)
           {
            echo "match";
            }
           else 
           { echo "no match";}            
          
    }
     fclose($handle);
    
    }
}
?>
 
<table id="structure">
    <tr>
        <td id="page">
            <h2>delete data</h2>
                    
            <form action="delete5.php" method="post">
            <table>
                <tr>
                    <td>heading</td>
                    <td><input type="text" name="heading" maxlength="23" value="<?php echo htmlentities($myheading); ?>" /></td>
                </tr>
                <tr>
                    <td>links</td>
                    <td><input type="text" name="link" maxlength="40" value="<?php echo htmlentities($mylink); ?>" /></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" name="submit" value="delete data" /></td>
                </tr>
            </table>
            </form>
        </td>
    </tr>
</table>
 
below is the xml file it checks the data in.

Code: Select all

 
<gallery>
<image heading="roses" links="roses"/>
<image heading="grapes" links="grapes"/>
</gallery>
 

Re: input data doesnt compare with the text data

Posted: Thu Jan 07, 2010 11:51 pm
by califdon
It might be due to an end-of-line character or something. The == operator is pretty strict. Why don't you use preg_match()?

Re: input data doesnt compare with the text data

Posted: Thu Jan 07, 2010 11:56 pm
by angelic_devil
cud u type the line with the pregmatch

Re: input data doesnt compare with the text data

Posted: Fri Jan 08, 2010 1:01 am
by flying_circus
trim() made it work for me.

See line 18 in the following code.

Code: Select all

<?php
  $myheading="";
  $mylink="";
  $mybody="";
 
  if(isset($_POST['submit'])) {
    $mylink = $_POST["link"];
    $myheading = $_POST["heading"];
    $mybody = '<image heading="'.$myheading.'" links="'.$mylink.'"/>';
    
    $file = "article.xml";
    $handle = fopen($file, "r");
  
    if($handle) {
      while(!feof($handle)) {
        $temp = fgets($handle);
        
        if(trim($temp) == trim($mybody)){
          echo "Match: " . htmlentities($temp) . " Does match: " . htmlentities($mybody) . "<br />";
        } else {
          echo "No Match: " . htmlentities($temp) . " Does not match: " . htmlentities($mybody) . "<br />";
        }
      }
      fclose($handle);
    }
  }
?>
There are certainly better ways to accomplish this task though. You should read up on PHP's xml functions.