Page 1 of 1

Use form to write to text file with validation

Posted: Fri Nov 18, 2005 3:57 am
by mesz
twigletmac | Please use

Code: Select all

tags when posting PHP code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Hi, I'm having a total mental block, I need the code below to fail if the 'images' field of my form is not filed in, but under all other circumstances write to my text file.
At the moment, the error displays under all circumstances and nothing writes to the file.
I'm sure it's a simple fix....
anyone

Code: Select all

<? 
$done = true; 
$fp = fopen('news.txt','a'); 
if (!$fp) { 
echo "Sorry could'n open file!<br>"; 
} 
else { 
    if(!(isset($_POST[Images]) && $_POST[Images] != "")) { 
    print("please add image link.\n\n"); 
    $done= false; 
  } 
 else { 
      
    echo "go back and add image"; 
      
  } 
    
if($done == true) { 


        $fp = fopen('news.txt','a'); 
        $line = date("d.m.y") . "¬" . $HTTP_POST_VARS['name'] . "¬".$HTTP_POST_VARS["email"] . "¬".trim 

($HTTP_POST_VARS["subject"]). "¬".$HTTP_POST_VARS["images"]. "¬".$HTTP_POST_VARS["url"]; 
        $line .= "¬" . $HTTP_POST_VARS['news']; 
        $line = str_replace("\r\n","<BR></BR>",$line); 
        $line = str_replace(" < "," LESS THAN ",$line); 
        $line = str_replace(" > "," GREATER THAN ",$line); 
        $line = str_replace(" >= "," GREATER THAN OR EQUAL TO ",$line); 
        $line = str_replace(" <= "," LESS THAN OR EQUAL TO ",$line); 
        $line = str_replace(" & "," and ",$line); 
        $line = str_replace("&","and",$line); 
        $line = stripslashes($line); 
        $line .= "\r\n"; 
        fwrite($fp, $line); 

} 




} 
        if(!fclose($fp)) 

?>

Posted: Fri Nov 18, 2005 4:33 am
by n00b Saibot
Use PHP Tags for the god's sake ... and forum's

> use $_POST instead of deprecated HTTP_POST_VARS
> always quote your indices
> Images don't come in $_POST array, they reside in $_FILES array

hope that starts you off....

Posted: Fri Nov 18, 2005 8:16 am
by BDKR
First off, I think the below if statement is impossible.

Code: Select all

else {
    if(!(isset($_POST[Images]) && $_POST[Images] != "")) {
    print("please add image link.\n\n");
    $done= false;
  }
In other words that if is asking "If it's not set AND it's not empty or blank? An "||" would be better there.

On to other things, why are you trying to open the file twice? try removing that second fopen() and see what happens.

You may also want to turn the error reporting all the way up while testing. Place error_reporting(E_ALL) at the top while testing.

Cheers,
BDKR

Posted: Fri Nov 18, 2005 9:20 am
by mesz
Cheers guys...I've followed your suggestions and come up with -

Code: Select all

<? 


if (!$fp) { 
echo "Sorry could'n open file!<br>"; 
} 


else { 
    if(!(isset($_POST[images])) { 
    print("please add image link.\n\n"); 

  } 










else { 
       
    echo "go back and add image"; 
       
  } 
     
else { 

$fp = fopen('news.txt','a'); 

        $line = date("d.m.y") . "¬" . $HTTP_POST_VARS['name'] . "¬".$HTTP_POST_VARS["email"] . "¬".trim 

($HTTP_POST_VARS["subject"]). "¬".$HTTP_POST_VARS["images"]. "¬".$HTTP_POST_VARS["url"]; 
        $line .= "¬" . $HTTP_POST_VARS['news']; 
        $line = str_replace("\r\n","<BR></BR>",$line); 
        $line = str_replace(" < "," LESS THAN ",$line); 
        $line = str_replace(" > "," GREATER THAN ",$line); 
        $line = str_replace(" >= "," GREATER THAN OR EQUAL TO ",$line); 
        $line = str_replace(" <= "," LESS THAN OR EQUAL TO ",$line); 
        $line = str_replace(" & "," and ",$line); 
        $line = str_replace("&","and",$line); 
        $line = stripslashes($line); 
        $line .= "\r\n"; 
        fwrite($fp, $line); 

} 




} 
        if(!fclose($fp)) 

?>
However, now I've messed it up worse than before!

I get the error -
Parse error: syntax error, unexpected '{' in C:\Source\phpCHAWK\action.php on line 10
I don't see why...I've only taken the second parameter check out of the validation statement.
Any ideas?

Posted: Fri Nov 18, 2005 11:32 am
by Charles256
unless I can't read (Entirely possible)
you didn't close this if statement..
if(!(isset($_POST[images])) {

Posted: Fri Nov 18, 2005 1:52 pm
by John Cartwright
Proper indentation goes a long way...

Posted: Mon Nov 21, 2005 2:55 am
by mesz
cheers for the help...
and yeah, I know, I never indent properly.
It's a hang up of being a script kid instead of originally coming from a programming background.
But, excuses aside, I know it's slack.
I'll let you know how I get on.

Posted: Mon Nov 21, 2005 6:06 am
by mesz
OK, I think I am never going to solve this!
This easiest of things has just turned into a horrible frenzy of deleting and adduing curly braces to close 'if' statements.
I now have a total and utter mess of a script.
Can someone please show me what it is supposed to look like...

Code: Select all

<? 
if (!$fp) { 
	echo "Sorry could'n open file!<br>"; 
		}{	
		if(!(isset($_POST[images])) {
   	 	print("please add image link.\n\n"); 
							} 	{
      							else { 
									$fp = fopen('news.txt','a'); 
									$line = date("d.m.y") . "¬" . $HTTP_POST_VARS['name'] . "¬".$HTTP_POST_VARS["email"] . "¬".trim ($HTTP_POST_VARS["subject"]). "¬".$HTTP_POST_VARS["images"]. "¬".$HTTP_POST_VARS["url"]; 
        							$line .= "¬" . $HTTP_POST_VARS['news']; 
        							$line = str_replace("\r\n","<BR></BR>",$line); 
        							$line = str_replace(" < "," LESS THAN ",$line); 
        							$line = str_replace(" > "," GREATER THAN ",$line); 
        							$line = str_replace(" >= "," GREATER THAN OR EQUAL TO ",$line); 
        							$line = str_replace(" <= "," LESS THAN OR EQUAL TO ",$line); 
        							$line = str_replace(" & "," and ",$line); 
        							$line = str_replace("&","and",$line); 
        							$line = stripslashes($line); 
        							$line .= "\r\n"; 
        							fwrite($fp, $line); 
								}{
				if(!fclose($fp)) 
		}	
?>

Posted: Mon Nov 21, 2005 7:05 am
by twigletmac
mesz wrote:OK, I think I am never going to solve this!
This easiest of things has just turned into a horrible frenzy of deleting and adduing curly braces to close 'if' statements.
I now have a total and utter mess of a script.
Can someone please show me what it is supposed to look like...
Well, if you take it back to the beginning and add some indenting, ensure all your array elements are quoted, change $HTTP_POST_VARS to $_POST, remove an extraneous else statement and ensure braces are all in the right place you get:

Code: Select all

<?php

if (!$fp) {
	echo "Sorry couldn't open file!<br>";
} else {
	if (!isset($_POST['images'])) {
		echo "please add image link.\n\n";

	} else {
		$fp = fopen('news.txt','a');

		$line = date("d.m.y") . "¬" . $_POST['name'] . "¬".$_POST["email"] . "¬".trim($_POST["subject"]). "¬".$_POST["images"]. "¬".$_POST["url"];

		$line .= "¬" . $_POST['news'];

		$line = str_replace("\r\n","<BR></BR>",$line);
		$line = str_replace(" < "," LESS THAN ",$line);
		$line = str_replace(" > "," GREATER THAN ",$line);
		$line = str_replace(" >= "," GREATER THAN OR EQUAL TO ",$line);
		$line = str_replace(" <= "," LESS THAN OR EQUAL TO ",$line);
		$line = str_replace(" & "," and ",$line);
		$line = str_replace("&","and",$line);
		$line = stripslashes($line);

		$line .= "\r\n";
		
		fwrite($fp, $line);
	}
}

if(!fclose($fp)) {

}
?>
Which won't make the script work as expected probably, but at least makes it easier to read...

Mac

Posted: Mon Nov 21, 2005 7:18 am
by mesz
cheers twig...
step forward..
now I get this error....
Sorry couldn't open file!
Warning: fclose(): supplied argument is not a valid stream resource in C:\Source\phpCHAWK\action.php on line 31

Posted: Mon Nov 21, 2005 7:23 am
by twigletmac
Well, if $fp isn't a file pointer then there's nothing for fclose() to close - so you probably want to move the fclose() statement to somewhere where you know there's something to close - i.e. change:

Code: Select all

fwrite($fp, $line);
    }
}

if(!fclose($fp)) {

}
to

Code: Select all

fwrite($fp, $line);
	}

	if(!fclose($fp)) {

	}
}
Mac

Posted: Mon Nov 21, 2005 7:25 am
by mesz
Thanks for all this help friend.
Much appreciated.
(This was only a minor mod I wanted to make to the script and it's become an all consuming obsession...I know it shouldn't have been this difficult).

Posted: Mon Nov 21, 2005 8:20 am
by mesz
To everyone who has helped so far...thank you.
This actually has just got a little more interesting and so you may want to help solve an actual 'php' problem rather than just my syntax being rubbish.
OK, if I declare the variable

Code: Select all

$fp
before the else/if clauses begin, everything writes to file, no matter whether the criteria of the else/if clauses are met or not.
Whereas if I decalre the variable

Code: Select all

$fp
in the else statement, the script fails everytime with this message -
Sorry couldn't open file!
Warning: fclose(): supplied argument is not a valid stream resource in C:\Source\phpCHAWK\action.php on line 33
Version 1 - where everything writes to file

Code: Select all

<?php 
$fp = fopen('news.txt','a'); 
if (!$fp) { 
    echo "Sorry couldn't open file!<br>"; 
} else { 
    if (!isset($_POST['images'])) { 
        echo "please add image link.\n\n"; 

    } else { 

        $line = date("d.m.y") . "¬" . $_POST['name'] . "¬".$_POST["email"] . "¬".trim($_POST["subject"]). "¬".$_POST["images"]. "¬".$_POST["url"]; 

        $line .= "¬" . $_POST['news']; 

        $line = str_replace("\r\n","<BR></BR>",$line); 
        $line = str_replace(" < "," LESS THAN ",$line); 
        $line = str_replace(" > "," GREATER THAN ",$line); 
        $line = str_replace(" >= "," GREATER THAN OR EQUAL TO ",$line); 
        $line = str_replace(" <= "," LESS THAN OR EQUAL TO ",$line); 
        $line = str_replace(" & "," and ",$line); 
        $line = str_replace("&","and",$line); 
        $line = stripslashes($line); 

        $line .= "\r\n"; 
         
        fwrite($fp, $line); 
    } 
} 

if(!fclose($fp)) { 

} 
?>
Version 2 - where nothing writes to file

Code: Select all

<?php 

if (!$fp) { 
    echo "Sorry couldn't open file!<br>"; 
} else { 
    if (!isset($_POST['images'])) { 
        echo "please add image link.\n\n"; 

    } else { 
       $fp = fopen('news.txt','a'); 
        $line = date("d.m.y") . "¬" . $_POST['name'] . "¬".$_POST["email"] . "¬".trim($_POST["subject"]). "¬".$_POST["images"]. "¬".$_POST["url"]; 

        $line .= "¬" . $_POST['news']; 

        $line = str_replace("\r\n","<BR></BR>",$line); 
        $line = str_replace(" < "," LESS THAN ",$line); 
        $line = str_replace(" > "," GREATER THAN ",$line); 
        $line = str_replace(" >= "," GREATER THAN OR EQUAL TO ",$line); 
        $line = str_replace(" <= "," LESS THAN OR EQUAL TO ",$line); 
        $line = str_replace(" & "," and ",$line); 
        $line = str_replace("&","and",$line); 
        $line = stripslashes($line); 

        $line .= "\r\n"; 
         
        fwrite($fp, $line); 
    } 
} 

if(!fclose($fp)) { 

} 
?>

Posted: Thu Nov 24, 2005 10:42 am
by mesz
can anybody suggest what I need to do to get the script to work.
I've been hacking away and I'm no clearer?
anyone?

It works

Posted: Fri Nov 25, 2005 2:31 am
by mesz
It's OK, sorted it.
You wouldn't believe how stupid the mistake was...
On the form itself, I had the field 'images' against the wrong text field, so when I was testing this I was testing it incorrectly.

Thanks for all your help everyone.

The working upload script.

Code: Select all

<?php 
$fp = fopen('news.txt','a'); 

if ($fp=="") { 
    echo "Sorry couldn't open file!<br>"; 
} else { 
//print_r($_POST);
    if ($_POST['images']=="") { 
        echo "please add image link.\n\n"; 

    } else { 

        $line = date("d.m.y") . "¬" . $_POST['name'] . "¬".$_POST["email"] . "¬".trim($_POST["subject"]). 

"¬".$_POST["images"]. "¬".$_POST["url"]; 

        $line .= "¬" . $_POST['news']; 

        $line = str_replace("\r\n","<BR></BR>",$line); 
        $line = str_replace(" < "," LESS THAN ",$line); 
        $line = str_replace(" > "," GREATER THAN ",$line); 
        $line = str_replace(" >= "," GREATER THAN OR EQUAL TO ",$line); 
        $line = str_replace(" <= "," LESS THAN OR EQUAL TO ",$line); 
        $line = str_replace(" & "," and ",$line); 
        $line = str_replace("&","and",$line); 
        $line = stripslashes($line); 

        $line .= "\r\n"; 
          
        fwrite($fp, $line); 
    } 
} 

if(!fclose($fp)) { 

} 
?>