Jump to content

AJAX call not returning dynamically assigned variable string


carolina-bound
 Share

Recommended Posts

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.
 
Accept: */*
Accept-Language: en-us
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
 
 
Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

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"; 
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.