Jump to content


Photo

PHP Form


  • Please log in to reply
9 replies to this topic

#1 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 06 August 2006 - 09:55 AM

I have a form written in PHP that the user fills out the form and can also upload images at the same time.  Once the form is submitted, the form fields are uploaded to a MySQL Database and the images are suppose o upload to a separate directory.  The problem I am having is that I can set it up so either the data is uploaded, or the images are uploaded, but they won't both get uploaded.  I have tried changing the names or the submit buttons around, and even if they are the same, only one or the other will upload.

if (!isset($_POST['Submit'])){  is the one line and I have changed the word submit to action to match up with the other line which reads

if (isset($j)&&$j>0) print "Your file(s) has been uploaded.<br>";
  print "<form method='post' enctype='multipart/form-data'>";
  for($i=0;$i<$numoffile;$i++) {
    print "<input type='file' name='myfiles[]' size='30'><br>";
  }
 print "<BR><BR> <input type='submit' name='action' value='Submit Work Order'>";

I have also tried changing the word action to submit (both with a capital and small "s") and again, one or the other will upload.

Is there any way to get them both to upload, or do I need to go back to the drawing board to figure out a way to upload 3 - 4 images with the form?

here is the complete form code:
<?php
include_once "myconnect.php";
?>
<?php
/* WorkOrder.php */
if (!isset($_POST['Submit'])){
?>
<head>
<link href="ge.css" rel="stylesheet" type="text/css">
<title>GE Work Order</title>
</head>
<form action="" method="post" enctype="multipart/form-data">
<center><div>Work Order #:<input type="text" size="12" maxlength="8" name="Work" value=<?php if (isset($_POST['Work'])) {echo $_POST['Order'];} else {echo date("mHis");}?>>
Date:<input type="text" size="12" maxlength="12" name="Sched" value=<?php if (isset($_POST['Sched'])) {echo $_POST['Date'];} else {echo date("Y-m-d");} ?>>
<?php
$result = mysql_query("SELECT * FROM employees") or die(mysql_error());
echo "Technician Name: <select name='Tech'>";
while($row=mysql_fetch_array($result))
{
  echo"<option>$row[Name]</option>";
}
  echo"</select>";
?></select><BR></><BR></>
Site:<select name="Site">
<option value="No Selection Made">Select Site</option>
<option value="Burdett">Burdett</option>
<option value="Coaldale">Coaldale</option>
<option value="Fort McLeod">Fort McLeod</option>
<option value="Taber">Taber</option>
</select>
Unit Serial #:<input type="text" size="10" maxlength="10" name="Serial">
Unit Hours:<input type="text" size="6" maxlength="6" name="Hours">
<BR></><BR></>
Unit Starts:<input type="text" size="6" maxlength="6" name="Starts">
Severity:<select name="Severe">
<option value="No Selection Made">Please Select Level</option>
<option value="Low Level">Low Level</option>
<option value="A.S.A.P.">A.S.A.P.</option>
<option value="Immediate Attention">Immediate Attention</option>
<option value="Severe">Severe</option>
</select>
 Safety:<select name="Safe">
<option value="No Selection Made">Please Select Level</option>
<option value="Low Risk of Injury">Low Risk of Injury</option>
<option value="Medium Risk of Injury">Medium Risk of Injury</option>
<option value="High Risk of Injury">High Risk of Injury</option>
<option value="Injury has Occurred">Injury has Occurred</option>
</select>
</div> <BR>
<div><B>Description of Issue:</B><BR><textarea rows="4" name="Issue" cols="116"></textarea>
</div><BR>
<div><B>Possible Resolution</B><BR><textarea rows="4" name="Resolve" cols="116"></textarea>
</div><BR>
<div><B>List of Parts Required (known at this time)</B>:</div>
<div><input type="text" size="155" maxlength="155" name="PartsA"><BR>
<input type="text" size="155" maxlength="155" name="PartsB"><BR>
<input type="text" size="155" maxlength="155" name="PartsC"><BR>
<input type="text" size="155" maxlength="155" name="PartsD"><BR>
<input type="text" size="155" maxlength="155" name="PartsE"><BR>
<input type="text" size="155" maxlength="155" name="PartsF"><BR>
<input type="text" size="155" maxlength="155" name="PartsG"><BR>
</div><BR>
<div>Request for Assistance:<BR><textarea rows="4" name="Assist" cols="116"></textarea>
</div><BR><BR>

<div>
<div><center><B>Send this form to:<BR>
<?php
$result = mysql_query("SELECT * FROM employees") or die(mysql_error());
echo "<select name='Tech'multiple>";
while($row=mysql_fetch_array($result))
{
  echo"<option>$row[Name]</option>";
}
  echo"</select>";
?></select>
<BR><div>Hold The Control Key down to select multiple names</b></div><BR><BR>




<!--Work Order # <?php $_POST["Work"];?> was opened on <?php $_POST["Sched"];?> by
<?php $_POST["Tech"];?>.<br><br>
The Unit is located in<?php $_POST["Site"]?> with serial number <?php $_POST["Serial"]?>
and this unit has <?php $_POST["Hours"]?> Hours on it.
The Reported level of Severity is<?php $_POST["Severe"];?>and the Safety Level is<?php $_POST["Safe"];?>. <BR>

Additional Details on this equipment failure are as follows:<BR><BR>
<B>Description of Issue:</B><BR>
<?php $_POST["Issue"]?><BR><BR>
<B>Possible Resolution</B><BR>
<?php $_POST["Resolve"]?><BR><BR>
<B>Request for Assistance:</B><BR>
<?php $_POST["Assist"]?><BR><BR>
Parts Required: (that the technician is currently aware of)
<?php $_POST["PartsA"]?><BR>
<?php $_POST["PartsB"]?><BR>
<?php $_POST["PartsC"]?><BR>
<?php $_POST["PartsD"]?><BR>
<?php $_POST["PartsE"]?><BR>
<?php $_POST["PartsF"]?><BR>
<?php $_POST["PartsG"]?><BR>  -->


</div><BR><BR>
<?php
 /***********************************************
  * Snippet Name : File Uploader                *
  * Scripted By  : Hermawan Haryanto            *
  * Website      : http://hermawan.com *
  * Email        : hermawan@codewalkers.com        *
  * License      : GPL (General Public License) *
  ***********************************************/
  $numoffile = 5;
  // Fix path of your file to be uploaded, don't forget to CHMOD 777 to this folder
  $file_dir  = "/wamp/www/BenProject/uploadfile/";
  if ($_POST) {
    for ($i=0;$i<$numoffile;$i++) {
      if (trim($_FILES['myfiles']['name'][$i])!="") {
        $newfile = $file_dir.$_FILES['myfiles']['name'][$i];
        move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile);
        $j++;
      }
    }
  }
  if (isset($j)&&$j>0) print "Your file(s) has been uploaded.<br>";
  print "<form method='post' enctype='multipart/form-data'>";
  for($i=0;$i<$numoffile;$i++) {
    print "<input type='file' name='myfiles[]' size='30'><br>";
  }
 print "<BR><BR> <input type='submit' name='action' value='Submit Work Order'>";
 //"<input type="submit" name="submit" value="Submit Work Order">"  <BR>
?>
</form>
<?php
} else
{
$Work = $_POST["Work"];
$Sched = $_POST["Sched"];
$Name = $_POST["Tech"];
$Site = $_POST["Site"];
$Serial = $_POST["Serial"];
$Hours = $_POST["Hours"];
$Starts = $_POST["Starts"];
$Issue = $_POST["Issue"];
$Severity = $_POST["Severe"];
$Resolution = $_POST["Resolve"];
$Assistance = $_POST["Assist"];
$PartsA = $_POST["PartsA"];
$PartsB = $_POST["PartsB"];
$PartsC = $_POST["PartsC"];
$PartsD = $_POST["PartsD"];
$PartsE = $_POST["PartsE"];
$PartsF = $_POST["PartsF"];
$PartsG = $_POST["PartsG"];
$Safety = $_POST["Safe"];


mysql_query("INSERT INTO `work`(Work, Sched, Name, Site, Serial, Hours, Starts, Issue,
Severity, Resolution, Assistance, PartsA, PartsB, PartsC, PartsD, PartsE, PartsF, PartsG, Safety)
VALUES ('$Work', '$Sched', '$Name', '$Site', '$Serial', '$Hours', '$Starts', '$Issue',
'$Severity', '$Resolution', '$Assistance','$PartsA', '$PartsB', '$PartsC', '$PartsD',
'$PartsE', '$PartsF', '$PartsG', '$Safety')")or die(mysql_error());

echo "Your Work Order has been posted successfully";

}
?>
<?php
//echo print_r($_POST)
?>


#2 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 06 August 2006 - 10:20 AM

How does this script work? I can see two opening form tags but only one closing form tag.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#3 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 06 August 2006 - 10:25 AM

That is probably where the problem is.  I had the form working and then added the script to upload images.  I may need to remove the one at the top, and then use a hidden attribute.  Do you think that would work?

#4 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 06 August 2006 - 10:31 AM

Well you can have two forms on one page, but only the contents of the one that is submitted will get POSTed to the handling script. So if you want the inputted data and the image selection to both be posted, put all the input into the one form.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#5 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,430 posts
  • LocationBelgium

Posted 06 August 2006 - 10:34 AM

may i ask what the following does?

<!--Work Order # <?php $_POST["Work"];?> was opened on <?php $_POST["Sched"];?> by
<?php $_POST["Tech"];?>.<br><br>
The Unit is located in<?php $_POST["Site"]?> with serial number <?php $_POST["Serial"]?>
and this unit has <?php $_POST["Hours"]?> Hours on it.
The Reported level of Severity is<?php $_POST["Severe"];?>and the Safety Level is<?php $_POST["Safe"];?>. <BR>

Additional Details on this equipment failure are as follows:<BR><BR>
<B>Description of Issue:</B><BR>
<?php $_POST["Issue"]?><BR><BR>
<B>Possible Resolution</B><BR>
<?php $_POST["Resolve"]?><BR><BR>
<B>Request for Assistance:</B><BR>
<?php $_POST["Assist"]?><BR><BR>
Parts Required: (that the technician is currently aware of)
<?php $_POST["PartsA"]?><BR>
<?php $_POST["PartsB"]?><BR>
<?php $_POST["PartsC"]?><BR>
<?php $_POST["PartsD"]?><BR>
<?php $_POST["PartsE"]?><BR>
<?php $_POST["PartsF"]?><BR>
<?php $_POST["PartsG"]?><BR>  -->

if it is your intention that it is being outputted, then i think your missing a few echo's/print's. also from $_POST['issue'] to $_POST['PartsG'] a few ; are also missing, set your error_reporting to error_reporting(E_ALL);

also looking at the following, tells me you are someone that really likes typing...

$Work = $_POST["Work"];
$Sched = $_POST["Sched"];
$Name = $_POST["Tech"];
$Site = $_POST["Site"];
$Serial = $_POST["Serial"];
$Hours = $_POST["Hours"];
$Starts = $_POST["Starts"];
$Issue = $_POST["Issue"];
$Severity = $_POST["Severe"];
$Resolution = $_POST["Resolve"];
$Assistance = $_POST["Assist"];
$PartsA = $_POST["PartsA"];
$PartsB = $_POST["PartsB"];
$PartsC = $_POST["PartsC"];
$PartsD = $_POST["PartsD"];
$PartsE = $_POST["PartsE"];
$PartsF = $_POST["PartsF"];
$PartsG = $_POST["PartsG"];
$Safety = $_POST["Safe"];

to safe you some time, try the following next time:

while (false !== (list($k, $v) = each($_POST))) {
   ${$k} = $v;
}



#6 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 06 August 2006 - 10:36 AM

Sorry, the first portion isn't in use right now.  I am learning, and trying to figure out how to email results to people who are selected through one of the form fields, so it is quite possible that it isn't even close.  I am still trying to figure it out, so if I am way off, save me the time and tell me to start over.

#7 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 06 August 2006 - 10:46 AM

Do you have a suggestion as the best way to set this up so they are all in one form?

#8 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 06 August 2006 - 11:04 AM

Using

while (false !== (list($k, $v) = each($_POST))) {
  ${$k} = $v;
}

Does that just cover everything within what I typed, and prevents errors as well?

#9 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 06 August 2006 - 11:13 AM

Do you have a suggestion as the best way to set this up so they are all in one form?


Remove this line:
print "<form method='post' enctype='multipart/form-data'>";

and move this code
  if ($_POST) {
    for ($i=0;$i<$numoffile;$i++) {
      if (trim($_FILES['myfiles']['name'][$i])!="") {
        $newfile = $file_dir.$_FILES['myfiles']['name'][$i];
        move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile);
        $j++;
      }
    }
  }
  if (isset($j)&&$j>0) print "Your file(s) has been uploaded.<br>";

from inside the form part of the script to inside the "else" construct where you are reading all the other POSTed values.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#10 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 06 August 2006 - 11:28 AM

Not sure if I moved the one peice to the right spot, so I am showing it below.  Neither the image uploads, not the form data are being copied to the database now.  I realize the uploads should be going to a directory, but they are not going there.

Ken
<?php
 /***********************************************
  * Snippet Name : File Uploader                *
  * Scripted By  : Hermawan Haryanto            *
  * Website      : http://hermawan.com *
  * Email        : hermawan@codewalkers.com        *
  * License      : GPL (General Public License) *
  ***********************************************/
  $numoffile = 5;
  // Fix path of your file to be uploaded, don't forget to CHMOD 777 to this folder
  $file_dir  = "/wamp/www/BenProject/uploadfile/";
  print "<form method='post' enctype='multipart/form-data'>";
  for($i=0;$i<$numoffile;$i++) {
    print "<input type='file' name='myfiles[]' size='30'><br>";
  }
 print "<BR><BR> <input type='submit' name='action' value='Submit Work Order'>";
 //"<input type="submit" name="submit" value="Submit Work Order">"  <BR>
?>
</form>
<?php
} else
if ($_POST) {
    for ($i=0;$i<$numoffile;$i++) {
      if (trim($_FILES['myfiles']['name'][$i])!="") {
        $newfile = $file_dir.$_FILES['myfiles']['name'][$i];
        move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile);
        $j++;
      }
    }
  }
  if (isset($j)&&$j>0) print "Your file(s) has been uploaded.<br>";
{
$Work = $_POST["Work"];
$





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users