ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I want to somehow use PHP to load the same page over and over but with different content. For example, if a user clicks "next book", the same page will load, but they will be able to see the info for the next book stored in my file.
i was trying to use a hidden field - but it didn't seem to like it.
I believe putting __FILE__ in the action of a form will achieve the same thing (__FILE__ holds the name of the current file, complete path)
Or you can try using header("location: the_url_") to redirect to the same file.
Originally posted by coolman0stress I believe putting __FILE__ in the action of a form will achieve the same thing (__FILE__ holds the name of the current file, complete path)
Or you can try using header("location: the_url_") to redirect to the same file.
1) If I put the file in the action of a form, wouldn't I need a "Submit" button to cause the action to take place? I just want them to be able to click a link to have the same effect.
2) Is "header()" some php function I don't know about? Where would I invoke it??
1) How about using the page's url as the href part of your link?
2) header basically encapsulates all the stuff that gets sent to a browser from the server before the actual content. It includes headers, mime types (what type of document it is), cookies, redirects, etc... More info
Though in your case i think 1 would be the easiest choice.
Last edited by coolman0stress; 11-24-2003 at 03:05 AM.
Originally posted by coolman0stress 1) How about using the page's url as the href part of your link?
2) header basically encapsulates all the stuff that gets sent to a browser from the server before the actual content. It includes headers, mime types (what type of document it is), cookies, redirects, etc... More info
Though in your case i think 1 would be the easiest choice.
Coolman,
1) is what I was trying to do - I agree with you, it seems like the easiest solution.
My question is this: will I be able to pass parameters to the new page just by using an href??
For example, I have a file where each line is a new book and contains information about that book to be displayed.
If a user is viewing a book, and wants to see the next one in the list, he simply clicks "next", and then I want to display the info for the next book (on the same page, overwriting the previous data).
So I'm thinking I will need to keep some sort of counter or something (I can worry about thos details), but will I be able to access that $counter variable from another document once it is loaded??
Basically what you need to do is pass an extra variable through the url about the state of your application, so what page should be displayed next.
Say your php file is called "book.php". The first time you load it, you want to for example display the first book. That's the default behaviour. Nothing was passed to the file.
Now on that page you create a link, "Next", where you take the current book # and increment it by one and pass this number through the url to the same script. Something like this:
href="book.php?book_number=1"...
Now what you have to do is when your script loads, check for the variable "book_number", if it was not set, then this is the first time the user used the book, so you display the default page. But if it was set to something, use that number to determine which book to display and increment the number again for the link.
How do I check if a variable exists if it hasn't been initialized yet?
Also,
if I have <a href="next.php?booknum=1">
then won't "1" always get passed no matter what? How would I increment it and then pass the incremented value?? It would also have to decrement if a user clicked the "previous" link.
You're right, you need to find a way to retrieve the value that was passed to the script. There are a couple of ways of doing it.
The most versatile method is to use $_REQUEST['varname'] which will return the value of the variable. I've just started learning php myself, but i found $_REQUEST to be the best, since it doesn't matter in what form the variable was passed (wether it was through get, post or cookie).
Here's an example:
PHP Code:
<?php $num= $_REQUEST['num']; // get number that might have been passed echo "Num: ", $num, "<br/>"; // display current number echo "<a href=", $_SERVER['php_self'], "?num=", ++$num, ">Next</a>"; // create the next link by incrementing the current num ?>
This has proven to be quite difficult. It turns out that passing ++$num works just fine, however, if I want to pass another parameter, such as $title, if I concatenate it like you have shown me, I lose data.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.