Can anyone tell me how to retrieve the response from the php file im posting to and then send them to a js function please.
I also have a animated gif image in the script which doesnt show the animation. Can anyone get this working too?
the php response is in this format variable1=blabla&varaibale2=blabla
function sendPixelData(e:MouseEvent):void {
capture_mc.visible = false;
send_mc.visible = false;
txtReturn.htmlText="Uploading Image......<img src="loading.gif" /><br/> Please Wait!";
var output:String = "";
var col = "";
for (var i:Number=0; i<bitmapData.height; i++) {
for (var j:Number=0; j<bitmapData.width; j++) {
col = bitmapData.getPixel(j,i).toString(16);
// In some cases, the color will be truncated (e.g. "00FF00" becomes "FF00")
// so we are adding the missing zeros.
while (col.length<6) {
col = "0" + col;
}
output += col;
}
}
var request:URLRequest = new URLRequest("GetPixelData.php");
var variables:URLVariables = new URLVariables();
var myURLLoader:URLLoader = new URLLoader();
variables.pixels=output;// all image pixel
//trace("output" + output)
variables.width=videoPreviewWidth;// video width
variables.height=videoPreviewHeight;// video height
request.data=variables;
request.method=URLRequestMethod.POST;
myURLLoader.addEventListener ("complete", onC);
myURLLoader.load (request);
function onC (e) {
var result:String =
ExternalInterface.call( "redirectToURL(send variables here)" );
trace ("save complete");
}
}
The result from PHP is stored in the data property of the URLLoader, so to retrieve it use:
function onC (e:Event) {
var result:String = String(e.target.data);
...etc
For part 2 of your question, Flash doesnt support gif file format - you need to use jpg or png.
BTW, dont use strings for event types - use the event type constant, eg:
myURLLoader.addEventListener (Event.COMPLETE, onC);
Related
Trying to parse the input JSON from the Woocommerce Webhook to Google Spreadsheet via Google App Script.
Used this one :
function doPost(request) {
var json = request.postData.getDataAsString();
var obj = JSON.parse(json);
// getting some of the Woocommerce data just as an example
// Hook was fired after order.created
var id = obj.order.id;
var orderNumber = obj.order.order_number;
var payMethod = obj.order.payment_details.method_title;
// write data in a document, not useful, but straightforward for testing
var doc = DocumentApp.openById('myDocumentId');
doc.appendParagraph("Id: " + id);
doc.appendParagraph("orderNumber: " + orderNumber);
doc.appendParagraph("payMethod: " + payMethod);
}
But receive nothing into the Google Sheets.
And with this one:
function doPost(request) {
var content = JSON.parse(request.postData.contents);
var row = [];
for (var elem in content) {
row.push(content[elem]);
}
var ss = SpreadsheetApp.openById("SHEET ID")
var sheet = ss.getSheetByName("Sheet1");
sheet.appendRow(row);
var result = {"result":"ok"};
return ContentService.createTextOutput(JSON.stringify(result))
.setMimeType(ContentService.MimeType.JSON);
}
It's receiving data, but it's not parsed:
Is there anyway to fix this and make the data in sheet viewable?
Thanks in advance.
I have found the answer on my question at https://ru.stackoverflow.com/
thanks to Alexander Ivanov
The main thing why the woocommerce webhook not parsed is that the JSON is not valid when WC send it to the spreadsheet macros.
And sheet posting it as one element {order:{}}
so we need to edit the code like this :
var content = JSON.parse(request.postData.contents)[0];
or like this (in my case):
var content = JSON.parse(request.postData.contents)['order'];
in case we have no idea what data will be received, we may try to determine the value:
function doPost(request) {
var result = {
result: undefined
};
try {
var content = JSON.parse(request.postData.contents);
var row = [];
if (content.hasOwnProperty('order')) {
for (var elem in content['order']) {
row.push(content['order'][elem]);
}
} else {
row.push(request.postData.contents);
}
var ss = SpreadsheetApp.openById('SHEET ID')
var sheet = ss.getSheets()[0];
sheet.appendRow(row);
result.result = 'ok';
} catch (err) {
result.result = 'err';
}
return ContentService.createTextOutput(JSON.stringify(result))
.setMimeType(ContentService.MimeType.JSON);
}
I'm new to PHP, but I'm developing in Visual Studio LightSwitch.
I'd like to pass several javascript variables from a data record to a PHP script during a javascript save event.
What are my options?
Your options are limited to ajax, or an image request.
Option 1. AJAX
function success() {
console.log(this.responseText);
}
var ajax = new XMLHttpRequest();
ajax.onload = success;
// myVars is a string containing the values you need to record. In the format
// of ?var=value&var1=value&var2=value
ajax.open("get", "/record.php?" + myVars, true);
ajax.send();
Option 2. Image
var a = document.getElementById('my-button');
a.addEventListener('click', function(e) {
var target = e.target || e.srcElement;
var img = new Image();
// get your variables values and create a url here.
img.src = 'http://myserver.tld/record.php?' + myVars;
});
I'm working on a facebook tab that accepts some text and also a picture using an html fileUpload component, the idea is that when the user selects a picture in the fileUpload component, the image they select appears on the page as a way of previewing the picture before uploading it to the server. I tried fectching the image url using val(), but for security reasons, browsers do not give the complete url of a local file. Is there a way to do this using either php or jquery?
Thanks
I believe this is what you're looking for:
function handleFiles(files) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
var imageType = /image.*/;
if (!file.type.match(imageType)) {
continue;
}
var img = document.createElement("img");
img.classList.add("obj");
img.file = file;
preview.appendChild(img);
var reader = new FileReader();
reader.onload = (function(aImg) { return function(e) { aImg.src = e.target.result; }; })(img);
reader.readAsDataURL(file);
}
}
You might also be interested in reading other examples, like this one.
Edit: This method relies on the FileReader API, so it won't work on IE9 as you've pointed out, but I don't think it is possible otherwise.
At the end of the day, do you need IE9 compatibility ? You might find it acceptable to add a functionality that works for all but a few percent of your user base.
I'm using Code Igniter and the Googlemaps library. This library generates a lot of Javascript code dynamically, including the contents of the InfoWindows for each new marker, but I'd like to keep that in a separate template file, like a regular View.
I have this Javascript code (from Googlemaps' library):
var lat = marker.getPosition().lat();
var long = marker.getPosition().lng();
var windowContent = "";
if( _new ) {
var newIW = new google.maps.InfoWindow( { content: windowContent } );
What I want to do is to load windowContent from a template file. I have already succeeded in dynamically generating a form for this variable and using lat and long variables defined just above, but how can I achieve this in Code Igniter? I can't use load->view because I'm not in a Controller's context. And I cannot use include() or readfile() either because of CI's security constraints.
Any hints?
Using pure javascript, get the lat and long, make a url with the lat and long in the query string, and use xhr to do the ajax call.
var lat = marker.getPosition().lat();
var long = marker.getPosition().lng();
var xhr;
var url = "http://myurl.to/script.php?lat="+lat+"&lng="+long;
if(typeof XMLHttpRequest !== 'undefined')
xhr = new XMLHttpRequest();
else {
//Get IE XHR object
var versions = ["MSXML2.XmlHttp.5.0",
"MSXML2.XmlHttp.4.0",
"MSXML2.XmlHttp.3.0",
"MSXML2.XmlHttp.2.0",
"Microsoft.XmlHttp"];
for(var i = 0, len = versions.length; i < len; i++) {
try {
xhr = new ActiveXObject(versions[i]);
break;
}
catch(e){}
}
}
xhr.onreadystatechange = function(){
//This function is called every so often with status updates
//It is complete when status is 200 and readystate is 4
if(xhr.status == 200 && xhr.readyState === 4) {
//Returned data from the script is in xhr.responseText
var windowContent = xhr.responseText;
//Create the info window
var newIW = new google.maps.InfoWindow( { content: windowContent } );
//Pass newIW to whatever other function to use it somewhere
}
};
xhr.open('GET', url, true);
xhr.send();
if using a library like jQuery it would be like
var lat = marker.getPosition().lat();
var long = marker.getPosition().lng();
var url = "http://myurl.to/script.php";
jQuery.ajax({
"url":url,
"data":{ //Get and Post data variables get put here
"lat":lat,
"lng":long
},
"dataType":"html", //The type of document you are getting, assuming html
//Could be json xml etc
"success":function(data) { //This is the callback when ajax is done and successful
//Returned data from the script is in data
var windowContent = data;
//Create the info window
var newIW = new google.maps.InfoWindow( { content: windowContent } );
//Pass newIW to whatever other function to use it somewhere
}
});
I have written a code in javascript which creates a p attribute dynamically along with a button which will remove the content. The function removeValue works fine in Chrome and Firefox but does not work in IE. Also, the setAttribute('style','') is not working in IE either. Lastly when I send the values to another page using window.location it sends undefined instead of the text.
Everything seems to work fine in Firefox and Chrome but I can't get it to work in IE (Currently using IE 7). How can I solve this issue?
The code:
function removeValue(ob)
{
ob.parentNode.parentNode.removeChild(ob.parentNode);
}
function throwval(obj)
{
var sent_id = obj.id; //get id of the button
var v = document.getElementById(sent_id).value;
var newp = document.createElement("p"); //create a new <p> tag
var text = document.createTextNode(v);
var buttonnode= document.createElement('input');
buttonnode.setAttribute('type','button');
buttonnode.setAttribute('name','del');
buttonnode.setAttribute('value','Remove');
buttonnode.setAttribute('style','background-color: Transparent;width: 125;color: blue;border:0');
buttonnode.setAttribute('onclick','removeValue(this)');
newp.appendChild(text);
newp.appendChild(buttonnode);
document.getElementById("getselected").appendChild(newp); //append the new <p> tag in the div
}
function sendvalues()
{
var div_val = document.getElementById("getselected");
if(!div_val.getElementsByTagName("p").length)
{
alert("Select a value");
}
else
{
//get seperate values of the paragraph inside div
var str="|";
for (i=0; i < div_val.getElementsByTagName("p").length; i++)
{
var paragraphs = div_val.getElementsByTagName("p");
if(!paragraphs.item(i).textContent)
{
var pvalues = paragraphs.item(i).innerText;
}
else
{
var pvalues = paragraphs.item(i).textContent;
}
//var sendpvalues = "products=" + pvalues;
// alert(pvalues);
str = str + pvalues + "|";
//alert (str);
//ajaxOb.send(sendpvalues);
}
// alert(str);
window.location="send_data.php?str="+str;
}
}
Turns out that IE supports 'innerText' and firefox supports 'textContent'. I fixed the undefined issue by using 'if' statement. Code updated
IE has an issue with the setAttribute function and event handlers. (Read here.) Use the onclick property in addition to setAttribute.
buttonnode.setAttribute('onclick','removeValue(this)');
buttonnode.onclick = function() { removeValue(buttonnode); };