Quick question on 'return' and 'exit'
Moderator: General Moderators
-
scarface222
- Forum Contributor
- Posts: 354
- Joined: Thu Mar 26, 2009 8:16 pm
Quick question on 'return' and 'exit'
Hey guys I use return when one of my forms has an error to stop the form from submitting but when I do so the bottom half of the page past return does not display, is there a way around this?
Re: Quick question on 'return' and 'exit'
return() is used within a function to immediately end the function and optionally pass data back on its exit.
Try a different method to kill the form, perhaps putting it in an if statement that checks whether there are any errors.
Try a different method to kill the form, perhaps putting it in an if statement that checks whether there are any errors.
- markusn00b
- Forum Contributor
- Posts: 298
- Joined: Sat Oct 20, 2007 2:16 pm
- Location: York, England
Re: Quick question on 'return' and 'exit'
Can we see the code? We can then evaluate the way you're doing it and possibly offer improvements.scarface222 wrote:Hey guys I use return when one of my forms has an error to stop the form from submitting but when I do so the bottom half of the page past return does not display, is there a way around this?
-
scarface222
- Forum Contributor
- Posts: 354
- Joined: Thu Mar 26, 2009 8:16 pm
Re: Quick question on 'return' and 'exit'
lol it is an mp3 upload script for this form. The problem is, the form contains more inputs and I want to stop the whole submission process if there is an error, and have it displayed on the page, but of course I do not want the html affected. If you guys really need I will paste the whole thing, but I think this illustrates my situation. The other form inputs just have checks like
example:
Mp3 form sample
<form name="titleform" action="" method="POST" enctype="multipart/form-data">
<input type="file" name="uploadedfile" size="40" />
</form>
example:
Code: Select all
if ($max_users==0){
echo 'This field cannot be 0';
return;
}<form name="titleform" action="" method="POST" enctype="multipart/form-data">
<input type="file" name="uploadedfile" size="40" />
</form>
Code: Select all
//upload mp3
//**********************************************************************************************
echo "Please wait while we attempt to upload your file...<br><br>";
//**********************************************************************************************
$target_path = "usercontent/$user/audio/";
$flag = 0; // Safety net, if this gets to 1 at any point in the process, we don't upload.
$filename = $_FILES['uploadedfile']["name"];
$filesize = $_FILES['uploadedfile']["size"];
$mimetype = $_FILES['uploadedfile']["type"];
$filename = htmlentities($filename);
$filesize = htmlentities($filesize);
$mimetype = htmlentities($mimetype);
$target_path = $target_path . basename( $filename );
if($filename != ""){
echo "Beginning upload process for file named: ".$filename."<br>";
echo "Filesize: ".$filesize."<br>";
echo "Type: ".$mimetype."<br><br>";
}
//First generate a MD5 hash of what the new file name will be
//Force a MP3 extention on the file we are uploading
$hashedfilename = md5($filename);
$hashedfilename = $hashedfilename.".mp3";
//Check for empty file
if($filename == ""){
$error = "No File Exists!";
$flag = $flag - 4;
}
//Now we check that the file doesn't already exist.
$existname = "usercontent/$user/audio/".$hashedfilename;
if(file_exists($existname)){
if($flag == 0){
$error = "Your file already exists on the server!
Please choose another file to upload or rename the file on your
computer and try uploading it again!";
}
$flag = $flag + 1;
}
//Whitelisted files - Only allow files with MP3 extention onto server...
$whitelist = array(".mp3");
foreach ($whitelist as $ending) {
if(substr($filename, -(strlen($ending))) != $ending) {
$error = "The file type or extention you are trying to upload is not allowed!
You can only upload MP3 files to the server!";
$flag++;
}
}
//Now we check the filesize. If it is too big or too small then we reject it
//MP3 files should be at least 1MB and no more than 6.5 MB
if($filesize > 6920600){
//File is too large
if($flag == 0){
$error = "The file you are trying to upload is too large!
Your file can be up to 6.5 MB in size only.
Please upload a smaller MP3 file or encode your file with a lower bitrate.";
}
$flag = $flag + 1;
}
if($filesize < 1048600){
//File is too small
if($flag == 0){
$error = "The file you are trying to upload is too small!
Your file has been marked as suspicious because our system has
determined that it is too small to be a valid MP3 file.
Valid MP3 files must be bigger than 1 MB and smaller than 6.5 MB.";
}
$flag = $flag + 1;
}
//Check the mimetype of the file
if($mimetype != "audio/x-mp3" and $mimetype != "audio/mpeg"){
if($flag == 0){
$error = "The file you are trying to upload does not contain expected data.
Are you sure that the file is an MP3?";
}
$flag = $flag + 1;
}
//Check that the file really is an MP3 file by reading the first few characters of the file
$f = @fopen($_FILES['uploadedfile']['tmp_name'],'r');
$s = @fread($f,3);
@fclose($f);
if($s != "ID3"){
if($flag == 0){
$error = "The file you are attempting to upload does not appear to be a valid MP3 file.";
}
$flag++;
}
//All checks are done, actually move the file...
if($flag == 0){
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
//Change the filename to MD5 hash and FORCE a MP3 extention.
if(@file_exists("usercontent/$user/audio/".$filename)){
//Rename the file to an MD5 version
rename("usercontent/$user/audio/".$filename, "usercontent/$user/audio/".$hashedfilename);
echo "The file ". basename( $filename ). "
has been uploaded. Your file is <a href='uploads/$hashedfilename'>here</a>.";
}
else{
echo "There was an error uploading the file, please try again!";
}
} else{
echo "There was an error uploading the file, please try again!";
}
}
else {
echo "File Upload Failed!<br>";
if($error != ""){
echo $error;
return;
}
}
-
scarface222
- Forum Contributor
- Posts: 354
- Joined: Thu Mar 26, 2009 8:16 pm
Re: Quick question on 'return' and 'exit'
Isn't there a simple solution to this? A substitute or something of the kind for return or simple method to of accomplishing the same thing?
-
scarface222
- Forum Contributor
- Posts: 354
- Joined: Thu Mar 26, 2009 8:16 pm
Re: Quick question on 'return' and 'exit'
Is there any expert who can answer this question and save my day?
-
scarface222
- Forum Contributor
- Posts: 354
- Joined: Thu Mar 26, 2009 8:16 pm
Re: Quick question on 'return' and 'exit'
Thanks for the response but I do not quite understand. Everything is in one file. I just used return to kill the php script so it does not submit and shows the error but the bottom portion of the page gets cut off. What do you mean by returning from and to?