形式与上传一次提交

时间:2014-02-27 14:49:56

标签: php forms submit

我整个星期都在努力解决这个问题。

我有一个用于捕获数据和空表单字段验证的表单,如果任何字段为空,则会阻止提交表单。

同样在表格中,我有一个允许用户上传图像的输入。问题是用户无需在表单中输入任何数据即可上传图像。由于图像与表单相关联,因此必须在单个提交中写入,而不是单独提交。

我只发布了一小段代码示例,因为我认为问题是表单而输入的不是php?

<form enctype="multipart/form-data" method="POST" action="textandimages.php">

<div><input id="name" class="insetcard" name="name" type="text" placeholder="Name" value="<?PHP print $name ; ?>"/>
<p class="error"><?php echo $nameErr;?></p></div>

<input type="file" name="upload" />

<input name="Submit" type="submit" class="submitbutton" value="submit">

完整代码

<?php
session_start();
include "connect.php";
//require("checkLoginSession.php");
$message = $_GET['message'];

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$path = "uploads/" . $_FILES["file"]["name"];
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
    //echo '<img src="'.$path.'" alt="" />';

    if (file_exists("uploads/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "uploads/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
      }
    }
}

$Link = mysql_connect($Host, $User, $Password);
$user = $_SESSION['UserName'];
$Query = "INSERT INTO $Table_3 VALUES ('0','".mysql_escape_string($user)."','{$path}')";
if (mysql_query($Query, $Link)) { echo ("Image stored"); }else { die ("Failed to connect to database!: " .mysql_error()); };

error_reporting(E_ERROR);
$nameErr = $intelligenceErr = $strengthErr = $speedErr = $energyErr = $fightingErr = $googleErr = $biographyErr = "";
$name = $intelligence = $strength = $speed = $energy = $fighting = $google = $biography = "";

function validate_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

if (isset($_POST['Submit']))
{
    $has_errors = false;

    if (empty($_POST["name"])) {
        $has_errors = true;
        $nameErr = "Enter a name";
    }else{
        $name = validate_input($_POST["name"]);
    }

    if (empty($_POST["intelligence"])) {
        $has_errors = true;
        $intelligenceErr = "Enter intelligence value";
    } else {
        $intelligence = validate_input($_POST["intelligence"]);
    }

    if (empty($_POST["strength"])) {
        $has_errors = true;
        $strengthErr = "Enter strength value";
    } else {
        $strength = validate_input($_POST["strength"]);
    }

    if (empty($_POST["speed"])) {
        $has_errors = true;
        $speedErr = "Enter speed value";
    } else {
        $speed = validate_input($_POST["speed"]);
    }

    if (empty($_POST["energy"])) {
        $has_errors = true;
        $energyErr = "Enter energy value";
    } else {
        $energy = validate_input($_POST["energy"]);
    }

    if (empty($_POST["fighting"])) {
        $has_errors = true;
        $fightingErr = "Enter fighting value";
    } else {
        $fighting = validate_input($_POST["fighting"]);
    }

    if (empty($_POST["google"])) {
        $has_errors = true;
        $googleErr = "Enter the googleindex";
    } else {
        $google = validate_input($_POST["google"]);
    }

    if (empty($_POST["biography"])) {
        $has_errors = true;
        $biographyErr = "Enter a biography";
    } else {
        $biography = validate_input($_POST["biography"]);
    }

    if (!$has_errors)
    {
        $Link = mysql_connect($Host, $User, $Password);
        $user = $_SESSION['UserName'];
        $Query = "INSERT INTO $Table_2 VALUES ('0','".mysql_escape_string($user)."','".mysql_escape_string($name)."','".mysql_escape_string($intelligence)."', '".mysql_escape_string($strength)."', '".mysql_escape_string($speed)."', '".mysql_escape_string($energy)."', '".mysql_escape_string($fighting)."', '".mysql_escape_string($google)."', '".mysql_escape_string($biography)."')";

        if(mysql_db_query ($DBName, $Query, $Link)) {
            $message = "Card created";
            header("Location: comics.php?message=".urlencode($message));
        } else {

            die("Query was: $Query. Error: ".mysql_error($Link));
        }
    }
}
?>

1 个答案:

答案 0 :(得分:0)

测试textandimages.php中的两个值:

if(isset($_POST['name'],$_POST['upload']) && !empty($_POST['name']) && !empty($_POST['upload']) ){
// do something
} else { 
//error, something is missing...
}