Sending and receiving data - php
Not getting data back into flash from php that queries mysql data, think the problem is with my as3 code here?
The php works, the as3 posts to the php ok, its the returning of the variables to as3 that I am unsure about and seems to be the problem?
public static function MineData():void{
var myRequest:URLRequest = new URLRequest("login.php");
var myLoader:URLLoader = new URLLoader();
myLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
var myVariables:URLVariables = new URLVariables();
myVariables.School_name_test = String(PostToPHP3.Temp_flash_TI_School_name_test);
myRequest.method = URLRequestMethod.POST;
myRequest.data = myVariables;
function onLoaded(event:Event) {
var myURLVariables:URLVariables = new URLVariables(event.target.data);
DT_display_string_teacher_login_teacher_first_name = myURLVariables.mined_teacher_first_name;
Main.listeningFORPortalteacherlogin.tellMainPortalteacherlogin();
}
myLoader.addEventListener(Event.COMPLETE, onLoaded);
myLoader.load(myRequest);
}
Kindest regards
Within your PHP file, ensure you're only echoing back the values which should be returned to Flash. The format for this returned string is:
VarName=Value
With any further values appended to the same String using the & operator to separate them:
VarName=Value&VarName2=Value2
And so on. See the following example for how you might need to use it:
<?php
echo "mined_teacher_first_name=" . $FIRST_NAME_RETURNED_FROM_SQL;
?>
This example obviously doesn't include all the PHP to retrieve the data from the database but that's how you get the data back to Flash. If you have a very large amount of data, you can also output an XML file from PHP and then parse that from within Flash.
I'll also add that your Flash code for extracting these values looks perfectly fine, although it's not absolutely necessary to convert them to a URLVariables object, you can in fact, access them directly from event.data if you so choose:
function onLoaded(event:Event) {
DT_display_string_teacher_login_teacher_first_name = event.target.data.mined_teacher_first_name;
Main.listeningFORPortalteacherlogin.tellMainPortalteacherlogin();
}
Related
Encoding variables for Google Maps geocoder with PHP
I am trying to encode variables for the URL for the google maps geocoding API with PHP. I have used both urlencode and rawurlencode and I get the same result for Székesfehérvár, Hungary PHP returns Sz%E9kesfeh%E9rv%E1r%2C%20Hungary Which cannot be decoded by any of the online decoders. When I encode with the online encoder tools I get: Sz%C3%A9kesfeh%C3%A9rv%C3%A1r%2C+Hungary When I plug that in directly to the Google Maps geocoder URL, it is successful. The PHP function fails every time. This is the code I am using: function getLatandLong($landmark,$city,$state) { global $lat; global $long; global $city; if (!empty($landmark)) { $encoded_vars = "$landmark,$city,+$state"; } else { $encoded_vars = "$city, $state"; } $encodedVars = urlencode($encoded_vars); $doc = new DOMDocument(); $doc->load("http://maps.google.com/maps/api/geocode/xml?address=$encodedVars&sensor=false"); //input address
Well, I tried $encodedVars = utf8_encode($encoded_vars); And that seems to be working, even though the echo shows (probably because I don;t have the right page encoding set on my catch page: Székesfehérvár,+Hungary
not receiving JSON from php with getJSON
I am trying to pass an array to the browser using php and jquery but I the when I try to use the 'data' returned from php's encode_json, it comes up undefined. I'm just learning php, jquery, and json and so far haven't found very good documentation on alot of this stuff, especially json, even in the books I have. Thanks in advance! Here is a stripped down version of the jquery I have $(document).ready(function(){ var jsonResult;//I will want to be able to use the data in other functions $.getJSON("json.php", function(data){ jsonResult = data; var str; var nuts = [203,204,205,207]; str = '<p>' + data[nuts[0]].NutraDesc + '</p>'; $('#stuff').html(str); } ); }); This is the php: include_once 'databasePHP.php'; $json_tst = $db->query( "SELECT def.Nutr_No, NutrDesc, Nutr_Val, Units FROM nutr_def as def JOIN nut_data as data ON def.Nutr_No = data.Nutr_No WHERE data.NDB_No = 1001 LIMIT 0, 2"); $food = array(); while($row = $json_tst->fetch(PDO::FETCH_ASSOC)) { $Nutr_No = $row['Nutr_No']; $food[$Nutr_No][] = array( 'NutrDesc' => $row['NutrDesc'], 'Nutr_Val' => $row['Nutr_Val'], 'Units' => $row['Units'] ); }; echo json_encode($food); ?> which returns this json which I checked on jsonlint.com and it said it was valid: {"203":[{"NutrDesc":"Protein","Nutr_Val":"0.85","Units":"g"}],"204":[{"NutrDesc":"Total lipid (fat)","Nutr_Val":"81.11","Units":"g"}]}
It probably doesn't work because the numbers should be strings. Try to add quotes around the numbers in nuts, like this: var nuts = ["203","204","205","207"]; The following probably works as well: str = '<p>' + data[String(nuts[0])].NutraDesc + '</p>'; Also, have you tried adding console.log(data); to the getJSON function to make sure it receives the JSON? EDIT: Here is a working JSFiddle from your code: http://jsfiddle.net/rKLqM/ Things that were wrong: you weren't parsing the result as JSON (JSON.parse) NutraDesc was spelled wrong You didn't convert the numbers to strings You needed to add [0] to the jsonResult because there's an extra array within it (see the [])
In Javascript object property can be accessed with obj["propName"] So, change var nuts = [203,204,205,207]; to var nuts = ["203","204","205","207"];
Ajax - PHP associative array into jquery associative array via ajax/json
I have the following hardcoded into jquery and I want to move the code over to pull the values from a database using ajax. I get the data back and pass it through using json_encode but I need to keep the same format. codes['851'] = new Array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208'); codes['852'] = new Array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208'); codes['522'] = new Array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','120','205','206','207','208','209'); Here is the php array prior to json_encode. $codes = array(); codes['851'] = array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208'); codes['852'] = array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208'); codes['522'] = array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','120','205','206','207','208','209'); I am trying to keep the same format as I do not want to rewrite all the other code in the script. Is it possible to match format?
if I understand right, you need format like this in your ajax responce. codes['851'] = new Array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208'); codes['852'] = new Array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208'); codes['522'] = new Array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','120','205','206','207','208','209'); for PHP you need next: $codes = array(); $codes['851'] = array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208'); $codes['852'] = array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208'); $codes['522'] = array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','120','205','206','207','208','209'); echo 'codes='.json_encode($codes).';'; this is not similar visual, but equal in JS Object structure.
So the issue was in the ajax code, I had code outside of my ajax call that wasn't being called. After I moved the methods inside the success callback of the ajax call it all worked perfectly.
POST vars from flash to php file_put_contents filename
I'm recording webcam data and sending to a .php to be stored and viewed later within the flash. That element is working fine but I need to give the file a unique ID each time it does this based on variables set in flash. here is the code I'm using to POST the flv as a byteArray: var url_ref:URLRequest = new URLRequest("save_vid.php"); url_ref.contentType = 'application/octet-stream'; url_ref.data = _baFlvEncoder.byteArray;//url_data; url_ref.method = URLRequestMethod.POST; urlLoader.dataFormat = URLLoaderDataFormat.BINARY; try { urlLoader.load( url_ref ); } catch (e:Error) { trace(e); } The variables I need to append to the filename set in PHP file: var currentVideo:String; var currentName:String; My PHP file so far: <?php echo 'Data:<pre>'; print_r($_POST); echo '</pre>'; file_put_contents("test.flv",$GLOBALS[ 'HTTP_RAW_POST_DATA' ]); $sCurrentVideo = $_POST['currentVIdeo']; $sCurrentName = $_POST['currentName']; ?>++ Can anyone here lead me in the right direction? Thanks in advance.
You will want to use the "URLVariables" class. Here is a helpful tutorial explaining it's usage. Create an instance of it, set the values to match the two variables you want set, also add in the raw video data. // Define the variables to post var urlVars:URLVariables = new URLVariables(); urlVars.currentVideo = currentVideo; urlVars.currentName = currentName; urlVars.videoData = _baFlvEncoder.byteArray; Then in the urlReq, set the data equal to the urlVars instance: url_ref.data = urlVars; You will then be able to access these values in the $_POST variable in PHP. file_put_contents("test.flv",$_POST[ 'videoData' ]);
JSON object "undefined" error in Javascript
I am uploading a file using PHP and want to return the file name and the file status to javascript. In PHP I create the json object by: $value = array('result' => $result, 'fileName' => $_FILES['myfile']['name']); print_r ($value); $uploadData = json_encode($value); This creates the json object. I then send it to a function in javascript and recieve it as a variable called fileStatus. alert (fileStatus); It displays {"result":"success","fileName":"cake"} which should be good. But when I try and do fileStatus.result or fileStatus.fileName I get an error saying that they are undefined. Please help I'm really stuck on this. Thanks.
The fileStatus is just a string at this point, so it does not have properties such as result and fileName. You need to parse the string into a JSON object, using a method such as Firefox's native JSON.parse or jQuery's jQuery.parseJSON. Example: var fileStatusObj = jQuery.parseJSON(fileStatus);
If the alert displays {"result":"success","fileName":"cake"} then you probably still have to turn the string into a JSON object. Depending on the browsers you are developing for you can use the native JSON support or the JSON.org implementation to turn your string into an object. From there on it should work as expected.
When you are setting the variable, do not put quotes around it. Just set the variable like this: var fileStatus = <?php echo $uploadData; ?>; or: var fileStatus = <?=$uploadData?>; Do not do this: var fileStatus = '<?php echo $uploadData; ?>';