Display border above input image field?

时间:2017-08-05 10:39:39

标签: php ajax image mysqli border

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

My HTML code is:

<!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>

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();

?>

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?

1 个答案:

答案 0 :(得分:0)

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" />

Hope it will work for you.