user2605321
user2605321

Reputation: 93

File Upload using ajax and passing a variable

I am uploading pictures using ajax.. It's happening very well..But the problem is I can't pass the current album_id through ajax to the php page where uploading script is there..Can anyone help? Here's my code:

index.php

$(function () {

     $('body').on('click', '.upload', function(){   
            var form = new FormData($('#myform')[0]);

                $.ajax({
                url: 'image_send.php',
                type: 'POST',
                success: function (r) {
                    $('.container').html(r);
                },
                data: form,
                cache: false,
                contentType: false,
                processData: false
            });
        });
    });

<div id="image_upload_form">
<form enctype="multipart/form-data" id="myform">    
<input type="file" accept="image/*" multiple name="image[]" id="image" />
<input type="button" value="Upload images" class="upload" />
</form>

I get the current album id with $album_id = @$_GET['image_album'];

My php code: image_send.php

foreach($_FILES['image']['tmp_name'] as $key => $tmp_name ){
    if(((@$_FILES['image']['type'][$key] == 'image/jpeg') ||
    (@$_FILES['image']['type'][$key] == 'image/jpg') || 
    (@$_FILES['image']['type'][$key] == 'image/png') ||
    (@$_FILES['image']['type'][$key] == 'image/tif') ||
    (@$_FILES['image']['type'][$key] == 'image/tiff') ||    
    (@$_FILES['image']['type'][$key] == 'image/gif') ||
    (@$_FILES['image']['type'] == 'image/png')) 
    && (@$_FILES['image']['size'][$key] < 1073741824))      //10 mb in bytes
    {
        if(file_exists("image/".@$_FILES['image']['name'][$key])){
        echo @$_FILES['image']['name'][$keys]."Already exists";
        }
        else{
    $picture = @$_FILES['image']['name'][$key];
    $image_upload_query = mysql_query("INSERT INTO image_upload_public VALUES('','$picture','Here I want to insert the current album_id')");
            if($image_upload_query){
            move_uploaded_file(@$_FILES['image']['tmp_name'][$key], "image/".$picture);
            }

        }

I don't understand where in ajax will I insert $album_id and how will I get that in my php page..I know that my script is vulnerbale to sql injection..I can deal with that later..But for the time being I want to know how can I pass the $album_id to the other page and insert it into my database

Upvotes: 2

Views: 2797

Answers (2)

Steve
Steve

Reputation: 91

You are using "POST" to send the data to the php file (image_send.php).

You need to use $_POST['image_album'] as opposed to $_GET['image_album'].

Upvotes: 0

Dave
Dave

Reputation: 3658

<input type="hidden" name="album-id" value="something" />

Pass the value like any other using an input. Then access it in $_POST.

if ( isset($_POST['album-id']) ) {
    $album_id = $_POST['album-id'];
}

Then include it in your SQL.

Upvotes: 2

Related Questions