0

how to send data from javascript to php?if i use ajax, there will be error? here's my code

script.js

function uploadPhoto(imageURI) {
var options = new FileUploadOptions();

    options.fileKey="file";
    options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
    options.mimeType="image/jpeg";

    options.chunkedMode = false;

    var ft = new FileTransfer();
    ft.upload(imageURI, "http://*my_ip*/TA/php/upload.php", win, fail, options);
};

upload.php

include 'db.php';
$t=time();
$id_place = $_GET["id_place"];
$file_name = $_FILES["file"]["name"].$t.".jpg";
$dir_full = "images/full/"."full_".$file_name;
$dir_small = "images/small/"."small_".$file_name;

move_uploaded_file($_FILES["file"]["tmp_name"], $dir_full);

$im_src = imagecreatefromjpeg($dir_full);
$src_width = imageSX($im_src);
$src_height = imageSY($im_src);

$dst_width = 50;
$dst_height = ($dst_width/$src_width)*$src_height;

$im = imagecreatetruecolor($dst_width,$dst_height);
imagecopyresampled($im, $im_src, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height);

imagejpeg($im, $dir_small);

$temp = "http://*my_ip*/TA/php/images/full/"."full_".$file_name;
$temp2 = "http://*my_ip*/TA/php/images/small/"."small_".$file_name;
//$id_place = .$id_place;
$query = "insert into gallery values ('','$id_place','$temp','$temp2')";
mysql_query($query);

imagedestroy($im_src);
imagedestroy($im);

if i use ajax, the script.js will be like this (correct me if im wrong)

script.js with ajax

function uploadPhoto(imageURI) {
    jquery.ajax({
        type: 'GET',
        url: 'http://203.189.122.77/TA/php/gallery.php',
                    data: {id_place: window.localStorage("id_place")},
            dataType: 'jsonp',
            jsonp: 'jsoncallback',
            timeout: 5000,
                    success: function(data, status){
            //alert(window.localStorage.getItem("id_place"));
            var options = new FileUploadOptions();
                        options.fileKey="file";
            options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
            options.mimeType="image/jpeg";
            options.chunkedMode = false;

            var ft = new FileTransfer();
            ft.upload(imageURI, "http://203.189.122.77/TA/php/upload.php", win, fail, options, true);
        },
        error: function(){
            alert('There was an error when download images');
        }
    });

};

if i use ajax, there always be in error function.if im not use ajax, i cannot get id_place

8
  • You have a SQL injection vulnerability. Commented May 26, 2013 at 14:02
  • What does the error say? Commented May 26, 2013 at 14:03
  • That isn't actually JSONP. Commented May 26, 2013 at 14:06
  • @SLaks so what should i write in my js n php?? dont know what is the error.but it always appear (there was an error when download images).. and in database, everything is complete (id_place, temp, temp2) but the image is not uploaded.. but when im not use ajax, the file is uploaded but the id_place is missing in my database..it says "0" Commented May 26, 2013 at 14:09
  • Put a breakpoint within the error callback and look at the local variables. jquery's error handle functions can take arguments, the first of which is the XHR request object. Within the error function, output the XHR object to the console and look for the error. Commented May 26, 2013 at 14:15

1 Answer 1

1

With JSONP, the server does not return a regular JSON object, rather it returns a JavaScript function to be run on the client. Your client code then executes the function returned from the server for access to the data.

I believe another post: Simple jQuery, PHP and JSONP example? will explain how to do this in your context.

To understand the mechanics of JSONP more generally, http://en.wikipedia.org/wiki/JSONP might be of use.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.