This topic is locked
[SOLVED]

  Pass variable back from Javascript

2/9/2012 10:59:43 AM
PHPRunner General questions
P
Pauldee author

Hi
I am struggling to work how how to pass a variable created in an external Javascript page back to PHP Runner.
The User inputs a Postcode which is passed to a Javascript page which looks up the the street relating to the postode from an external server.
A variable is created in Javascript which needs to be passed back to an Address field in PHP Runner.
The code being used is:
//HTML - Input box for postcode

<INPUT onblur="javascript:SPLGetAddressData(document.getElementById('postcode').value)" name=postcode
//Javascript Snippet - process looks up the address from the postcode and creates the MultiLineAddress variable

if (COUNTRY!="") {MultiLineAddress = MultiLineAddress + COUNTRY + "\n"}

document.getElementById("memo").value=MultiLineAddress;
The "memo" field does not exist on my page but I have left this there as in a normal HTML page this is how the variable is passed back to the page.
//PHP Runner Javascript OnLoad event

var ctrl = Runner.getControl(pageid, 'Surname');

var ctrlAddress = Runner.getControl(pageid, 'Address');
function func() {

ctrlAddress.setValue(MultiLineAddress)

};
ctrl.on('blur', func);
This is where my problem is. How do I pass the variable 'MultiLineAddress' back to my 'address' field in PHP Runner?
Thanks for any help offered.
Paul

C
cgphp 2/9/2012

Please, post the full code in a well formatted way using the code snippet (<>).

P
Pauldee author 2/9/2012

I have added custom HTML into my form as follows which creates a text field for the user to input a postcode, when the text field loses focus the Javascript is called and the postcode is passed to the function:



<INPUT onblur="javascript:SPLGetAddressData(document.getElementById('postcode').value)" name=postcode

<TD><DIV id=SPLSearchArea></DIV></TD>


The Javascript all works as expected and retrieves address data from the postode that the the user had input and builds a string called MultiLineAddress with the following code:



if (COMP!="") {MultiLineAddress = MultiLineAddress + COMP + "\n"}

if (LINE1!="") {MultiLineAddress = MultiLineAddress + LINE1 + "\n"}

if (LINE2!="") {MultiLineAddress = MultiLineAddress + LINE2 + "\n"}

if (LINE3!="") {MultiLineAddress = MultiLineAddress + LINE3 + "\n"}

if (TOWN!="") {MultiLineAddress = MultiLineAddress + TOWN + "\n"}

if (COUNTY!="") {MultiLineAddress = MultiLineAddress + COUNTY + "\n"}

if (POSTCODE!="") {MultiLineAddress = MultiLineAddress + POSTCODE + "\n"}

if (COUNTRY!="") {MultiLineAddress = MultiLineAddress + COUNTRY + "\n"}

document.getElementById("address").value=MultiLineAddress;


There is too much code to add here but everything works fine up to this point. MultiLineAddress now contains an address.
My problem lies in passing the string MultiLineAddress back to a PHP Runner database field called 'address', I just do not know what the syntax is to do this.
Thanks

Paul

P
Pauldee author 2/10/2012

Ok, I have now worked this one out myself!
In PHP Runner 'Javascript OnLoad Event', I needed to define my address field as a Global Variable (as apposed to using var ctrl):



ctrl = Runner.getControl(pageid, 'Address');


Then in the external Javascript, pass the MultiLineAddress variable to my Global Variable:



if (COMP!="") {MultiLineAddress = MultiLineAddress + COMP + "\n"}

if (LINE1!="") {MultiLineAddress = MultiLineAddress + LINE1 + "\n"}

if (LINE2!="") {MultiLineAddress = MultiLineAddress + LINE2 + "\n"}

if (LINE3!="") {MultiLineAddress = MultiLineAddress + LINE3 + "\n"}

if (TOWN!="") {MultiLineAddress = MultiLineAddress + TOWN + "\n"}

if (COUNTY!="") {MultiLineAddress = MultiLineAddress + COUNTY + "\n"}

if (POSTCODE!="") {MultiLineAddress = MultiLineAddress + POSTCODE + "\n"}

if (COUNTRY!="") {MultiLineAddress = MultiLineAddress + COUNTRY + "\n"}



ctrl.setValue(Address1.value);


The address MultiLineAddress variable is immediately passed to my Address field and all is well!
Thanks to anyone that may had been currently considering this problem. If there a better way of doing this, please let me know.
Paul

W
wildwally 2/10/2012

I'd like to do something similar with a site that I'm working with, and wondering if you could provide any more details in the process.
I want my user to enter a zipcode which i have in a table. once entered i want to search for all city names that the zipcode corrisponds to and return the results for the user to select the correct city. Then pass the city, state and zipcode back to my main page that started the process. This helps with spelling errors/variations of the cities and make future data mining much more managable.
Thanks in advance for any help you could offer.

P
Pauldee author 2/10/2012

I use a service called Postcodelite (postcodelite.com) which, I believe, is only available for UK postcodes, this is also a subscription based service. They provide all the Javascript from their website to look up address data from their servers.
An internet search finds providers in the US.
I am looking into whether Google provide the street level data as a free service. I currently get the longitude and latitude of an address through Google geocode.
Will post any info here.
Paul