chesemonkyloma
09-17-2006, 04:16 PM
Is there any better way of having self submitting forms than just using a bunch of elseif statements? Here's my code, and it looks very messy.
<?php $metaTitle="Sockigami - Submit Model";
include('http://sockigami.com/includes/header.inc');
include('http://models.sockigami.com/includes/modelsnavigation.inc');
include('http://sockigami.com/includes/navigation.inc');?>
<div class="content">
<?php
if(isset($_POST['Submit1'])){
if ($_files['Picture']['size']<100000){
$modelName=ucwords(strtolower($_post[ModelName]));
$creatorName=ucwords(strtolower($_post[CreatorName]));
$id=substr(md5(uniqid(rand(), true)), 0, 6);
$imageType=substr(strrchr($_files['Picture']['Name'], "."), 1);
$imagePath="images/models/".$id.".".$imageType;
$link=mysql_connect("localhost", "username", "password")or die('i cannot connect to the database because: ' . mysql_error());
mysql_select_db ("sockigam_", $link);
$sql="INSERT INTO Models (Name, Creator, Email, Description, Category, Socks, NoOfSteps, Difficulty, ImageType, ID)
values ('$modelName', '$creatorName', '$_POST[Email]', '$_POST[Description]', '$_POST[Category]', $_POST[Socks], $_POST[StepsNumber], '$_POST[Difficulty]', '$imageType', '$id')";
mysql_query($sql, $link) or die(mysql_error());
move_uploaded_file($_files["picture"]["tmp_name"], $imagepath);
echo "<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">
<table>";
for ($i=1; $i<=$_post[StepsNumber]; $i++){
echo "<tr>
<td>Step $i:</td>
<td><textarea rows=2 cols=30 name=\"step{$i}\"></textarea></td>
</tr>";
}
echo "<input type=\"hidden\" name=\"model\" value=\"{$modelName}\"><tr><td><input type=\"submit\" name=\"submit2\" value=\"submit\"></td></tr>
</table></form>";
}
else {
echo "<font color=\"red\">The picture size is greater than 100 kilobytes</font>";
}
}elseif(isset($_POST['Submit2'])){
$link=mysql_connect("localhost", "username", "password")
or die('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("sockigam_", $link);
$sql="SELECT Email, NoOfSteps, ID FROM Models
WHERE Name='$_POST[Model]'";
$query=mysql_query($sql, $link) or die(mysql_error());
$result=mysql_fetch_array($query);
for ($i=1; $i<=$result[noofsteps]; $i++)
{
$steps .= $i.". ".$_post[step."".$i]." <br />";
}
$sql2="UPDATE Models
SET Steps = '$steps'
WHERE Name='$_POST[Model]'";
mysql_query($sql2, $link) or die(mysql_error());
echo "Thank you for your time, your model is being reviewed";
mail("chesemonkyloma@gmail.com",
"New Model: $_POST[Model]",
"http://models.sockigami.com/master/checkmodel.php?id={$result[ID]}",
'From: "New Model" <models@sockigami.com>');
mail("$result[Email]",
"Thank you for your Sockigami Model",
"Thank you for submitting the $_POST[Model]\n
You will be sent an email if it is accepted, and it will be located at:\n
http://models.sockigami.com/models/showmodel.php?id={$result[ID]}",
'From: "New Model" <models@sockigami.com>');
echo $result[Email];
}else{
?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
<tr>
<td>Name of Creator:</td>
<td><input type="text" name="CreatorName" size="30" value=""></td>
</tr>
<tr>
<td>Email Address:</td>
<td><input type="text" name="Email" size="30" value=""></td>
</tr>
<tr>
<td>Model Category:</td>
<td>
<select name="Category">
<option></option>
<option value="Entertainment">Entertainment</option>
<option value="Decoration">Decoration</option>
<option value="Other">Other</option>
</select>
</td>
</tr>
<tr>
<td>Model Name:</td>
<td><input type="text" name="ModelName" size="30" value=""></td>
</tr>
<tr>
<td>Description:</td>
<td><textarea rows=2 cols=30 name="Description"></textarea></td>
</tr>
<tr>
<td>Model Difficulty:</td>
<td>
<select name="difficulty">
<option value="Easy">Easy</option>
<option value="Medium">Easy-Medium</option>
<option value="Hard">Medium</option>
<option value="Medium">Medium-Hard</option>
<option value="Hard">Hard</option>
</select>
</td>
</tr>
<tr>
<td>Number of Socks:</td>
<td><input type="text" name="Socks" size="2" value=""></td>
</tr>
<tr>
<td>Number of Steps:</td>
<td><input type="text" name="StepsNumber" size="2" value=""></td>
</tr>
<tr>
<td>Picture:</td>
<td>
<input type="file" name="Picture" />
</td>
</tr>
</table>
<input type="submit" name="Submit1" value="Submit">
</form>
<?php
}
?>
</div>
<?php include('http://sockigami.com/includes/footer.inc');?>
<?php $metaTitle="Sockigami - Submit Model";
include('http://sockigami.com/includes/header.inc');
include('http://models.sockigami.com/includes/modelsnavigation.inc');
include('http://sockigami.com/includes/navigation.inc');?>
<div class="content">
<?php
if(isset($_POST['Submit1'])){
if ($_files['Picture']['size']<100000){
$modelName=ucwords(strtolower($_post[ModelName]));
$creatorName=ucwords(strtolower($_post[CreatorName]));
$id=substr(md5(uniqid(rand(), true)), 0, 6);
$imageType=substr(strrchr($_files['Picture']['Name'], "."), 1);
$imagePath="images/models/".$id.".".$imageType;
$link=mysql_connect("localhost", "username", "password")or die('i cannot connect to the database because: ' . mysql_error());
mysql_select_db ("sockigam_", $link);
$sql="INSERT INTO Models (Name, Creator, Email, Description, Category, Socks, NoOfSteps, Difficulty, ImageType, ID)
values ('$modelName', '$creatorName', '$_POST[Email]', '$_POST[Description]', '$_POST[Category]', $_POST[Socks], $_POST[StepsNumber], '$_POST[Difficulty]', '$imageType', '$id')";
mysql_query($sql, $link) or die(mysql_error());
move_uploaded_file($_files["picture"]["tmp_name"], $imagepath);
echo "<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">
<table>";
for ($i=1; $i<=$_post[StepsNumber]; $i++){
echo "<tr>
<td>Step $i:</td>
<td><textarea rows=2 cols=30 name=\"step{$i}\"></textarea></td>
</tr>";
}
echo "<input type=\"hidden\" name=\"model\" value=\"{$modelName}\"><tr><td><input type=\"submit\" name=\"submit2\" value=\"submit\"></td></tr>
</table></form>";
}
else {
echo "<font color=\"red\">The picture size is greater than 100 kilobytes</font>";
}
}elseif(isset($_POST['Submit2'])){
$link=mysql_connect("localhost", "username", "password")
or die('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("sockigam_", $link);
$sql="SELECT Email, NoOfSteps, ID FROM Models
WHERE Name='$_POST[Model]'";
$query=mysql_query($sql, $link) or die(mysql_error());
$result=mysql_fetch_array($query);
for ($i=1; $i<=$result[noofsteps]; $i++)
{
$steps .= $i.". ".$_post[step."".$i]." <br />";
}
$sql2="UPDATE Models
SET Steps = '$steps'
WHERE Name='$_POST[Model]'";
mysql_query($sql2, $link) or die(mysql_error());
echo "Thank you for your time, your model is being reviewed";
mail("chesemonkyloma@gmail.com",
"New Model: $_POST[Model]",
"http://models.sockigami.com/master/checkmodel.php?id={$result[ID]}",
'From: "New Model" <models@sockigami.com>');
mail("$result[Email]",
"Thank you for your Sockigami Model",
"Thank you for submitting the $_POST[Model]\n
You will be sent an email if it is accepted, and it will be located at:\n
http://models.sockigami.com/models/showmodel.php?id={$result[ID]}",
'From: "New Model" <models@sockigami.com>');
echo $result[Email];
}else{
?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
<tr>
<td>Name of Creator:</td>
<td><input type="text" name="CreatorName" size="30" value=""></td>
</tr>
<tr>
<td>Email Address:</td>
<td><input type="text" name="Email" size="30" value=""></td>
</tr>
<tr>
<td>Model Category:</td>
<td>
<select name="Category">
<option></option>
<option value="Entertainment">Entertainment</option>
<option value="Decoration">Decoration</option>
<option value="Other">Other</option>
</select>
</td>
</tr>
<tr>
<td>Model Name:</td>
<td><input type="text" name="ModelName" size="30" value=""></td>
</tr>
<tr>
<td>Description:</td>
<td><textarea rows=2 cols=30 name="Description"></textarea></td>
</tr>
<tr>
<td>Model Difficulty:</td>
<td>
<select name="difficulty">
<option value="Easy">Easy</option>
<option value="Medium">Easy-Medium</option>
<option value="Hard">Medium</option>
<option value="Medium">Medium-Hard</option>
<option value="Hard">Hard</option>
</select>
</td>
</tr>
<tr>
<td>Number of Socks:</td>
<td><input type="text" name="Socks" size="2" value=""></td>
</tr>
<tr>
<td>Number of Steps:</td>
<td><input type="text" name="StepsNumber" size="2" value=""></td>
</tr>
<tr>
<td>Picture:</td>
<td>
<input type="file" name="Picture" />
</td>
</tr>
</table>
<input type="submit" name="Submit1" value="Submit">
</form>
<?php
}
?>
</div>
<?php include('http://sockigami.com/includes/footer.inc');?>