在输入图像字段上方显示边框? [英] Display border above input image field?

查看:160
本文介绍了在输入图像字段上方显示边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在图像输入字段上方显示可以显示上传图像的边框?

Is there a way to display a border above an image input field where the uploaded image could be displayed?

我的HTML代码是:

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>form practice</title>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){

            $("#dob").change(function(){
                var value = $("#dob").val();
                var dob = new Date(value);
                var today = new Date();
                var age = Math.floor((today-dob) / (365.25 * 24 * 60 * 60 * 1000));
                if(isNaN(age)) {

                    // will set 0 when value will be NaN
                    age=0;
                }
                else {
                    age=age;
                }
                $('#age').val(age);

            });

        });
    </script>

    <style>

        #profilepic {
            border-style: solid;
            border-width: 2px;
            margin-top: 150px;
        }

    </style>
  </head>

  <body>

    <div class="form2" id="form2" name="form2">

      <form action="php/form2.php" method="post" id="Personalinfo">

        <label for="fname">Name:</label>
        <input type="text" id="fname" name="firstname" placeholder="Client Name..">

        <label for="lname">Lastname:</label>
        <input type="text" id="lname" name="lastname" placeholder="Client Lastname..">

        <label for="dob">Birthday:</label>
        <input type="text" id="dob" name="dob" placeholder="yyyy/mm/dd..">

        <label for="age">Age:</label>
        <input type="text" id="age" name="age" placeholder="Client Age.."><br><br>

        <label for="profilepic">Profile Picture:</label>
        <input type="file" id="profilepic" name="profilepic" accept="image/*" placeholder="Όνομα Πελάτη.." border="5"><br><br>

        <input type="submit" name="submitForm2" value="Submit">


      </form>
    </div>

  </body>
</html>

我在MySQLi数据库中存储数据的PHP代码是:

And my PHP code that stores the data in the MySQLi database is:

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "testdb";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    if (isset($_POST['submitForm2'])) {
        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $dob = $_POST['dob'];
        $age = $_POST['age'];
        $profilepic = $_POST['profilepic'];

        $sql = "INSERT INTO info (firstname, lastname, dob, age, profilepic)
                VALUES ('{$firstname}', '{$lastname}', '{$dob}', '{$age}', '{$profilepic}')";

        if ($conn->query($sql) === TRUE) {
            echo "New record created successfully";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }

    } else {
        echo "Are you sure you enter a firstname and the name of your html submit 
              is submitForm";
    }

    $conn->close();

?>

我在互联网上看到很多教程使用AJAX来实现这样的可视化结果但似乎是我的编码水平很难!有没有一种简单的方法在我的代码中在图像输入元素上方显示带有虚拟图片的边框?

I've seen many tutorials on the internet using AJAX to achieve such a visualised result but it seems a kind of difficult for my coding level! Is there a simple way to display a border with a dummy picture above the image input element in my code?

推荐答案

只是一个想法,用你的代码试试这个

Just an idea, Try this with your code

$('input[type=file]').change(function () {
	$('#dummyImg').attr('src',URL.createObjectURL(event.target.files[0]));
});

#dummyImg{width:100%;}
.img-block{border:2px solid cyan;width:400px;}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="img-block">
  <img src="" id="dummyImg"  />
</div>
<label for="profilepic">Profile Picture:</label>
<input type="file" id="profilepic" name="profilepic" accept="image/*" placeholder="Όνομα Πελάτη.." border="5"><br><br>
<input type="submit" name="submitForm2" value="Submit" />

希望它能为你效劳。

这篇关于在输入图像字段上方显示边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆