Jump to content

Problem with a script after a form submit.


trink

Recommended Posts

Alright, what I've got is this:

<?php
$postx=$_POST[postx];
$msg=$_POST[msg];
$name=$_POST[name];
$isimg='';
$filename='posts.txt';
if($postx){
if(trim($name)){
	if(trim($msg)){
		if($_FILES['imgup'] ['tmp_name']){
			move_uploaded_file($_FILES['imgup'] ['tmp_name'], "../img/{$_FILES['imgup'] ['name']}");
			$isimg='<img class="leftfloat" src="'.$_FILES['imgup'] ['name'].'" />';
		}
		fwrite($fp=fopen($filename, "a"), '<div class="ptitle"><center><b>'.$name.'</b></center></div><div class="post">'.$isimg.$msg.'</div><br>') && fclose($fp);
	}else{
		if($_FILES['imgup'] ['tmp_name']){
			move_uploaded_file($_FILES['imgup'] ['tmp_name'], "../img/{$_FILES['imgup'] ['name']}");
			$isimg='<img class="leftfloat" src="'.$_FILES['imgup'] ['name'].'" />';
			fwrite($fp=fopen($filename, "a"), '<div class="ptitle"><center><b>'.$name.'</b></center></div><div class="post">'.$isimg.$msg.'</div><br>') && fclose($fp);
		}else{
			$message='You must have a message or a file in order to post.';
		}
}if(!trim($name)){
	$message='You must use a name in order to post.';
}
}
$handle=fopen("posts.txt", "r");
$posts=fread($handle, filesize("posts.txt"));
fclose($handle);
?>

Basically what it's supposed to do is:

If they click the submit button, its supposed to check if they have a name, if they don't, it sets $message as 'you must have a name blah blah'.

If they have a name, then it checks if they have a message, if they do have a message then it checks if they have a file, and if they do, it uploads it and sets a variable as a link to it, then writes all the stuff to an external file.  If they do not have a message, but they do have an image, then it uploads the image and writes it all to the file.  If they do not have a message or an image, then it sets $message as 'you must have a message blah blah'.

The problem is, when I open the page, it says that there was an "unexpected $end" found on the last line of the file.  OR if I change that }if(!trim($name)){ line to }else{ it says that there is an "unexpected else" on that line. I've looked over this file at least 20 times and found nothing that could be causing the problem, any help would be appreciated.

 

You can find the full document at http://71.97.138.27:1009/doesntwork.txt  If the IP changes for some reason, I'll post the change.

 

Any help would be greatly appreciated.

You're missing a closing bracket to either the 1st or 2nd if statement depending on your intentions.

In your editor, write the code so that the brackets line up and you 'll see what I mean:

 

IF(blah)

{

    some stuff here

}

else

{

    some more stuff here

    if

    {

          more stuff here

    }

    else

    {

          more stuff here

      }

}

As a side note, as I've been learning, I've found that it keeps the code cleaner and easier to debug if you use

elseif

{

      blah blah

}

 

if an else statement is immediately followed by an if statement.  You eliminate a set of brackets to track that way.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.