Forum Moderators: coopster
i need to sort out a quick solution to a problem.
i am trying to help a friend organise an online catalogue of her spice girls collection... lol
i need to make a form submit a name being entered into different databases and tables at the same time.
i have set up the database with 6 tables each with 10 fields.
the name will be the name of the image file but will fall into more than i catagory.
the form i need would use checkboxes to say it needs to be in such a catagory etc
i then need to get that to my database which i cant seem to work out.
i could do it by just using normal input fields but that would mean typing in the same name in each catagory it needs entering in.
this is the form.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
body {
background-color: #CCCCCC;
}
.style1 {
color: #000033;
font-size: 18px;
font-weight: bold;
}
-->
</style></head>
<body>
<div align="center">
<p class="style1">input files for me to add to your site.</p>
<p> </p>
<p>first choose a name for the image file you want to add to the page</p>
<form id="form2" name="form2" method="post" action="#*$!#*$!x.php">
<label>
item name: <input name="name" type="text" />
</label>
</form>
<p> </p>
<p> </p>
</p>
<p>next choose which sections you would like the image in</p>
<table width="150" border="0">
<tr>
<td><label>
<input type="checkbox" name="spice girls" value="$name" />
SPICE GIRLS </label></td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="geri" value="$name" />
GERI</label></td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="victoria" value="$name" />
VICTORIA </label></td>
</tr>
<tr>
<td><input type="checkbox" name="emma" value="$name" />
EMMA </td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="mel b" value="$name" />
MEL B</label></td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="mel c" value="$name" />
MEL C</label></td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<p>
<label> </label>
</p>
<p>NOW CHOOSE WHICH SUB CATAGORIES YOU WOULD LIKE THEM IN</p>
<form id="form1" name="form1" method="post" action="#*$!#*$!x.php">
<label></label>
<BR />
<label></label>
<BR />
<label></label>
<table width="150" border="0">
<tr>
<td><label>
<input type="checkbox" name="cd" value="checkbox" />
CD'S</label></td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="dvd" value="checkbox" />
DVD'S</label>
</td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="video" value="checkbox" />
VIDEO</label>
</td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="awards" value="checkbox" />
AWARDS</label></td>
</tr>
<tr>
<td><input type="checkbox" name="memorabilia" value="checkbox" />
MEMORABILIA</td>
</tr>
<tr>
<td><input type="checkbox" name="clothing" value="checkbox" />
CLOTHING</td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="magazines" value="checkbox" />
MAGAZINES</label>
</td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="jewewllery" value="checkbox" />
JEWELLERY</label></td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="autographed" value="checkbox" />
AUTOGRAPHED</label></td>
</tr>
</table>
<p><BR />
NEXT ENTER AS MUCH INFORMATION AS YOU CAN ABOUT THE OBJECT</p>
<p>
<label>COMMENTS
<textarea name="textarea" cols="80" rows="30"></textarea>
</label>
<BR />
</p>
<label></label>
<BR />
<label></label>
<p>WHEN U HAVE FINISHED CLICK SUBMIT BUTTON BELOW</p>
<p>
<label>SUBMIT
<input type="submit" name="Submit" value="Submit" />
</label>
<BR />
</p>
</form>
</div>
</body>
</html>
this is just a quick page for the form.
that gives you an idea of the information i need to submit and sort.
the php page it connects to i have connected to database etc etc but thats as far as i have got
anyone offer a quick solution so that i can get her started?
1) you should not name your fields with more than one word, name your "spice girls" checkbox. name this to something like "spiceGirls, or "spice_girls". it will be much easier to retrieve later on if you have a one word name. i think php will rename the "spice girls" to something else automatically on submit. you'l have to print_r($_POST) to get the real name.
2) i dont know if this was intentional, but your page contains 2 forms. when you hit submit, only the last form information will get sent since you ended your first form prematurely right after the first input. so this means that the top portion of your form will not get sent. to fix this remove the "</form>" after your first "<input>". also remove the second form declaration further down.
3) when you want to have a dynamic name/value in html, instead of using
value="$name"
use
value="<? echo $name;?>"
4) the way you have the checkboxes setup, you'll probably have to retrieve and check your checkboxes individually to see if it was checked and then form your queries accordingly. bothing wrong with that, but what if you have something like this:
<input type="checkbox" name="category[]" value="spiceGirls" />
SPICE GIRLS </label></td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="category[]" value="Geri" />
GERI</label></td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="category[]" value="Victoria" />
VICTORIA </label></td>
</tr>
<tr>
<td><input type="checkbox" name="category[]" value="Emma" />
EMMA </td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="category[]" value="Mel B" />
MEL B</label></td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="category[]" value="Mel C" />
MEL C</label></td>
with this method, you retrieve it once, run through the array and form your query this way.
$category = $_POST['category'];
for($i = 0; $i < sizeof($category); $i++){
//do stuff here
}
the same thing you can do for the second checkbox group. name it "subcategory[]" or something.
if you want further help your database query formation, please show your tables construct.
i am a real beginner when it comes to form validation etc so take it easy with me please... lol
i have read more tutorials on here etc but cant get my head around it and i need to get this started for her.
this is my new page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
body,td,th {
color: #000000;
}
body {
background-color: #CCCCCC;
}
-->
</style></head>
<body><table width="800" border="0" align="center">
<tr>
<td> </td>
</tr>
<tr>
<td><div align="center">
<form action="#*$!#*$!x
.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<label><br />
<br />
name the file you want to upload
<input type="text" name="textfield" />
</label>
<p>choose the gatagories you want this adding into </p>
<p>
<label>
<input type="checkbox" name="checkbox" value="spicegirls" />
spice girls</label>
</p>
<p>
<label>
<input type="checkbox" name="checkbox2" value="geri" />
geri</label>
</p>
<p>
<label>
<input type="checkbox" name="checkbox3" value="victoria" />
victoria</label>
</p>
<p>
<label>
<input type="checkbox" name="checkbox4" value="emma" />
emma</label>
</p>
<p>
<label>
<input type="checkbox" name="checkbox5" value="melb" />
mel b</label>
</p>
<p>
<label>
<input type="checkbox" name="checkbox15" value="m" />
mel c</label>
</p>
<p> </p>
<p>choose other catagories</p>
<p>
<label>
<input type="checkbox" name="checkbox6" value="cd" />
cd</label>
<label>
<input type="checkbox" name="checkbox7" value="dvd" />
dvd</label>
<label>
<input type="checkbox" name="checkbox8" value="vidro" />
video</label>
<label>
<input type="checkbox" name="checkbox9" value="awards" />
awards</label>
<label>
<input type="checkbox" name="checkbox10" value="memorabilia" />
memorabilia</label>
</p>
<p>
<label>
<input type="checkbox" name="checkbox11" value="clothing" />
clothing</label>
<label>
<input type="checkbox" name="checkbox12" value="magazines" />
magazines</label>
<label>
<input type="checkbox" name="checkbox13" value="jewellery" />
jewellery</label>
<label>
<input type="checkbox" name="checkbox14" value="autographs" />
autographs</label>
</p>
<p> </p>
<p>now choose the file you want to upload </p>
<p>
<label>upload
<input type="file" name="file" />
</label>
</p>
<p> </p>
<p>
<label>submit
<input type="submit" name="Submit" value="Submit" />
</label>
</p>
<p> </p>
</form>
</div></td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</body>
</html>
as you can imagine cataloging a collection that covers 5 solo artists and 1 group could be vast
the database is set up as 6 tables
spicegirls
deri
victoria
emma
melb
melc
the 10 fields are attached to each of the tables in the order they appear in the form i.e cd,dvd,video,awards,memorabilia,clothing,magazines,jewellery,autographs and a blank for later if needed.
later i will be using the data to get the image name from the database fields etc
as obviously some will appear in more than one catagory the image name needs to be in each one selected
on my eventual image pages i will pull the information of image names and add to the page.
hope this helps?
print_r($_POST); //print all posted vars
print_r($_FILES); //print all uploaded files
The first thing you need to do is check if file is a valid image file. additionaly you can also check for the size(make sure its not too big).
i made a function that i used to check for valid file types. It seems to work ok, but i havent really tested it too extensively since my website is small and this is only used by a few users with appropriate permissions(so other users cant even see the form). i also have a validator written in js. just pass the $_FILE['file']['name'] to it
function validImage($var){
$var = strtolower($var);$allowed[] = "jpg"; //jpg is allowed
$allowed[] = "gif"; //gif is allowed
$pattern = "/^[a-zA-Z0-9_-]+\.(";
foreach($allowed as $allow){
$pattern.="$allow¦";
}
$pattern.=")$/";
if(preg_match($pattern, $var)){
return true;
}
return false;
}
the next step is to form your query and store the URL of the file you just uploaded so that you know where it is to access it later.
since your checkboxes are not in array format, you will need to run through all the checkboxes of the top group against the bottom group. something like this i'd imagine:
[code]
//if spice girls is checked
if(isset($_POST['checkbox'])){
$sql = "insert into spicegirls (";
//begin concatenation here. check for checked items from bottom group. (cd, dvd, etc) a whole bunch of if statements are probbaly needed here.
//run query
mysql_query($sql);
}
//repeat for checkbox2 - checkbox5 and checkbox15
the new form is this..
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
body {
background-color: #CCACD2;
}
-->
</style></head>
<html>
<body>
<form enctype="multipart/form-data" action="#*$!#*$!xx.php" method="post">
<p align="center">CHOOSE THE PICTURE FILE YOU WANT TO UPLOAD </p>
<p align="center">maximium file size is 350kb </p>
<p align="center"> </p>
<p align="center">
<input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
Choose a file to upload:
<input name="uploaded_file" type="file" />
</p>
<p align="center"> </p>
<p align="center">USE THE FILE NAME YOU ARE UPLOADING </p>
<p>
<label>
<div align="center">
<div align="center">FILENAME:
<input type="text" name="item" />
<br />
<br />
<br />
<br />
CHOOSE WHICH CATEGORY THE PICTURE NEEDS TO BE INCLUDED IN<br />
<br />
<input type="checkbox" name="item" value="spicegirls" />
SPICE GIRLS<br />
<br />
<input type="checkbox" name="item" value="geri" />
GERI
<input type="checkbox" name="item" value="victoria" />
VICTORIA
<input type="checkbox" name="item" value="emma" />
EMMA
<input type="checkbox" name="item" value="melb" />
MEL B
<input type="checkbox" name="item" value="melc" />
MEL C<br />
<br />
<br />
CHOOSE ITEM CATEGORY<br />
<br />
<input type="checkbox" name="item" value="cd" />
CD
<input type="checkbox" name="item" value="dvd" />
DVD
<input type="checkbox" name="item" value="video" />
VIDEO
<input type="checkbox" name="item" value="awards" />
AWARDS<br />
<br />
<input type="checkbox" name="item" value="memorabilia" />
MEMORABILIA
<input type="checkbox" name="item" value="clothing" />
CLOTHING
<input type="checkbox" name="item" value="magazines" />
MAGAZINES
<input type="checkbox" name="item" value="checkbox" />
JEWELLERY
<input type="checkbox" name="item" value="checkbox" />
AUTOGRAPHS<br />
<br />
<br />
<br />
<br />
YOU CAN ADD THE ITEM IN AS MANY CATEGORIES AS YOU LIKE<br />
<br />
<br />
<br />
<input name="submit" type="submit" value="Upload" />
</div>
</label>
<p align="center"> </p>
<p> </p>
<p> </p>
<p> </p>
<p>
<label></label>
</p>
</form>
</body>
</html>
i managed to get the file upload function to work already so now i need to work on the rest.
i'm sure i'll be back