Refine a PHP script

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
kev1952
-


Joined: 08 Sep 2005
Posts: 105
Location: Townsville Australia

PostPosted: Sun Jan 29, 2006 4:11 am    Post subject: Refine a PHP script Reply with quote

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 View user's profile Send private message Send e-mail Visit poster's website
Tom Chapman
-


Joined: 09 Jul 2005
Posts: 933
Location: Australia

PostPosted: Sun Jan 29, 2006 4:54 am    Post subject: Reply with quote

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 View user's profile Send private message Visit poster's website MSN Messenger
kev1952
-


Joined: 08 Sep 2005
Posts: 105
Location: Townsville Australia

PostPosted: Sun Jan 29, 2006 5:10 am    Post subject: Reply with quote

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: &nbsp;&nbsp;<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 View user's profile Send private message Send e-mail Visit poster's website
Tom Chapman
-


Joined: 09 Jul 2005
Posts: 933
Location: Australia

PostPosted: Sun Jan 29, 2006 5:14 am    Post subject: Reply with quote

Kool thanks! Is there anyway to remove the submit button so once you select it redirects?
Back to top View user's profile Send private message Visit poster's website MSN Messenger
MonkeyNation
-


Joined: 05 Feb 2005
Posts: 921
Location: Cardiff

PostPosted: Sun Jan 29, 2006 9:19 am    Post subject: Reply with quote

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 View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number
kev1952
-


Joined: 08 Sep 2005
Posts: 105
Location: Townsville Australia

PostPosted: Sun Jan 29, 2006 10:35 am    Post subject: Reply with quote

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 View user's profile Send private message Send e-mail Visit poster's website
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Sun Jan 29, 2006 12:20 pm    Post subject: Reply with quote

Code:
<form method="POST" action="tut.php">
<p class="p1">
<div class="c4" align="center">Choose Tutorial: &nbsp;&nbsp;<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 View user's profile Send private message Visit poster's website
kev1952
-


Joined: 08 Sep 2005
Posts: 105
Location: Townsville Australia

PostPosted: Sun Jan 29, 2006 10:46 pm    Post subject: Reply with quote

Thanks, Roganty. Here is the finished product:
Code:
<form method="POST" action="tut.php">
<p class="p1">
<div class="c4" align="center">Choose Tutorial: &nbsp;&nbsp;<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 View user's profile Send private message Send e-mail Visit poster's website
Tom Chapman
-


Joined: 09 Jul 2005
Posts: 933
Location: Australia

PostPosted: Mon Jan 30, 2006 6:43 am    Post subject: Reply with quote

Thanks all for your help since I have now intergrated this php powered form into my site. :-)
Back to top View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group