/    Sign up×
Community /Pin to ProfileBookmark

Stop page execution

Hi, i have an upload script written in html and php as follows:

[code=php]
<form action=”<? $PHP_SELF ?>” method=”post” enctype=”multipart/form-data” name=”form” />
<label for=”file” > Filename: </label>
<input type=”file” name=”file” id=”file” class=”registration” />
<br />
<input type=”submit” name=”Submit” value=”Submit” onclick=”LimitAttach(this.form, this.form.file.value)” />
<p>

<?php

if (!isset($_POST[Submit])){
print “You need to select a file and click submit. “;
}
elseif ((($_FILES[“file”][“type”] == “image/jpeg”)
|| ($_FILES[“file”][“type”] == “image/pjpeg”))
&& ($_FILES[“file”][“size”] < 20000)){

if ($_FILES[“file”][“error”] > 0){
echo “Error: ” . $_FILES[“file”][“error”] . “<br />”;
}
else{

print “Upload: ” . $_FILES[“file”][“name”] . “<br />”;
print “Type: ” . $_FILES[“file”][“type”] . “<br />”;
print “Size: ” . ($_FILES[“file”][“size”] / 1024) . ” Kb<br />”;
print “Stored in: ” . $_FILES[“file”][“tmp_name”];
}

}
else{
echo “Invalid file type or size.”;
}

?>[/code]

Along with the bit of javascript on the submit button tag i also have this in the header (copied from another source…):

[CODE]<SCRIPT LANGUAGE=”JavaScript”>
<!– Original: ArjoGod, Shauna Merritt –>
<!– Modified By: Ronnie T. Moore, Editor –>

<!– This script and many more are available free online at –>
<!– The JavaScript Source!! http://javascript.internet.com –>

<!– Begin
extArray = new Array(“.jpg”, “.jpeg”);
function LimitAttach(form, file) {
allowSubmit = false;
if (!file) return;
while (file.indexOf(“\”) != -1)
file = file.slice(file.indexOf(“\”) + 1);
ext = file.slice(file.indexOf(“.”)).toLowerCase();
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) { allowSubmit = true; break; }
}
if (allowSubmit) form.submit();
else
alert(“Please only upload files that end in types: ”
+ (extArray.join(” “)) + “nPlease select a new ”
+ “file to upload and submit again.”);
}
// End –>
</script>[/CODE]

Now, the problem is if an image that does not end in .jpg is uploaded, the javascript does display the error message, but the page continues to load and return the “wrong file type error”. So is there anyway to stop the php script running if the alert dialog comes up?

Thanks for any help.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@vayumaheshApr 26.2007 — Just add return statement in the else condition.

else {

alert(" .... ");

return allowSubmit;

}
×

Success!

Help @fjords spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...