Use form to write to text file with validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Use form to write to text file with validation

Post 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)) 

?>
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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....
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post 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?
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

unless I can't read (Entirely possible)
you didn't close this if statement..
if(!(isset($_POST[images])) {
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Proper indentation goes a long way...
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post 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.
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post 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)) 
		}	
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post 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).
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post 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)) { 

} 
?>
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post 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?
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

It works

Post 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)) { 

} 
?>
Post Reply