Run BASIC
« How to pass variable via URL »

Welcome Guest. Please Login or Register.
Dec 8, 2009, 6:02pm



Run BASIC
Run BASIC Community Wiki
Easy Web Programming with Run BASIC

Run BASIC :: Run BASIC Programming :: Run BASIC Coding Questions :: How to pass variable via URL
   [Search This Thread][Send Topic To Friend] [Print]
 AuthorTopic: How to pass variable via URL (Read 105 times)
Psycho
Full Member
***
member is offline

[avatar]



Joined: Jan 2008
Gender: Male
Posts: 157
Location: North Carolina, USA
Karma: 2
 How to pass variable via URL
« Thread Started on Nov 4, 2009, 10:47pm »

Hello everyone,

I've heard this mentioned and even used programs that included it in some fashion but I don't understand how to pass a variable to another RB program via the URL.

I understand UrlKeys$ gives the address from which the browser came so I could probably use word$ to determine the information that was at the end of the address if it in fact carried the variable information.
The part I really don't understand (unless I've already downplayed the first part) is how to include the information as the running program calls the next program ???

I really need this simplified if that's possible so I'll throw out some example code that someone may be able to help me understand with.

Let's say the following code is a program saved as passTrial1.
'passTrial1
textbox #wordtyped, "",15
print " ";
link #go, "Go", [go]
#wordtyped setfocus()
wait


[go]
wordToPass$= #wordtyped contents$()
'now how to add wordToPass$ along in URL??????
PassTrial2Add$="http://localhost:8008/seaside/go/?app=passTrial2"
html "<script language='javascript'>window.open('";PassTrial2Add$;"','new','')</script>"
end

It asks for a word, the user enters it, presses Go, then it calls the address for another program that is hosted and saved as passTrial2.
Now passTrial2 runs, gets the UrlKeys$ (I'm assuming is the way) and prints out the word the user typed in passTrial1
'passTrial2

'capture wordToPass$ from UrlKeys$???? How??? (can probably be done with word$)
print "You typed in ";wordToPass$
end


How does one go about doing that?

Any help would be appreciated.

John "Psycho" Siejkowski
Link to Post - Back to Top  IP: Logged
StefanPendl
Global Moderator
*****
member is offline

[avatar]

Run for BASIC ...



Joined: Aug 2007
Gender: Male
Posts: 555
Location: Austria
Karma: 4
 Re: How to pass variable via URL
« Reply #1 on Nov 5, 2009, 2:50am »

You would call the second application with
Code:
http://localhost:8008/seaside/go/runbasicpersonal?app=passTrial2&variable0=test&variable1=12


Inside passTrial2 UrlKeys$ would contain
Code:
app=passTrial2&variable0=test&variable1=12


You can simply check this by creating and publishing a project, which only contains the following
Code:
print
print UrlKeys$
end

« Last Edit: Nov 5, 2009, 2:51am by StefanPendl »Link to Post - Back to Top  IP: Logged

---
Stefan


Any code I post can be used by others in their own projects, there is no need to ask for permission, just give credit if you like

Run BASIC 1.01, FF3.5 (IE8), Windows Vista Home Premium SP2, AMD Turion X2 RM-70 2GHz, 4GB RAM
StefanPendl
Global Moderator
*****
member is offline

[avatar]

Run for BASIC ...



Joined: Aug 2007
Gender: Male
Posts: 555
Location: Austria
Karma: 4
 Re: How to pass variable via URL
« Reply #2 on Nov 5, 2009, 2:57am »

On the other hand, you could call the other project with the RUN command and render it too.

Parent Project
Code:
run "Child", #child
render #child


The above will create a separate object containing the project called "Child", which will act like a container, so anything you do in the child container will be executed in the child project.

You can still interact with the parent if need is.

There is a bug report, which is already fixed, which shows this way of parent/child interaction.
Link to Post - Back to Top  IP: Logged

---
Stefan


Any code I post can be used by others in their own projects, there is no need to ask for permission, just give credit if you like

Run BASIC 1.01, FF3.5 (IE8), Windows Vista Home Premium SP2, AMD Turion X2 RM-70 2GHz, 4GB RAM
votan
Senior Member
****
member is offline





Joined: Jul 2008
Gender: Male
Posts: 300
Karma: 3
 Re: How to pass variable via URL
« Reply #3 on Nov 5, 2009, 6:52am »

I always do it the following way..
I.e.: Your first program creates a link like this:
Code:
http://localhost:8008/seaside/go/runbasicpersonal?app=passTrial2&wps=typedword

The program passTrial2 then checks the URL like this:
Code:
w = 1
while word$(UrlKeys$,w,"&") <> ""
fullvar$ = word$(UrlKeys$,w,"&")
varname$ = word$(fullvar$,1,"=")
varvalue$ = word$(fullvar$,2,"=")
if varname$ = "app" then application$ = varvalue$
if varname$ = "wps" then wordpass$$ = varvalue$
w = w + 1
wend


The program then sets the variables application$ to "passTrial2" and wordpass$ to "typedword". (Attention UrlKeys$ is casesensitive.. so urlkeys$ will not work)
This makes it easy to read as many variables from the url as you want. (while checking the app value most probably does not really make sense... it's just for illustrating it here.)
« Last Edit: Nov 5, 2009, 7:50am by votan »Link to Post - Back to Top  IP: Logged
Psycho
Full Member
***
member is offline

[avatar]



Joined: Jan 2008
Gender: Male
Posts: 157
Location: North Carolina, USA
Karma: 2
 Re: How to pass variable via URL
« Reply #4 on Nov 5, 2009, 1:35pm »

Wonderful!

Thank you gentlemen, you got me exactly where I needed to go :D

The main program calls the other program both as a child that is run and rendered in the main screen as well as in a new window (it's an option the users wanted). I have my little test programs working properly now so I can apply what I've learned to my real program. I'll have to go back to the parent/child thread to get back up to speed as I had followed them closely way back when. Some of those early threads are where I learned about the whole "parent/child" concept. Without a classroom, you can't beat these forums for learning.

Everything sure seems easier to do once you know how ;)

Thanks again!

John "Psycho" Siejkowski
« Last Edit: Nov 5, 2009, 1:36pm by Psycho »Link to Post - Back to Top  IP: Logged
Psycho
Full Member
***
member is offline

[avatar]



Joined: Jan 2008
Gender: Male
Posts: 157
Location: North Carolina, USA
Karma: 2
 Re: How to pass variable via URL
« Reply #5 on Nov 6, 2009, 2:01pm »

I wanted to follow up on this in case this helps anyone else.
Taking the advice and examples from Stefan and votan, I was able to get my sample programs working so I would better understand how to apply to my primary program. The primary program has a search feature that yields anywhere from 0 to 100's of results, depending on search parameters, and displays them in a native RB table. That RB table includes a link column that can be clicked on for further detail about that item. When a link is clicked in the table, it runs a child program that displays another RB table with detailed information about that item. After expanding the program to another facility, users requested the option to have the detailed table open in its own window. I added that as an option that could be turned on or off but it still renders the child program in the primary window regardless. Prior to learning here how easy it really is to pass a variable via the URL (thanks again for that), I was having the parent program write the info to a text file, then the child and external programs were reading from that file. It's not a horrible option but pointing to the file was requiring me to code exact paths as many global variables (I believe ResourcesRoot$ being one of them) are being lost when run in child programs. This made it more difficult to keep the code generic at each facility. Passing the info through the URL will eliminate the need for the text file for the external program being displayed in a new window.
The next step was to review earlier threads on how to pass variables from the parent to the child for the rendered child program. I modified the code in these examples to pass the information as a function first, then call a function of the child to display the info. To prevent redundant code, I then have the external program call the same function to display the info (note the remarked out print statement).
To further prevent the rendered child program from displaying the info multiple times, I simply put in an if/then statement to check for info in the URL and only run that code if the program was called as a new window.
Whew! I hope I made that clear.
Below are the sample programs to assist in the explanation.

passTrial1
   'passTrial1
textbox #wordtyped, "",25
print " ";
link #go, "Go", [go]
print " ";
link #quit, "Quit", [quit]
#wordtyped setfocus()
global #display
wait

[go]
wordToPass$= #wordtyped contents$()
'now how to add wordToPass$ along in URL??????
PassTrial2Add$="http://localhost:8008/seaside/go/?app=passTrial2&wps="+wordToPass$
html "<script language='javascript'>window.open('";PassTrial2Add$;"','new','')</script>"

'how do I get passTrial2 the info from wordToPass$ when run as child?
print
run "passTrial2",#display
#display initValues$(wordToPass$)
#display showinfo()
render #display
wait


[quit]
cls
print "Goodbye"
end


passTrial2
   'passTrial2

w = 1
while word$(UrlKeys$,w,"&") <> ""
fullvar$ = word$(UrlKeys$,w,"&")
varname$ = word$(fullvar$,1,"=")
varvalue$ = word$(fullvar$,2,"=")
if varname$ = "app" then application$ = varvalue$
if varname$ = "wps" then wordToPass$ = varvalue$
w = w + 1
wend

'look for spaces in returned variable (shown with %20)
'This should be a function or sub when passing multiple variables
spaces=0
for x = 1 to len(wordToPass$)
if mid$(wordToPass$,x,3)="%20" then spaces=spaces+1
next x
for x = 1 to spaces+1
newWordToPass$=newWordToPass$+word$(wordToPass$,x,"%20")
'add space if we aren't at the end of the string
if x<spaces+1 then newWordToPass$=newWordToPass$+" "
next x


if UrlKeys$<>"" then 'display only in new window not child program
print "The program is called ";application$
a = showinfo()
'print "You typed in ";newWordToPass$
end if
end

function initValues$(die$)
global newWordToPass$
newWordToPass$=die$
end function

'to display when child is rendered in main program
function showinfo()
print "You typed in ";newWordToPass$
end function


BTW, votan, you'll see where I used your code as is, including displaying the program name (only in the new window). Agreed it's not needed but was great as an example so thanks again.

John "Psycho" Siejkowski
Link to Post - Back to Top  IP: Logged
StefanPendl
Global Moderator
*****
member is offline

[avatar]

Run for BASIC ...



Joined: Aug 2007
Gender: Male
Posts: 555
Location: Austria
Karma: 4
 Re: How to pass variable via URL
« Reply #6 on Nov 7, 2009, 10:06am »


Nov 6, 2009, 2:01pm, Psycho wrote:
Prior to learning here how easy it really is to pass a variable via the URL (thanks again for that), I was having the parent program write the info to a text file, then the child and external programs were reading from that file. It's not a horrible option but pointing to the file was requiring me to code exact paths as many global variables (I believe ResourcesRoot$ being one of them) are being lost when run in child programs. This made it more difficult to keep the code generic at each facility.


The problem exists only, if the child is called from within a sub procedure, but then you can ask the parent for the missing information.

DefaultVarsParent

print "Parent"
print
print "DefaultDir$ ...... "; DefaultDir$
print "Platform$ ........ "; Platform$
print "UserInfo$ ........ "; UserInfo$
print "UrlKeys$ ......... "; UrlKeys$
print "UserAddress$ ..... "; UserAddress$
print "ProjectsRoot$ .... "; ProjectsRoot$
print "ResourcesRoot$ ... "; ResourcesRoot$
print "Err$ ............. "; Err$
print "Err .............. "; Err
print "EventKey$ ........ "; EventKey$
print "RowIndex ......... "; RowIndex
print
html "<hr/>"

run "DefaultVarsChild", #child
render #child

html "<hr/>"

run "DefaultVarsChildFunction", #childFunct
#childFunct initVars(#self)
#childFunct display()
render #childFunct

call ShowDefaults
end

function GetIP$()
GetIP$ = UserAddress$
end function

sub ShowDefaults
html "<hr/>"
html "<hr/>"
print "Inside SUB"
run "DefaultVarsChild", #childSub
render #childSub

html "<hr/>"

run "DefaultVarsChildFunction", #childFunctSub
#childFunctSub initVars(#self)
#childFunctSub display()
render #childFunctSub
end sub


DefaultVarsChild

print "Child"
print
print "DefaultDir$ ...... "; DefaultDir$
print "Platform$ ........ "; Platform$
print "UserInfo$ ........ "; UserInfo$
print "UrlKeys$ ......... "; UrlKeys$
print "UserAddress$ ..... "; UserAddress$
print "ProjectsRoot$ .... "; ProjectsRoot$
print "ResourcesRoot$ ... "; ResourcesRoot$
print "Err$ ............. "; Err$
print "Err .............. "; Err
print "EventKey$ ........ "; EventKey$
print "RowIndex ......... "; RowIndex
print
wait


DefaultVarsChildFunction

function display()
print "Child Function"
print
print "DefaultDir$ ...... "; DefaultDir$
print "Platform$ ........ "; Platform$
print "UserInfo$ ........ "; UserInfo$
print "UrlKeys$ ......... "; UrlKeys$
print "UserAddress$ ..... "; UserAddress$
print "ProjectsRoot$ .... "; ProjectsRoot$
print "ResourcesRoot$ ... "; ResourcesRoot$
print "Err$ ............. "; Err$
print "Err .............. "; Err
print "EventKey$ ........ "; EventKey$
print "RowIndex ......... "; RowIndex
print
end function

function initVars(#parent)
UserAddress$ = #parent GetIP$()
end function

Link to Post - Back to Top  IP: Logged

---
Stefan


Any code I post can be used by others in their own projects, there is no need to ask for permission, just give credit if you like

Run BASIC 1.01, FF3.5 (IE8), Windows Vista Home Premium SP2, AMD Turion X2 RM-70 2GHz, 4GB RAM
Psycho
Full Member
***
member is offline

[avatar]



Joined: Jan 2008
Gender: Male
Posts: 157
Location: North Carolina, USA
Karma: 2
 Re: How to pass variable via URL
« Reply #7 on Nov 8, 2009, 6:28pm »

Nice idea Stefan.
In this particular case, I prefer the URL method but I think I already have something else done that could benefit from this passing of globals.

Thanks!

John "Psycho" Siejkowski
Link to Post - Back to Top  IP: Logged
StefanPendl
Global Moderator
*****
member is offline

[avatar]

Run for BASIC ...



Joined: Aug 2007
Gender: Male
Posts: 555
Location: Austria
Karma: 4
 Re: How to pass variable via URL
« Reply #8 on Nov 9, 2009, 3:11am »

The URL method is good for nonsensitive data, but asking the parent is better to exchange sensitive data, like credentials and such.
Link to Post - Back to Top  IP: Logged

---
Stefan


Any code I post can be used by others in their own projects, there is no need to ask for permission, just give credit if you like

Run BASIC 1.01, FF3.5 (IE8), Windows Vista Home Premium SP2, AMD Turion X2 RM-70 2GHz, 4GB RAM
   [Search This Thread][Send Topic To Friend] [Print]

Google
Webrunbasic.proboards.com
Click Here To Make This Board Ad-Free


This Board Hosted For FREE By ProBoards
Get Your Own Free Message Boards & Free Forums!