Image upload without refreshing page

Asked

Viewed 108 times

0

I have the following code that allows you to choose a file and upload it (VIEW IMAGE)inserir a descrição da imagem aqui. I have a form with several inputs and in the end it is possible to upload an image. the problem with my code is that when uploading, it emits an echo even instead of simply uploading to the same page, eventually losing everything written in the form.

HTML code of the form:

    <form class="addform" name="ContactForm" action="addDataAdmin.php" method="POST" enctype="multipart/form-data" autocomplete="off" onsubmit="return ValidateContactForm();">

            ISBN:
            <input type="text" name="ISBN">
            <br> Author's Name:
            <input type="text" name="Authorsname">
            <br> Title:
            <input type="text" name="Title">
            <br> Edition:
            <input type="number" name="edition">
            <br> Year:
            <input type="text" name="year" onkeypress="return justNumber(event)">
            <br> Category:
            <select name="category" size="1">
                <option value="computing">Computing</option>
                <option value="Romance">Romance</option>
                <option value="Fiction">Fiction</option>
                <option value="Non-Fiction">Non-Fiction</option>
            </select>
            <br> Publisher:
            <input type="text" name="publisher">
            <br> Quantity-in-stock:
            <input type="number" name="quantityinstock">
            <br> Price:
            <input type="text" name="price" onkeypress="return justNumber(event)">
            <br>
            <input type="file" name="fileToUpload" id="fileToUpload">
            <input type="submit" value="Upload Image" name="submit">
            <br>
            <input type="submit" value="Send" name="send">
            <input type="reset" value="Clear">
        </form>

Codigo em PHP para fazer o upload.
<?php
    include("config.php");


$target_dir = "uploads/";
$target_file = $target_dir . basename ( $_FILES ["fileToUpload"] ["name"] );
$uploadOk = 1;
$imageFileType = pathinfo ( $target_file, PATHINFO_EXTENSION );

if (isset ( $_POST ["submit"] )) {
    $check = getimagesize ( $_FILES ["fileToUpload"] ["tmp_name"] );
    if ($check !== false) {
        $message = "File is an image - " . $check ["mime"] . ".";
                echo "<script>alert('$message'); window.location.href='adminpage.html';</script>";
        echo "File is an image - " . $check ["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}


if($_SERVER['REQUEST_METHOD'] == 'POST') {

     if(isset($_POST["send"])){


        $ISBN =$conn->real_escape_string($_POST['ISBN']);
        $Authorsname=$conn ->real_escape_string($_POST['Authorsname']);
        $Title= $conn ->real_escape_string($_POST['Title']);
        $edition= $conn ->real_escape_string($_POST['edition']);
        $year= $conn->real_escape_string($_POST['year']);
        $publisher= $conn->real_escape_string($_POST['publisher']);
        $category=$conn->real_escape_string($_POST['category']);
        $quantityinstock=$conn->real_escape_string($_POST['quantityinstock']);
        $price= $conn->real_escape_string($_POST['price']);
        $Image = $conn->real_escape_string ( file_get_contents ( $_FILES ["fileToUpload"] ["tmp_name"]));


        $stmt = $conn->prepare("SELECT * FROM books WHERE ISBN =?");
        $stmt->bind_param("s",$ISBN);
        $ISBN = $_POST['ISBN'];
        $stmt->execute();
        $stmt->store_result();

        if ($stmt->affected_rows > 0){
            echo $message = "ISBN already exists";
                echo "<script>alert('$message'); window.location.href='adminpage.html';</script>";

        }else{

            $ustmt = $conn->prepare ("INSERT INTO books (ISBN, Authorsname, Title, edition, year, publisher, category, quantityinstock, price, Image) VALUES(?,?,?,?,?,?,?,?,?,?)");
            $ustmt->bind_param("sssiissiis",$ISBN,$Authorsname,$Title,$edition,$year,$publisher,$category,$quantityinstock,$price, $Image);
            $ISBN = $_POST['ISBN'];
            $Authorsname = $_POST['Authorsname'];
            $Title = $_POST['Title'];
            $edition = intval($_POST['edition']);
            $year = intval($_POST['year']);
            $publisher = $_POST['publisher'];
            $category = $_POST['category'];
            $quantityinstock = intval($_POST['quantityinstock']);
            $price = intval($_POST['price']);
            $Image = $_POST['Image'];
            $ustmt->execute();

     if($ustmt->affected_rows > 0)
     {
        echo $message = "You have succefully added a new book";
                echo "<script>alert('$message'); window.location.href='adminpage.html';</script>";

        }
            else
            {
                echo 'Error';

            }
        }
}
}
?>
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.