3.2.0

AW.HTTP.Request

My problem is getting an AW.INPUT text box, that is displaying a "document number" retrieved from a database, to refresh and get the next available number after I have posted a AW.HTTP.Request, which updates a database & increments the document number.

I have a php function that gets the next available document number but I can't work out hot to get that to refresh, So... I have decided to try and sumbit the page after the AW.HTTP.Request.

No matter what I try I can not work out how to do it succesfully?

If I insert the submit after the AW.HTTP Request, it process the alert box with the response and updates the database, but fails to submit.

If I Insert the submit in the "Invoice.onControlClicked" confirm script, it sumbits and updates the database, but the page submit is before the update and the document number stays the same unless I manually submit...

Please help?


Here is a page with the some example code (with the submit in the Invoice.onControlClicked): -

<?php include ("dbconnect.php"); 	?>
<?Php include ("functions.inc.php"); 	?>
<Body>
<script src="runtime/lib/aw.js"></script>
<link href="runtime/styles/xp/aw.css" rel="stylesheet"></link>
<form name="form" action="test.php">
 <fieldset>
  <legend>Company Details</legend>
   <table>				 
    <tr><td>Company Name:</td><td>
      <script type="text/javascript">
       var companyName = new AW.UI.Input;
       companyName.setStyle("width","150px");
       document.write(companyName);
        
     </script></tr>
     <tr><td>Document:</td><td>
     <script type="text/javascript">
       var nextdoc = '<?php DocumentNext() ?>'
       var doc = new AW.UI.Input;
       doc.setControlText(nextdoc);
       doc.setStyle("width","60px");
       document.write(doc);
                             
    </script></tr>
      <tr><td>Description:</td><td>
      <script type="text/javascript">
      var desc = new AW.UI.Input;
      desc.setStyle("width","200px");
      document.write(doc);
      </script></tr>
      
</table>
 </fieldset>		
  </form>
   </Body>



<script>
var Invoice= new AW.UI.Button;
Invoice.setControlText("Invoice");
Invoice.setControlValue(5);
Invoice.onControlClicked = function(){
if (confirm("Are you sure you want to increment the document number?")) {
 alert("You were sure!") 
 Invoice.setControlValue(4);
 document.form.submit();

 }
 else
 {
 alert("You were not sure!") 
 }
 }	 
            
var Submit = new AW.UI.Button;
Submit.setControlText("Submit")
Submit.onControlClicked = function(){
    document.form.submit();
    }		 	

    
Invoice.onControlValueChanged = function(){  	
    var g = new AW.HTTP.Request;
             var document = '<?Php DocumentNextInv() ?>'
    g.setUR("HTTPResponse.php");
    g.setRequestMethod("POST");
    g.setParameter("Document", document);
    g.request();
    g.response = function(data){
       alert(data); // process response data
                }
    }

document.write(Invoice);
document.write(Submit);
</script>



Jez
April 21,
Sorry My Mistake on this section,

var nextdoc = 'DocumentNext() ?>'

Should of been,

var nextdoc = <? 'DocumentNextInv() ?>'
Jez
April 21,
Ok...

I've worked out that I can get incremented document number in the PHP response page (HTTPRepsonse.php).

In this page I query the database (after I have updated it) and set a variable which I can see when I echo it in back in a response alert window. The question is how do I retrieve the this value back into my orginal javascript page, where the HTTP.Request was sent from?

I have tried adding a script tag in HTTPRepsonse.php setting a Javascript variable to be the varaibale for the next document number, but when I alert this back I get nothing!!

So the question is what can you actually do with the response apart from alert it?

How do you get javascript variable values from the response?

I haven't tried XML yet?
Jez
April 21,
You should use eval() to evaluate the plain text returned by the response.
for example:
alert(eval('AW.version'))
April 21,
Thank you for response, but I have tried I am not exactly sure how it can be used.

When try your example it returns "2.02" which is ok. However if I try and changing

alert(eval('AW.version'))


to

alert(eval('nextdocument'))


which is variable I have set in the HTTPresponse.php I get nothing.

I have tried allsorts with no results. I also tried

alert(eval(data)


To see if I can see where AW.Version came from... Nothing?

Jez
April 21,
AW.version came from javascript (AW.js) (browser) and the browser can NOT see any PHP variable, so ,you should return the 'value' stored in 'nextdocument' variable or a reference to a javascript variable as string (plain text).
if your PHP response returns the string "var abc = 345;alert(abc);" and then use eval(data) will see an 345 alert.
April 21,
Sorry if I am being really bad here, but I have tried to that and it works (only if I exclude all other text from the response). But what can I do with it? I have tried to use that javascript value, in the page, i.e. alert(abc) and get nothing.

what I am looking for, is to return the a value into a javascript variable and then refresh the page/AW.INPUT control (with the new document number)..

Or even process the HTTP Request and then submit the page would be a step forward, although the whole point of Ajax is to not submit the page. So please feel free to offer advice on submitting the page after the HTTP.Request
Jez
April 21,
....If you do advice on the submit process, it needs to come from the same AW.Button control. I can easily have two buttons, but the concept/page allows the user to produce an invoice, so the AW.Button will invoice the page, update the database, print an invoice and then return back refreshed with the next document number.

Jez
April 21,
The reason for the alert(abc) on the page is because abc variable is defined inside the function and therefore is not visible from outside, but if you define a 'global' variable on the page and then asign a new value to it in the response everything seems easier.

instead of the simplistic
eval(data)
In your example could be:

companyName.setControlText(eval(data));
companyName.refresh();
//were data is a new name returned by PHP and refreshed via this request.

You can even return multiple values or arrays to be evaluated an if needed using json notation (there are examples in the forum, just search for aw.http.request of json)
Hope it helps
April 21,
Yes that really helped!!

I'm now using an Array as your have advised, plus I need more than value returned back from the HTTP.Request.

However,

If I create the Array manually it works. If I return the same text via the HTTP.Request result if fails?

Maybe this is where I use the eval() function?

This works (I'm bring back two values (document, Ledger) and updates the document number on the page.) :-

g.response = function(data){
var mydata  = new Array("131","145");
var nextDoc = mydata[0];
var nextled  = mydata[1];			
alert(mydata);
doc.setControlText(nextDoc);
doc.refresh();
}


This fails: -

g.response = function(data){
var mydata  = new Array(data);
var nextDoc = mydata[0];
var nextled  = mydata[1];			
alert("Array0: "+mydata[0]);
alert(mydata);
doc.setControlText(nextDoc);
doc.refresh();
}


Now if alert(data) it returns "131","145" which is exactly what works when I assign the Array manually. I have tried brackets.

I am almost there!! And learning everyday :)
Jez
April 22,
OK, basically you have two options (although arrays & string concatenation could be more complicate or confusing)

var mydata = new Array("131","145");
works, but you can't return a string that way without a common delimiter, so you can return a string like "['131','145']" and then evaluate it (converts to array) .
var mydata = eval(data);
.. or ... use a values separator character like:
"131;145" and then use split to make it an array also.
var mydata = data.split(';')

(just be carefull in the last case to not use a separator that values
might contain like comma (numeric punctuation) also '&' and others)

check that code do it with alert(mydata[0])
HTH
Carlos
April 22,
Thanks Carlos,

That almost worked. If I try it manually as follows: -

g.response = function(data){
alert(data);
var data = "['10','11']";
var mydata = eval(data);		  
alert(mydata[1]);
}


The alert(mydata[1] Returns 11 :)

When I try the HTTP.Response,

g.response = function(data){
alert(data);
var mydata = eval(data);		  
alert(mydata[1]);
}


alert(mydata) returns undefined :(

Here's my php echo line that is making up the response. This looks ok in the alert box? Ummm

echo '"['."'".$nextdoc."','".$nextled."'".']"';
Jez
April 22,
Ok.. I've got it!

g.response = function(data){
alert(data);
var mydata = data.split(";");		  
alert(mydata[1]);


php side,

echo $nextdoc.";".$nextled;



Excellent!! I have been battling with this for two days now, so thanks to Carlos and ??????. I've learnt a lot from this and my project can move forward in a lot's of area's because of it.

Thanks once again.

Jez
April 22,

This topic is archived.

See also:


Back to support forum