return user to original webpage

Are you using LiveCode to create server scripts or CGIs?

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
edgore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 197
Joined: Wed Jun 14, 2006 8:40 pm

return user to original webpage

Post by edgore » Fri Oct 11, 2013 4:20 pm

Okay...

So I have an lc script that a user goes to that presents them with a list of editable fields showing data pulled from a database. The user can edit the info in the fields and press a button, which submits the data to another script via POST. That script then adds/deletes/updates the database (depending on the button clicked on the previous page - doesn't really matter for this). At the end of this processing, I would like to have the script send the user back to the previous scipt, which would now display the updated information,a nd allow them to make more changes if they want. So, basically the second script is "invisible - all the user ever sees is the main page (unless there is an error or something - in that case I want the second script to display an error message for a bit, then redirect back to the orignal page.

Here is what I have been trying:

this is the content of redirect.txt referenced below:
<meta HTTP-EQUIV="REFRESH" content="[[rTimer]]; url=http://localhost/idmgmt/">

Code: Select all

--this is the code I have to display an error page for 10 seconds before redirecting
   if the result is not a number then -- check the result and display the error message, if there is one
       revCloseDatabase theDBID
       put "10" into rTimer
       put url("file:redirect.txt") into theHTTPHeader
       put merge(theHTTPHeader) into theHTTPHeader
       put header theHTTPHeader
       put"<html>MYSQL ERROR</html>"
       exit to top
     end if
--this is the code to go back to the original page if everything works fine
     put "0" into rTimer
     put url("file:redirect.txt") into theHTTPHeader
     put merge(theHTTPHeader) into theHTTPHeader
     put header theHTTPHeader
     put "<html></html>"
Right now neither of them works. I never get a refresh - first one stays on the error message, the second one stays on the blank page.

Now that I have written all this out, it occurs to me that the best solution is probably to not have two scripts - instead have one script that checks to see if $_post has anything in it, process that if it does, then display the edit page from the same script, and have the submit go right back to this same script. It seems like it would work...

Still, it would be nice to know how to send a user to web page other than the one that was posted to.

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 928
Joined: Fri Aug 15, 2008 7:15 am

Re: return user to original webpage

Post by bangkok » Fri Oct 11, 2013 4:35 pm

edgore wrote: Now that I have written all this out, it occurs to me that the best solution is probably to not have two scripts - instead have one script that checks to see if $_post has anything in it, process that if it does, then display the edit page from the same script, and have the submit go right back to this same script. It seems like it would work...
That's the best strategy. You keep everything in the same lc script. And by doing tests, you can switch or if/then
-if POST contains something, then writes db
-if POST contains nothing, then displays db
edgore wrote: Still, it would be nice to know how to send a user to web page other than the one that was posted to.
easy to do : use "header Location"

Inside your index.lc page for instance :

Code: Select all

put header "Location: index.lc?C=1"
put "anything"
or

Code: Select all

put header "Location: newpage.lc"
put "anything"
Watch out : this command must be issued BEFORE you display anything on the page (otherwise it wont work).

And by the way, it can help you, using GET, to manage error messages for instance, in the same index.lc page.

Code: Select all

if $_GET["C"]=1 then 
put "Error"
quit
end if

if an error occurs then
put header "Location: index.lc?C=1"
put "anything"
quit
end if

edgore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 197
Joined: Wed Jun 14, 2006 8:40 pm

Re: return user to original webpage

Post by edgore » Fri Oct 11, 2013 5:54 pm

Thanks! Putting it all into a single script with conditionals worked perfectly.

I will have to read up on the location header and your other suggestions, since they seem like they would be useful, but I just don't understand them well enough to get exactly what is going on. Thanks for the pointer though, I now know what to research!

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 928
Joined: Fri Aug 15, 2008 7:15 am

Re: return user to original webpage

Post by bangkok » Fri Oct 11, 2013 6:41 pm

Nothing fancy, just try it in a page. You'll see.

It's like an "automatic jump to another webpage", like if the user would click on a hyperlink. It's very usefull (provided that you don't display anything before to issue the command).

Post Reply

Return to “CGIs and the Server”