View previous topic :: View next topic |
Author |
Message |
kev1952 -
Joined: 08 Sep 2005 Posts: 105 Location: Townsville Australia
|
Posted: Sun Jan 29, 2006 4:11 am Post subject: Refine a PHP script |
|
|
I imagine there is an easier way to do the following and would like some advice:
Code: | <?
$ans = $_POST['ans'];
if ($ans == "Choose from List") {
include "tutorial.html";
}
if ($ans == "MS Word") {
include "word.html";
}
if ($ans == "Paint Shop Pro") {
include "psp.html";
}
if ($ans == "Hosts File") {
include "hosts.html";
}
?> |
The code works fine but seems a bit to lengthy to me (and I wrote it). It processes a simple drop dowm menu in the main code and directs the user to the relevant page.
Can someone show me a better way - I'm really enjoying using PHP and want to learn much more, yet, about it. _________________ Cheers.... Kev
Kev's Place - http://www.kevsplace.com
Powered by Abyss X1. |
|
Back to top |
|
 |
Tom Chapman -
Joined: 09 Jul 2005 Posts: 933 Location: Australia
|
Posted: Sun Jan 29, 2006 4:54 am Post subject: |
|
|
Hey Kev, great code I was wondering if I could use your code! =D If so Thanks and How would I setup my form would I just use the post function or would I also need to name the tags or what?
Thanks, Tom |
|
Back to top |
|
 |
kev1952 -
Joined: 08 Sep 2005 Posts: 105 Location: Townsville Australia
|
Posted: Sun Jan 29, 2006 5:10 am Post subject: |
|
|
I reckon it could be better but you are welcome to use it. Here's the html for it:
Code: | <form method="POST" action="tut.php">
<div class="c4" align="center">Choose Tutorial: <select name="ans" size="1"></div>
<option selected>Choose from List
<option>MS Word
<option>Paint Shop Pro
<option>Hosts File
</select>
<input type="submit" value="Go!">
</form>
|
As you can see from the PHP I'm just using it to take a user to a selected webpage. Knock yourself out! :) _________________ Cheers.... Kev
Kev's Place - http://www.kevsplace.com
Powered by Abyss X1. |
|
Back to top |
|
 |
Tom Chapman -
Joined: 09 Jul 2005 Posts: 933 Location: Australia
|
Posted: Sun Jan 29, 2006 5:14 am Post subject: |
|
|
Kool thanks! Is there anyway to remove the submit button so once you select it redirects? |
|
Back to top |
|
 |
MonkeyNation -
Joined: 05 Feb 2005 Posts: 921 Location: Cardiff
|
Posted: Sun Jan 29, 2006 9:19 am Post subject: |
|
|
Code: | switch($_POST['ans']) {
case "Choose from List":
include("tutorial.html");
break;
case "MS Word":
include("word.html");
break;
case "Paint Shop Pro":
include("psp.html");
break;
case "Hosts File":
include("hosts.html");
break;
default:
include("default.html"); // Common sense tells you what this is.
break;
} |
And option needs a closing tag.
MrWiseOne ->
Code: | <select name='ans' onchange='this.form.submit();'> |
_________________
 |
|
Back to top |
 |
 |
kev1952 -
Joined: 08 Sep 2005 Posts: 105 Location: Townsville Australia
|
Posted: Sun Jan 29, 2006 10:35 am Post subject: |
|
|
Thanks. MN. I'm going to use the PHP code you showed. I knew there had to be another way to do it. Don't know that it will make much difference in a relatively short script like mine but it does make the script execution better (and probably more so for a larger selection range).
And I could just about kiss your feet (well.....:]) for the added attribute to the "select" function in the HTML code. It's does exactly what I wanted it to but couldn't work out how to make it happen. I wanted to avoid having to use a "submit" button and this has done just that!
Thanks again!
EDIT: If you would like to see the above script in action look here at the page(s) it relates to: http://kevsplace.no-ip.org/tutorial.html _________________ Cheers.... Kev
Kev's Place - http://www.kevsplace.com
Powered by Abyss X1. |
|
Back to top |
|
 |
roganty -
Joined: 08 Jun 2004 Posts: 357 Location: Bristol, UK
|
Posted: Sun Jan 29, 2006 12:20 pm Post subject: |
|
|
Code: | <form method="POST" action="tut.php">
<p class="p1">
<div class="c4" align="center">Choose Tutorial: <select name="ans" onchange="this.form.submit();"></div>
<option selected>Choose from List</option>
<option>MS Word</option>
<option>Paint Shop Pro</option>
<option>Hosts File</option>
</select>
<!--input type="submit" value="Go!"-->
</form>
</p> |
(copied from http://kevsplace.no-ip.org/tutorial.html)
kev1952, further to MonkeyNation advice to use the option closing tags, there is three other amendments needed.
1. move the </div> to just before after the </form> tag
2. change the "selected" attribute in the first option tag to selected="true"
3. move the </p> to before the </form> tag, but after the </div> tag
that should do it _________________ Anthony R
Roganty | Links-Links.co.uk |
|
Back to top |
|
 |
kev1952 -
Joined: 08 Sep 2005 Posts: 105 Location: Townsville Australia
|
Posted: Sun Jan 29, 2006 10:46 pm Post subject: |
|
|
Thanks, Roganty. Here is the finished product:
Code: | <form method="POST" action="tut.php">
<p class="p1">
<div class="c4" align="center">Choose Tutorial: <select name="ans" onchange="this.form.submit();">
<option selected="true">Choose from List</option>
<option>MS Word</option>
<option>Paint Shop Pro</option>
<option>Hosts File</option>
</select>
</div></p></form> |
_________________ Cheers.... Kev
Kev's Place - http://www.kevsplace.com
Powered by Abyss X1. |
|
Back to top |
|
 |
Tom Chapman -
Joined: 09 Jul 2005 Posts: 933 Location: Australia
|
Posted: Mon Jan 30, 2006 6:43 am Post subject: |
|
|
Thanks all for your help since I have now intergrated this php powered form into my site. :-) |
|
Back to top |
|
 |
|