Jump to content

Form with drop down menu


imrie

Recommended Posts

Hello,PHP, HTML, MySQLI need some help again with an upload form (v sorry). I would like to have at the top a textarea with browse box. Then below a Title to give the file. Then another bar to type Authour in. Then below that i need a drop down bar with options,( just say A, B , C). And finally a upload button. heres the hard bit:Once upload is pressed i need the file to be sent to a directory depending on what was chosen in the drop down bar. (A, B , C - the chosen categories) and all of the information to be inserted into a row in a table of a database. (either table A, B, C)Hope this makes sense,Thanks.

Link to comment
Share on other sites

the form

<form action="script.php" method="post">   File: <input type="file" name="file" />   Title: <input type="text" name="title" />   Author: <input type="text" name="author" />   Option: <select name="options">		<option value="a">A</option>		<option value="b">B</option>		<option value="c">C</option>   </select>   <input type="submit" name="Submit"></form>

script.php

<?phpif ($_POST['option'] == 'a') {   move_uploaded_file($_FILES["file"]["tmp_name"],	  "/*DIRECTORY FOR OPTION A*/" . $_FILES["file"]["name"]);}if ($_POST['option'] == 'b') {   move_uploaded_file($_FILES["file"]["tmp_name"],	  "/*DIRECTORY FOR OPTION B*/" . $_FILES["file"]["name"]);}if ($_POST['option'] == 'c') {   move_uploaded_file($_FILES["file"]["tmp_name"],	  "/*DIRECTORY FOR OPTION C*/" . $_FILES["file"]["name"]);}/*whatever method you use to connect to the sql database*/$query = "INSERT INTO `" . $_POST['option'] . "` (title, author, file_name) VALUES ('$_POST[title]', '$_POST[author]', '$_FILES[file][name]')";mysql_query($query);

something like that, its not tested, ask if you have any questions

Link to comment
Share on other sites

Fanstastic! thanks!, one more thing can i add another field to the form for a image to go with the file.Is this correct:Form:

<form action="script.php" method="post">   File: <input type="file" name="file" />   image: <input type= "file" name="file" />   Title: <input type="text" name="title" />   Author: <input type="text" name="author" />   Option: <select name="options">		<option value="a">A</option>		<option value="b">B</option>		<option value="c">C</option>   </select>   <input type="submit" name="Submit"></form>

script.php:Now this bit i dont know, its pratically the same as you did with the file but with a 2nd file minus the dropdown bar.Thanks again,

Link to comment
Share on other sites

<?phpif ($_POST['option'] == 'a') {   move_uploaded_file($_FILES["file"]["tmp_name"],	  "/*DIRECTORY FOR OPTION A*/" . $_FILES["file"]["name"]);}if ($_POST['option'] == 'b') {   move_uploaded_file($_FILES["file"]["tmp_name"],	  "/*DIRECTORY FOR OPTION B*/" . $_FILES["file"]["name"]);}if ($_POST['option'] == 'c') {   move_uploaded_file($_FILES["file"]["tmp_name"],	  "/*DIRECTORY FOR OPTION C*/" . $_FILES["file"]["name"]);}//upload imagemove_uploaded_file($_FILES["file"]["tmp_name"],	  "/*DIRECTORY FOR IMAGE*/" . $_FILES["file"]["name"]);/*whatever method you use to connect to the sql database*/$query = "INSERT INTO `" . $_POST['option'] . "` (title, author, file_name,image) VALUES ('$_POST[title]', '$_POST[author]', '$_FILES[file][name]','$_FILES[file][name]')";mysql_query($query);

Something like that and you will need to change the name of the image field it cannot be the same as the other file.

Link to comment
Share on other sites

Thank you. Can somebody please add into the script.php the following:For the image MAXFILESIZE: 500kb, formats: .jpg,.gif,.bmp.For the File MAXFILESIZE: 1.5mb, formats: .zip, .raralso i have modified it slightly is this correct:

<?phpif ($_POST['option'] == 'a') {   move_uploaded_file($_FILES["file"]["tmp_name"],	  "/*DIRECTORY FOR OPTION A*/" . $_FILES["file"]["name"]);}if ($_POST['option'] == 'b') {   move_uploaded_file($_FILES["file"]["tmp_name"],	  "/*DIRECTORY FOR OPTION B*/" . $_FILES["file"]["name"]);}if ($_POST['option'] == 'c') {   move_uploaded_file($_FILES["file"]["tmp_name"],	  "/*DIRECTORY FOR OPTION C*/" . $_FILES["file"]["name"]);}if (file_exists("PATH" . $_FILES["file"]["name"]))	  {	  echo $_FILES["file"]["name"] . " already exists. ";	  }     //upload imagemove_uploaded_file($_FILES["file"]["tmp_name"],	  "/*DIRECTORY FOR IMAGE*/" . $_FILES["file"]["name"]);if (file_exists("PATH" . $_FILES["file"]["name"]))	  {	  echo $_FILES["file"]["name"] . " already exists. ";	  }	/*whatever method you use to connect to the sql database*/$query = "INSERT INTO `" . $_POST['option'] . "` (title, author, file_name,image) VALUES ('$_POST[title]', '$_POST[author]', '$_FILES[file][name]','$_FILES[file][name]')";mysql_query($query);

Thanks,

Link to comment
Share on other sites

To make it easier for you in the directory selection, you could change the dropdown code to look like this:

<?php    $success = true;    $type = $_FILES['file']['type'];        if ($type == "image/gif" || $type == "image/jpeg" || $type == "image/bmp") {        //upload image        if ($_FILES['file']['size'] > 500000) {            echo "Error: The image is too large!";        } else {            move_uploaded_file($_FILES["file"]["tmp_name"],"/*DIRECTORY FOR IMAGE*/" . $_FILES["file"]["name"]);            if (file_exists("PATH" . $_FILES["file"]["name"])) {                echo $_FILES["file"]["name"] . " already exists. ";            }        }    } else if ($type == "application/rar" || $type == "application/zip") {        //Upload file        if ($_FILES['file']['size'] > 1500000) {            echo "Error: The file is too large!";        } else {            move_uploaded_file($_FILES["file"]["tmp_name"],$_POST['options'] . $_FILES["file"]["name"]);            if (file_exists("PATH" . $_FILES["file"]["name"]))                {                echo $_FILES["file"]["name"] . " already exists. ";                              }        }    } else {        //Invalid file type        $success = false;        echo "Error: File type $type not valid";    }        if ($success) {        /*whatever method you use to connect to the sql database*/        $query = "INSERT INTO `" . $_POST['option'] . "` (title, author, file_name,image) VALUES ('$_POST[title]', '$_POST[author]', '$_FILES[file][name]','$_FILES[file][name]')";        mysql_query($query);    }?>

Link to comment
Share on other sites

Ok, thanks you. I take it dont need this part any more "

if ($_POST['option'] == 'c') {

"Also is this the correct query to create the tables:

CREATE TABLE table_A (  Name varchar(25) NOT NULL default,  Image_file varchar(25) NOT NULL default '',  File varchar(25) NOT NULL default '',  Authour varchar(25) NOT NULL default '',  PRIMARY KEY  (File),  UNIQUE KEY File (File)) TYPE=MyISAM COMMENT='Uploader';CREATE TABLE table_B (  Name varchar(25) NOT NULL default,  Image_file varchar(25) NOT NULL default '',  File varchar(25) NOT NULL default '',  Authour varchar(25) NOT NULL default '',  PRIMARY KEY  (File),  UNIQUE KEY File (File)) TYPE=MyISAM COMMENT='Uploader';CREATE TABLE table_C (  Name varchar(25) NOT NULL default,  Image_file varchar(25) NOT NULL default '',  File varchar(25) NOT NULL default '',  Authour varchar(25) NOT NULL default '',  PRIMARY KEY  (File),  UNIQUE KEY File (File)) TYPE=MyISAM COMMENT='Uploader';

One last thing, how would i go about creating a system where instead of using the file name i could use, "/uploader?id=1"Thanks again for your help everyone. Much appreciated.

Link to comment
Share on other sites

Hello,About the id thing. I found a tutorial:hereIf you give it a read (its not long) i am thinking of using this as a click counter however, how would iu modify the upload script to change the URL to: www.gtld.com/uploads/click.php3?url=/12545.zipid=2 from www.gtld.com/uploads/12545.zip?Thanks

Link to comment
Share on other sites

Synook, your code doesent work.heres the code:

<?php	$success = true;	$type = $_FILES['file']['type']; 	   if ($type == "image/gif" || $type == "image/jpeg" || $type == "image/bmp") {		//upload image		if ($_FILES['file']['size'] > 500000) {			echo "Error: The image is too large!";		} else {			move_uploaded_file($_FILES["file"]["tmp_name"],"/image_uploads/" . $_FILES["file"]["name"]);			if (file_exists("PATH" . $_FILES["file"]["name"])) {				echo $_FILES["file"]["name"] . " already exists. ";			}		}	} else if ($type == "application/rar" || $type == "application/zip") {		//Upload file		if ($_FILES['file']['size'] > 1500000) {			echo "Error: The file is too large!";		} else {			move_uploaded_file($_FILES["file"]["tmp_name"],"/uploads/" . $_FILES["file"]["name"]);			if (file_exists("PATH" . $_FILES["file"]["name"]))				{				echo $_FILES["file"]["name"] . " already exists. ";							  }		}	} else {		//Invalid file type		$success = false;		echo "Error: File type $type not valid";	} 	   if ($success) {		mysql_connect("","", "");mysql_select_db("");$query = "INSERT INTO `" . $_POST['option'] . "` (title, author, file_name,image) VALUES ('$_POST[title]', '$_POST[author]', '$_FILES[file][name]','$_FILES[file][name]')";		mysql_query($query);	}?>

Returns thisNotice: Undefined index: file in /PATH/script.php on line 3Error: File type not valideven though it is a bmp and a zip

Link to comment
Share on other sites

Uh! hmm... did you submit a form which linked to that page? Did you have a <input type="file" name="file"... field on that form?

Link to comment
Share on other sites

Did you set the encoding type to "multipart/form-data", and a method of post? The error means that there was no file-reference with the name "file" submitted.. weird :) Try also writing print_r($_FILES); on the first line after the <?php and posting what it says.

Link to comment
Share on other sites

OK i get this error:Array ( [pictures] => Array ( [name] => 7920.zip [type] => application/x-zip-compressed [tmp_name] => /tmp/phpDbZm9D [error] => 0 => 60351 ) ) Notice: Undefined index: file in /home/fhlinux164/m/mysimtractor.com/user/htdocs/form3.php on line 5Error: File type not valid The form has been made from varius sources.here is script (form3.php):

<?php   print_r($_FILES); if($_SERVER['REQUEST_METHOD'] == "POST"){ $success = true;	$type = $_FILES['file']['type']; 	   if ($type == "image/gif" || $type == "image/jpeg" || $type == "image/bmp") {		//upload image		if ($_FILES['file']['size'] > 500000) {			echo "Error: The image is too large!";		} else {			move_uploaded_file($_FILES["file"]["tmp_name"],"/image_uploads/" . $_FILES["file"]["name"]);			if (file_exists("PATH" . $_FILES["file"]["name"])) {				echo $_FILES["file"]["name"] . " already exists. ";			}		}	} else if ($type == "application/rar" || $type == "application/zip") {		//Upload file		if ($_FILES['file']['size'] > 1500000) {			echo "Error: The file is too large!";		} else {			move_uploaded_file($_FILES["file"]["tmp_name"],"/uploads/" . $_FILES["file"]["name"]);			if (file_exists("PATH" . $_FILES["file"]["name"]))				{				echo $_FILES["file"]["name"] . " already exists. ";							  }		}	} else {		//Invalid file type		$success = false;		echo "Error: File type $type not valid";	} 	   if ($success) {		mysql_connect("","imrieimag", "");mysql_select_db("imrieimag");$query = "INSERT INTO `" . $_POST['option'] . "` (title, author, file_name,image) VALUES ('$_POST[title]', '$_POST[author]', '$_FILES[file][name]','$_FILES[file][name]')";		mysql_query($query);	}}?>

The form:

html><head><title>HTML Form for uploading image to server</title></head><body><form action="form3.php" method="post" enctype="multipart/form-data"><p>Skin File:<input type="file" name="pictures" /> <br>Image:<input type="file" name="pictures" />  <br>Title:<input type="text" name="Title" /><br>Author:<input type="text" name="Author" /><br>Category: <select name="options">		<option value="CLAAS_Cougar ">CLAAS_Cougar </option>		<option value="CLAAS_Disco ">CLAAS_Disco </option>		<option value="CLAAS_Round Baler ">CLAAS_Round Baler </option>		<option value="CLAAS_Xerion">CLAAS_Xerion</option>		<option value="Delvano_Sprayer">Delvano_Sprayer</option>		<option value="Disc_Harrow">Disc_Harrow</option>		<option value="Forage_Harvester ">Forage_Harvester </option>		<option value="Grain_Cart">Grain_Cart</option		<option value="Grain_Trailer">Grain_Trailer</option>		<option value="Header_Trailer ">Header_Trailer </option>		<option value="John_Deere_7810">John_Deere_7810</option>		<option value="John_Deere_7920 ">John_Deere_7920 </option>		<option value="John_Deere_8310">John_Deere_8310</option>		<option value="John_Deere_8310t">John_Deere_8310 T </option>		<option value="John_Deere_Corn_Header">John_Deere_Corn_Header </option>		<option value="John_Deere_Corn_Seeder">John_Deere_Corn_Seeder</option>		<option value="John_Deere_Front_Loader">John_Deere_Front_Loader </option>		<option value="John_Deere_Machine_40">John_Deere_Machine_40 </option>		<option value="John_Deere_Seeder">John_Deere_Seeder </option>		<option value="John_Deere_Square_Baler">John_Deere_Square_Baler </option>		<option value="John_Deere_STS">John_Deere_STS </option>		<option value="John_Deere_Telehandler">John_Deere_Telehandler </option>		<option value="John_Deere_Tiller">John_Deere_Tiller </option>		<option value="Lexion">Lexion</option>		<option value="Livestock_Trialer">Livestock_Trialer</option>		<option value="Manure_spreader">Manure_spreader </option>		<option value="Manure_Tanker">Manure_Tanker</option>		<option value="Miscellaneous">Miscellaneous</option>		<option value="New_Holland_NCR">New_Holland_NCR</option>		<option value="New_Holland_TG">New_Holland_TG </option>		<option value="Plough">Plough</option>		<option value="Truck">Truck</option>		   </select><input type="submit" value="Send" /></p></form></body></html>

Link to comment
Share on other sites

My script is failing because you seem to have changed, in your form, the name of your file upload field from "file" to "pictures" (<input type="file" name="pictures" />). Either rename it back to "file" (<input type="file" name="file" />) or change all instances of $_FILES['file'] to $_FILES['pictures'] (and $_FILES[file] to $_FILES[picture])...Remember, the first level of the $_FILES superglobal takes on the name of the file input field as its index...

Link to comment
Share on other sites

Ok, i tried and it sid this: Array ( [file] => Array ( [name] => maniscopic.zip [type] => application/x-zip-compressed [tmp_name] => /tmp/php8NsPOr [error] => 0 => 128647 ) ) Error: File type application/x-zip-compressed not valid Then i thought it could be to do with the fields being the opposite way round so i put the zip in image and vice versa and got this:Array ( [file] => Array ( [name] => baller.JPG [type] => image/pjpeg [tmp_name] => /tmp/phpeDtCQ9 [error] => 0 => 5735 ) ) Error: File type image/pjpeg not valid

Link to comment
Share on other sites

Umm no don't worry the script is actually working correctly now :) you can remove the print_r() statement. To fix your problem, simply change

} else if ($type == "application/rar" || $type == "application/zip") {

to

} else if ($type == "application/rar" || $type == "application/zip" || $type == "application/x-zip-compressed") {

to include the file type. :)Oh and change

if ($type == "image/gif" || $type == "image/jpeg" || $type == "image/bmp") {

to

if ($type == "image/gif" || $type == "image/jpeg" || $type == "image/bmp" || $type == "image/pjpeg") {

Also, keep on testing with all the file types you wnat to allow and update the if statements :)

Link to comment
Share on other sites

Whoa! Warning: move_uploaded_file(/uploads/John Deere 6900.zip) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/fhlinux164/m/mysimtractor.com/user/htdocs/form3.php on line 23Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpBKeBks' to '/uploads/John Deere 6900.zip' in /home/fhlinux164/m/mysimtractor.com/user/htdocs/form3.php on line 23Notice: Undefined index: option in /home/fhlinux164/m/mysimtractor.com/user/htdocs/form3.php on line 40Notice: Undefined index: title in /home/fhlinux164/m/mysimtractor.com/user/htdocs/form3.php on line 40Notice: Undefined index: author in /home/fhlinux164/m/mysimtractor.com/user/htdocs/form3.php on line 40

Link to comment
Share on other sites

Ok, for the first and second errors, are you sure your server supports spaces in filenames?For the other errors, change the insert query (right at the bottom of form3.php) to

$query = "INSERT INTO `" . $_POST['options'] . "` (title, author, file_name,image) VALUES ('$_POST[Title]', '$_POST[Author]', '$_FILES[file][name]','$_FILES[file][name]')";

You must have renamed the form input names after you posted your original code ages ago.Edit: why do people keep posting as I type :) - no, the 'title' and 'author' are already defined through the post data, just capitalise them as I said above.

Link to comment
Share on other sites

Ok still get this:Warning: move_uploaded_file(/uploads/aw14.zip) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/fhlinux164/m/mysimtractor.com/user/htdocs/form3.php on line 29Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpix4CWV' to '/uploads/aw14.zip' in /home/fhlinux164/m/mysimtractor.com/user/htdocs/form3.php on line 29there must be an error.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...