POST vars from flash to php file_put_contents filename - php

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' ]);

Related

Error when Passing Array from Php to AS3

I am new to AS3, and I had tried a few times to pass an array from php to AS3. But i can't manage to do it.
But i managed to narrow down the problem to 1 set of code, so wondering what do i need to change it.
When the function is this
function Asandler(event:Event){
var responseVariables:URLVariables = new URLVariables(event.target.data);
nobed = responseVariables.nobed ;
zip = responseVariables.zip;
Location = responseVariables.Location;
price = responseVariables.price;
}
It returns an error Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
But when i change it to
function Asandler(event:Event){
s1.test.text=event.target.data
}
It displays array with no problem, inside the dynamic text field.
php echo part
$solutions = array();
while ($row = mysqli_fetch_assoc($sql))
{
echo "nobed=".$solutions[1]=$row['nobed'];
echo "&zip=".$solutions[2]=$row['zip'];
echo "&Location=".$solutions[3]=$row['Location'];
echo "&price=".$solutions[4]=$row['price'];
}
Test Data string
nobed=100&zip=100&Location=100&price=100
New try, testing it with dynamic text field, it display the whole string.
var receivedValue:String = event.target.data.replace(/^\s+|\s+$/mg, "");
var test:Array = receivedValue.split(",");
s1.test.text =test[0];
But not too sure how to split the string up.

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.

mysql php select random with javascript rotator

I just started learning PHP, and have been trying to build a website to learn. I found a javascript script that rotates text on the internet here which looks like:
<script language="JavaScript">
function rotateEvery(sec)
{
var Quotation=new Array()
// QUOTATIONS
Quotation[0] = 'First quotation';
Quotation[1] = 'Second quotation';
Quotation[2] = 'Third quotation';
Quotation[3] = 'Fourth quotation';
Quotation[4] = 'Fifth quotation';
Quotation[5] = 'Sixth quotation';
Quotation[6] = 'You can add <b>as many</b> quotations <b>as you like</b>';
var which = Math.round(Math.random()*(Quotation.length - 1));
document.getElementById('textrotator').innerHTML = Quotation[which];
setTimeout('rotateEvery('+sec+')', sec*1000);
}
</script>
I also have a database table called events that has three fields ( id, when, tag) When is a date, tag is the description of the event (e.g Christmas Party/Halloween at my house).
What i am trying to do is select the events that are happening today and put them in my javascript rotator, randomly.
Is this possible? How would I go about implementing this? I know I am really bad at explaining my questions so if I left out any more details if you could just tell me and I can help.Thanks!
So if I understand your intent, you want to pull events from your database and pass them into the JavaScript on your page to use in your rotator:
In your PHP
Use whichever MySQL API you are using already to execute the query. Using the old mysql_*() functions would look like the following. (Note: use of the mysql_*() functions is actually NOT recommended, but it seems most likely that's what you're currently using. I'll update if I find out otherwise...)
// Assuming `when` is a real DATE or DATETIME data type in MySQL...
// compare to CURDATE() to get today's
$result = mysql_query("SELECT tag FROM events WHERE DATE(when)=CURDATE()");
if ($result) {
// array to hold all the output
$events = array();
while ($row = mysql_fetch_assoc($result)) {
// Add the event to your array
$events[] = $row['tag'];
}
// After building the array, encode it as JSON
// Later you'll echo this into your JavaScript in place of the array...
$events = json_encode($events);
}
Later in your HTML/JavaScript output
Output the JSON string (your array) into the JavaScript on your page:
function rotateEvery(sec)
{
// The JSON from PHP output here
// Would look something like
// ["Event 1","Event 2","Event 3"]
var Quotations = <?php echo $events; ?>;
var which = Math.round(Math.random()*(Quotation.length - 1));
document.getElementById('textrotator').innerHTML = Quotation[which];
setTimeout('rotateEvery('+sec+')', sec*1000);
}

Sending and receiving data

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();
}

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; ?>';

Categories