如何在提交html表单时检查日期和文件上传字段是否为空

How to check whether date and file uploading fields are not empty while submitting the html form

本文关键字:文件 字段 是否 日期 提交 html 表单 检查      更新时间:2023-09-26

<!-- it is working fine ,storing image in folder,name in database,and retrieve and view it in browser.--> 
<!DOCTYPE html>
<html>
<body bgcolor="pink">
<center>
<h3> FILE UPLOADING </h3>
<hr>
<hr>
<hr>
       
<form name=form1 action="new2.php" method="post" enctype="multipart/form-data">
    
	Select image to upload:<input type="file" style="border: solid 1px black" name="file" id="file" size="80">
	<br> 
	<br>
	
    <input type="submit" value="Upload Image" name="submit">
</form>
</center>
<?php
if(isset($_POST['submit'])) 
{
	if (!isset($_SESSION)) 
{
    session_start();
}
 
  $id= $_SESSION['id'];
$name=$_FILES["file"]["name"];
$extension=strtolower(substr($name,strpos($name,'.')+1));
$size=$_FILES["file"]["size"];
$type=$_FILES["file"]["type"];
$max_size=1000000;
$tmp_name=$_FILES["file"]["tmp_name"];
if(isset($name) && !empty($name))
{
if (($extension=='jpg'||$extension=='jpeg')&& ($type=='image/jpeg'||$type=='image/jpg') && $size<=$max_size)
{
$location='upload/';
if (file_exists($location.$name))
{
    echo "Sorry, file already exists.<br/>";
}
else
{
if(move_uploaded_file($tmp_name,$location.$name))
{
echo $name.'UPLOADED!<br/>';
//echo 'NAME=',$name,'   TYPE=',$type,'      SIZE=',$size;
$result=mysql_connect("localhost","root","") or die("couldnt connect");
mysql_select_db("sample") or die("couldnot select database");
mysql_query("insert into imagestore2 values ($id,'$name')");
if($result)
{
	echo "Image name saved into database<br/>";
}
else
{
	echo "Sorry,Couln't upload ur file<br/>";
}
}
else
{
echo 'There was an error<br/>';
}
}
}
else
{
echo'FILE MUST BE JPG/JPEG AND SIZE MUST BE 1MB OR LESS THAN THAT.<br/>';
}
}
else 
{ 
echo 'Please choose a file <br/>';
}
}
?>
<?php
//Retrieves data from MySQL
if (!isset($_SESSION)) 
{
    session_start();
}
 //if(isset($_POST['id']))
 //{
 $id= $_SESSION['id'];
mysql_connect("localhost", "root", "") or die ("Could not connect to the database");
mysql_select_db("sample") or die("Could not select database");
$data = mysql_query("SELECT `name` FROM imagestore2 where id='$id'") or
die(mysql_error());
//Puts it into an array
$file_path = 'http://localhost/projectwork/loginpage/upload/';
echo "UPLOADED IMAGES <br/>";
$count=0;
while($row = mysql_fetch_assoc($data))
{//Outputs the image and other data
$src=$file_path.$row['name'];
$count++;
echo "<img height=100 width=100 src=".$src."> &nbsp; &nbsp;";
}
?>
</center>
</body>
<!--reference http://jagdeepmalhi.blogspot.in/2011/02/phpmysql-store-file-path-in-database.html-->
</html>

我有一个HTML表单,有2个字段(照片拍摄日期,上传图像(。如果用户已经输入了这两个字段,我想通过提交按钮上传图像。我已经验证了文件上传字段是否为空,并使用提交按钮上传了文件。现在我正在表单标记中添加日期字段,需要检查两者是否都为空。有人能建议如何使用javascript吗?

尝试这个

 var valueDate = document.getElementById('Date').value;
    if (!valueDate) {
        alert('Invalid date');
    }