Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

php - Upload image using jquery

I have a working php code to upload image in the database. Is it Possible to transform it to jquery? If so, what do I need to do? I am new to jquery btw. Thanks

This code works just fine. But I need to do it in jquery.

<form action = 'upload.php' method = 'post' enctype="multipart/form-data">

    <input type="file" name="image" > <br>
    <input type= 'submit' value = 'Add' id = 'Add' name = 'Add'>

</form> 


<?php   
    if(isset($_FILES['image']))
    {
    $target_Path = "images/";
    $target_Path = $target_Path.basename($_FILES['image']['name'] );
    move_uploaded_file( $_FILES['image']['tmp_name'], $target_Path );

    $name = $_FILES['image']['name'];
    }
    if(isset($_POST['Add']))
    {

        if($_POST["Add"] == "Add") 
        {
        $add = "Insert Into img(path) Values('$name')";
        $up = mysql_query($add);

            $status = "Upload success!";
            print '<script type="text/javascript">'; 
            print 'alert(" '.$status.' ")'; 
            print '</script>';                  
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<form action='upload.php' method='post' enctype="multipart/form-data" id="formupload">
    <input type="file" name="image"/> <br>
    <input type='submit' value='Add' id='Add' name='Add/>
</form> 
  1. You need to first setup a callback for the submit event of the form.

    $("#formupload").on("submit", upload_image);
    
    • JQuery selectors work a lot like CSS; $("#formupload") selects the element whose id is formupload.
    • on is used to register a handler for an event.
    • Here, we are setting up a handler(the upload_image function) for the submit event of the element whose id is formupload.
  2. Make an AJAX call to the php script.

    function upload_image(event){
    
        event = event || window.event;
    
        // Prevent the default form action i.e. loading of a new page
        if(event.preventDefault){ // W3C Variant
            event.preventDefault();
        }
        else{ // IE < 9
            event.returnValue = false;
        }
    
        $.ajax({
            url: "upload.php",
            type: "POST",
            data: new FormData($('#formupload')[0]), 
    
            success : function(data){
                // Show success message
            },
            enctype: 'multipart/form-data',
            processData: false,
            contentType: false,
            cache: false
        });
    }
    
    • You can prevent the default action of form submission, which is to load the POST response, which is what the first few lines of the function is doing.
    • An AJAX call is made using $.ajax which is the jQuery utility for performing an AJAX call.
    • The url property is to be filled by that of your PHP script.
    • Since it is a file upload, specify the HTTP method as POST.
    • The data property is the payload of the POST request, which is the content of the file you are trying to upload.
    • You can specify the success callback using the success property, which is the function that will be called on completion of the file upload.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...