carolina-bound Posted October 19, 2014 Share Posted October 19, 2014 Where is my "Progr" return value? This is not my first day using php but I don't code in the language that often. I don't understand why my dynamic variable is not returned in AJAX GET? Essentially, I need to give the client a simple UI so that they can copy files to a printer and I wanted to provide a little unfiltered feedback so they knew whether operation was a success. My feedback was just going to be output of exec() function though cleaned up slightly (perhaps replace line breaks with html break tags). PHP version is 5.2.17.17 and cannot be upgraded. Web Server Apache. PHP dumbed down code: exec("LoadZPL_PURL.bat", $output); $results = implode("|", $output); $results = 'BEFORE'.substr($results, 4, 5).'AFTER'; echo "$results"; Results when run from command prompt give expected results of "BEFOREProgrAFTER". I just grabbed the substring here for a simple test, so that carriage returns and escape characters don't come into play. Results viewed in fiddler2 only give the following. Where is my "Progr"? HTTP/1.1 200 OK Content-Type: text/html Server: Microsoft-IIS/7.5 X-Powered-By: PHP/5.2.17 X-Powered-By: ASP.NET Date: Sun, 19 Oct 2014 18:58:35 GMT Connection: close Content-Length: 11 BEFOREAFTER I can post the AJAX code if really needed, I don't think issue resides here per fiddler2 trace, but here is fiddler2 request though I removed ip and a few client specific directory names. I haven't had problems with http requests in the past though I have never used in this fashion as I usually return record(s) from a table. GET http://x.x.x.179/custom/abc/reupload/LoadPrinterGraphic.php?¶m1=document.getElementById(uncShare').value¶m2=0.31039151530034853 HTTP/1.1 Accept: */* Accept-Language: en-us Referer: http://x.x.x.179/custom/abc/reupload/abc_populate_printer_purolator.php Accept-Encoding: gzip, deflate User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0) Host: x.x.x.179 DNT: 1 Connection: Keep-Alive Cookie: PHPSESSID=0e4c2a01916297affd1df9e737283889 I dumbed down bat file for replication purposes. Bat file currently contains: copy plogo.grf plogo2.grf Quote Link to comment https://forums.phpfreaks.com/topic/291931-ajax-call-not-returning-dynamically-assigned-variable-string/ Share on other sites More sharing options...
ginerjm Posted October 19, 2014 Share Posted October 19, 2014 (edited) This code: $results = implode("|", $output); $results = 'BEFORE'.substr($results, 4, 5).'AFTER'; echo "$results"; is confusing to me. You are taking $output and assembling the elements of that array into a pipe-separated string. Then you are grabbing 5 chars from the 5th position and returning it in the $results var. Really? How do you know what is going to be there from the array? Have you looked at $output? Edited October 19, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/291931-ajax-call-not-returning-dynamically-assigned-variable-string/#findComment-1494180 Share on other sites More sharing options...
carolina-bound Posted October 19, 2014 Author Share Posted October 19, 2014 I am eventually going to take the entire $output though replace carriage returns (or pipes in case of current code) with html break tags. Current pipe you see is legacy code I was testing with earlier. I wanted to make sure that issues were not being introduced due to carriage returns and escape characters in current $output so I just grabbed a small subset of data for my testing of passing data from php page to javascript ajax call. I then added "BEFORE" and "AFTER" as I still was not seeing anything passed in fiddler2. Quote Link to comment https://forums.phpfreaks.com/topic/291931-ajax-call-not-returning-dynamically-assigned-variable-string/#findComment-1494182 Share on other sites More sharing options...
ginerjm Posted October 19, 2014 Share Posted October 19, 2014 (edited) So - what is in $output? Simply echo that back to the caller and then have the caller display it in an alert. Better yet - I like to do this with any ajax-called script - is to just run the script from the browser address bar as if you were calling it from JS and add some debugging echos to do your testing. Edited October 19, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/291931-ajax-call-not-returning-dynamically-assigned-variable-string/#findComment-1494183 Share on other sites More sharing options...
carolina-bound Posted October 19, 2014 Author Share Posted October 19, 2014 (1) When running code as a php.exe application via a command prompt output is "BEFOREProgrAFTER". (2) When running code as a javascript AJAX call to same php page in item (1) output is "BEFOREAFTER" Quote Link to comment https://forums.phpfreaks.com/topic/291931-ajax-call-not-returning-dynamically-assigned-variable-string/#findComment-1494184 Share on other sites More sharing options...
carolina-bound Posted October 19, 2014 Author Share Posted October 19, 2014 I put in some logs which when run as AJAX call I can see that something is not quite right as test.txt has "BEFOREAFTER". So issue is not with return value. For some reason I am not kicking off bat file or something similiar, perhaps permission issues. I need to look at further. Apologies, I have actually spent a good amount of time on issue, but it appears that I am chasing shadows at the moment. exec("LoadZPL_PURL.bat", $output); $results = implode("|", $output); $results = 'BEFORE'.substr($results, 4, 5).'AFTER'; $myfile = fopen("c:\\temp\\test.txt", "w"); $txt = "$results"; fwrite($myfile, $txt); fclose($myfile); echo "$results"; Quote Link to comment https://forums.phpfreaks.com/topic/291931-ajax-call-not-returning-dynamically-assigned-variable-string/#findComment-1494186 Share on other sites More sharing options...
ginerjm Posted October 19, 2014 Share Posted October 19, 2014 1 - please the proper code tags when posting code here 'square bracket code square bracket' and 'squarebracket /code square bracket' 2 You really don't need to put quotes around your var names in your usage. $myfile = fopen("c:\\temp\\test.txt", "w"); $txt = $results; // NO QUOTES NEEDED. WHY THIS LINE? AND WHERE IS $RESULTS SET? fwrite($myfile, $txt); fclose($myfile); echo $results; // NO QUOTES NEEDED I notice you are using back slashes in your path and referencing your C drive. Are you not running on the web? Can you show us the url that you are calling your php script with? Quote Link to comment https://forums.phpfreaks.com/topic/291931-ajax-call-not-returning-dynamically-assigned-variable-string/#findComment-1494193 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.