Page 1 of 1
Changing a simple web page from GET to Sessions
Posted: Fri Aug 01, 2008 10:03 am
by timodemus
I have a simple script (written in php) that uses GET to transfer
variables, and I want to use sessions instead of GET.
How do I do it ? (Any code would be greatly appreciated,
because I learn this better if I have a fully functional script I can study !!!)
This is the script I want to change:
Code: Select all
<HTML>
<HEAD>
<TITLE>Web Page</TITLE>
</HEAD>
<BODY>
<?php
if($_GET['product'] =="hat") {
echo 'Hat<BR>';
};
if($_GET['product'] =="shoes") {
echo 'Shoes<BR>';
};
if($_GET['product'] =="jeans") {
echo 'Jeans<BR>';
};
?>
<p>Choose product: <a href="http://www.timodemus.net/?product=hat">Hat, </a>
<a href="http://www.timodemus.net/?product=shoes">shoes, </a>
<a href="http://www.timodemus.net/?product=jeans">jeans.</a>
</p>
</BODY>
</HTML>
Thanks!
Re: Changing a simple web page from GET to Sessions
Posted: Fri Aug 01, 2008 10:17 am
by Dynamis
If I am understanding your question correctly, you want to grab information for the URL and then display something based on that. To do this, GET and REQUEST (pretty much same thing) are the only ways to do this. I guess you could always use javascript to parse the URL and then set a session variable. Or the same w/ php. Both of these seem tedious and not worth the effort though. Maybe you could explain in more detail what you are trying to do.
Re: Changing a simple web page from GET to Sessions
Posted: Fri Aug 01, 2008 10:27 am
by timodemus
I'm trying to hide the information (in other words: not to display variables in URL) from user and to have the same functionality as in the script I posted. I don't want to use javascript, because some users have disabled it from browser's settings.
Re: Changing a simple web page from GET to Sessions
Posted: Fri Aug 01, 2008 10:47 am
by Dynamis
Here is how I would do it. Make a form that uses POST and a drop-down menu w/ onclick form submission. Try this and let me know if this works for your purpose:
Assume the page w/ this on it is called test.php. In other words, the form action must should be the same as page it is on.
Code: Select all
<?php
$input = $_POST['product'];
echo $input;
?>
<form action="test.php" method="POST">
<select name="product" onChange="this.form.submit();">
<option value=""></option>
<option value="hat">Hat</option>
<option value="shoes">Shoes</option>
<option value="jeans">Jeans</option>
</select>
</form>
Re: Changing a simple web page from GET to Sessions
Posted: Fri Aug 01, 2008 11:09 am
by timodemus
This is close to what I mean:
Code: Select all
<HTML>
<HEAD>
<TITLE>Web Page</TITLE>
</HEAD>
<BODY>
<?php
if($_POST['product'] =="hat") {
echo '- Hat -<br>
This hat is used by American Army.';
};
if($_POST['product'] =="shoes") {
echo '- Shoes -<br>
These shoes are water resistant.';
};
if($_POST['product'] =="jeans") {
echo '- Jeans -<br>
These jeans include a belt.';
};
?>
<form action="index.php" method="POST">
<select name="product" onChange="this.form.submit();">
<option value=""></option>
<option value="hat">Hat</option>
<option value="shoes">Shoes</option>
<option value="jeans">Jeans</option>
</select>
</form>
</BODY>
</HTML>
, but I want it with links, not with a drop-down menu.
By the way, what is onChange="this.form.submit();" ?
Is it javascript code?
Re: Changing a simple web page from GET to Sessions
Posted: Fri Aug 01, 2008 11:25 am
by Dynamis
Here are some possible options I see for you.
1. Instead of links, use buttons. That would allow you to submit the form when one is clicked, use the post method as in the previous code, and display the value. There may be some way you can destyle a button as well so it looks like a link, but I don't know about this off the top of my head.
2. Use links. Without using the get method I can only think of one solution. Grab the url for the page, parse it using substrings or regular expressions, and then display the correct information based on the parsed url.
3. You mentioned about not displaying the variable in the url. You could always use something like ?id=2 and in your code, if id=2 echo the hat information for example, or if id=3 echo the jeans information.
If I think of anything else I'll be sure to post. Also, if you want more information on any of these let me know.
Re: Changing a simple web page from GET to Sessions
Posted: Fri Aug 01, 2008 11:33 am
by timodemus
Thanks!
The 3rd one is allways a useful solution, but I want my URL to be just
http://www.timodemus.net/ and nothing else.
How do I make a button that uses any picture of my choise (and not using javascript, having a hand as a mouse pointer)?
Re: Changing a simple web page from GET to Sessions
Posted: Fri Aug 01, 2008 11:37 am
by Dynamis
timodemus wrote:How do I make a button that uses any picture of my choise (and not using javascript)?
Tutorial on html buttons
Here is the example they give on the website:
<input type="image" src="rainbow.gif" name="image" width="60" height="60">
You use src to point to the image you want to use as the button.
Re: Changing a simple web page from GET to Sessions
Posted: Fri Aug 01, 2008 11:55 am
by timodemus
Thanks!
How do I make a form that has
<input type="image" src="rainbow.gif" name="image" width="60" height="60">
and sends product=hat by POST, by pressing this button?
PS: I haven't made any forms myself, yet.
PPS: I speak finnish better than english.
Re: Changing a simple web page from GET to Sessions
Posted: Fri Aug 01, 2008 12:06 pm
by Dynamis
timodemus wrote:How do I make a form that has
<input type="image" src="rainbow.gif" name="image" width="60" height="60">
and sends product=hat by POST, by pressing this button?
I now realize that isn't a very good solution either (since buttons can not receive values directly), but if you want to use it, you could do the following:
Code: Select all
<?php
$input = $_POST['product'];
echo $input;
?>
<form action="test.php" method="POST">
<input type="hidden" name="product" value="hat">
<input type="image" src="a_hat_picture.gif" name="image" width="60" height="60">
</form>
<form action="test.php" method="POST">
<input type="hidden" name="product" value="jeans">
<input type="image" src="jeans_picture.gif" name="image" width="60" height="60">
</form>
<form action="test.php" method="POST">
<input type="hidden" name="product" value="shoes">
<input type="image" src="shoes_picture.gif" name="image" width="60" height="60">
</form>
In general, using links for something of this nature isn't a good solution. I would either do radio buttons, drop down, or perhaps check boxes.
Re: Changing a simple web page from GET to Sessions
Posted: Fri Aug 01, 2008 12:26 pm
by timodemus
Thanks again, I can now do the thing I wanted to.
The script I posted was just a bad example, I'm not making an online shop.
I want to use links, because I make dynamic pages such as
http://www.tammelantyosali.net/
(Click on the picture and then click "Työtoiminta" on the left and follow the links on the center.)
Another question: is it possible to make a php script that makes an image from any text automatically?
Re: Changing a simple web page from GET to Sessions
Posted: Fri Aug 01, 2008 4:03 pm
by Chalks
timodemus wrote:Another question: is it possible to make a php script that makes an image from any text automatically?
If you have the GD library installed, it's pretty simple:
to build an image from scratch, use
imagecreate()
to build an image from a predrawn image, use
imagecreatefromjpeg()
Then you will add more information onto the image with
various functions
usually, you will create a script "buildimage.php" and link to it like so:
Code: Select all
<img src="buildimage.php" alt="my image" />
incidentally, this is probably the same basic idea used to create
my avatar.