简体   繁体   中英

send data from javascript to php (phonegap android)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM