I have a snippet that generates a list of the States of the US from a mySQL database with PHP. The users sees something like this:
Alabama
Alaska
Arizona
Arkansas
California
Etc ...
The State names are hyperlinks, i.e. the name is the anchor text. I want the user to be able to click a State name and get a new page with info about that State. This data is also held in mySQL. The PHP / mySQL aspect is trivial but I don’t see how to do this ’within’ MODx.
For example my snippet could create a list of hyperlinks in this format:
www.domain.com/myscript.php?state=anchor text
But a user clicking that link would be taken ’outside’ of MODx to a PHP page so I don’t think this is the right approach.
Is there any way I could generate the page content from ’within’ MODx using a snippet?
Sorry if this is a dumb question. I don’t want a total answer, just need to be pointed in the right general direction. Thanks!
If you send the user to the same page each time, but with the state name in the $_POST data (or the URL), you could use a snippet on the landing page to produce the display of the data based on the $_POST variable.
I’m assuming that you only want to show one state’s data on the landing page. If you want to show all states and use anchors, I think forwarding the user to
(where nn is the ID of the landing page) would do it.
I think so.
Try expressing the link with a MODx link tag like this in your code (then MODx will make correct link for you):
echo ("<a href='[~12~]?usastate=$row[1]'>$row[1]</a><br />");
I’m not sure if it will be parsed after the snippet call, but it’s worth a try. If that doesn’t work, add a parameter to the snippet call:
Then in the snippet:
echo ("<a href='" . $myUrl . "?usastate=$row[1]'>$row[1]</a><br />");
Hello
thanks OpenGeek for your comment. If you can point me in the direction of documentation that would enable me to not have to declare pages as .php in order to pass variables (in the form of ?name=value pairs) I would be truly grateful. Please bear in mind that I am not a programmer but just someone who sees the potential of MODx.
As I wrote above, I have a snippet that displays a list of US States and when the user clicks a State (i.e. the anchor text) I want him to be taken to another page which contains all the info for that one State. Actually, for me, this is a model for a lot of other things I would like to try with MODx, so if I could figure out a solution in the way you said (and believe me I have tried and read a lot of things) I would be very happy.
Looking again at my site yesterday I was wrong - the pages are still showing the strange ending page1.php.html I have included this:
<base href="[(site_url)]"></base>
in the head section of my template
Tony
-
MODX Staff
- 2,502 Posts
You can pass variables to a page so long as the php script you have is either in a snippet or using the API. Don’t get hung up on file extensions as they are purely abstract for the purposes of friendly URLs when you have fURLs enabled. It uses Apache mod_rewrite to give a file an extension so even though your file has the extension .html or php they are both php files rendered via /index.php. Giving a page a php extension doesn’t do any thing but change the friendly url. You could call your files .pizza or .joe or whatever you want it is still parsed by the MODx parser.
This is a
user contributed document on how to Create a Snippet. Snippets are php scripts that perform any logic you want done on a page and you access them by calling them using the following Syntax: [[SnippetName? ¶meter=`` ¶meter2=``]] etc. Parameters are any value you wish to pass from the individual to the snippet. There need not be any parameters if the snippet doesn’t need values from the document.
Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup
magician.
Blog ✦
Twitter ✦
LinkedIn ✦
GitHub