1

I have some working code but want to add upload image field but with no success. My current code is:

Form:

<form id="add_product_form" enctype="multipart/form-data">
    <input id="uploadFile" type="file" name="image" class="img" /><br />
    <input id="status" type="checkbox" checked /><br />
    <input id="product" type="text" /><br />
    <label for="button" id="response"></label>
    <input type="button" id="button" value="Добави" /><br />
</form>

jQuery:

<script type="text/javascript">
$('document').ready(function(){
    $('#button').click(function(){

        var image = $('input[type=file]').val().split('\\').pop();
        function chkb(bool){ if(bool) return 1; return 0; } var status=chkb($("#status").is(':checked'));
        if($('#product').val()==""){ alert("enter name"); return false; } else { var product = $('#product').val(); } 

        jQuery.post("products_add_process.php", { 
            image: image,
            status: status, 
            product: product
            }, 

        function(data, textStatus) {
            $('#response').html(data);
            if(data == 1){
                $('#response').html("OK");
                $('#response').css('color','green');
                document.getElementById("add_product_form").reset();
            } else {
                $('#response').html("Not OK");
                $('#response').css('color','red');
            }
        });
    });
});
</script>

products_add_process.php:

<?php
$image_name = $_FILES['image']['name'];
$image_type = $_FILES['image']['type'];
$image_size = $_FILES['image']['size'];
$image_tmp_name = $_FILES['image']['tmp_name'];
$status = $_POST['status'];
$product = $_POST['product'];

if($image_name == '') {
    echo "<script>alert('Select image')</script>";
    exit();
    } else {
        $random_digit=rand(0000000000,9999999999);
        $image=$random_digit.$image_name;
        move_uploaded_file($image_tmp_name,"uploads/$image");

        $query=mysql_query("INSERT INTO products(product, image, status) VALUES ('$product', '$image', '$status')");

        if(mysql_affected_rows()>0){
            $response = 1;
            echo "1";
            } else {
            $response = 2;
            echo "2";
            }
        }
?>

I put alert 'Select image' and then understand that $image_name is empty and maybe somewhere have to put some code to say that I want to send DATA TYPE. I try to add to form enctype="multipart/form-data" but won't work.

Where and what have to add in existing code to make sending data, without making very big changes because this code have in a lot of files and will be difficult to edit big part of codes.

Maybe have to add that form and jQuery are in php file which is called in other mother file and structure is something like this:

products.php /call products_add.php/
   products_add.php /where is the form and jQuery/
       products_add_process.php /called from products_add.php to upload data/

p.s. Sorry if I don't explain my problem well but I'm from soon at stackoverflow and still learning how to post my questions :)

0 Answers0