Click to See Complete Forum and Search --> : Form submit+URL redirection


aommaster
05-13-2007, 03:05 PM
Hi guys!

I'm not really sure where the put this, and I just hope this is in the right place.

I would like to create a form on my website, where when the user types in something and presss the submit, it redirects him to a cetain webpage. Let's say he types in the word "cat" in the form. It should redirect him to:
/folder/cat.htm

How should I go about doing this. I have seen some scripts that need CGI (I think they are what I am looking for), but they seem overly complex for what I want, and since I have absolutely no knowledge of CGI, I can't even tell.

Please give me the easiest way of implementing a script like this, preferable without the use of javascript, since many browsers have that disabled.

Thanks for your time

web_bert
05-13-2007, 03:23 PM
Do you have the option to use and Server Side languages?

You would be best to create a case statement, in ASP this would be

Dim UserSelection : UserSelection = request.form("SelectBoxName")
Select Case UserSelection
Case "cat"
response.redirect("www.thecatpage.com")
Case else
response.redirect("www.indexpage.com")
End Select

If the user enters "cat" then you get thecatpage, otherwise you get the indexpage.

MarcD
05-13-2007, 03:31 PM
If your hosting service supports PHP, i would go for that.

I would image it would be something like this:

<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
if(!empty($_POST['keyword'])){
$target = 'folder/' . $_POST['keyword'] . '.html';
}
else{
$target = $_SERVER['PHP_SELF'];
}
}
else{
$target = $_SERVER['PHP_SELF'];
}
?>

<form action="<?php echo $target; ?>" method="post">
<input type="text" id="keyword" name="keyword" />
<input type="submit" value="Submit" />
</form>


I hope this helps you out

aommaster
05-13-2007, 10:50 PM
Do you have the option to use and Server Side languages?

You would be best to create a case statement, in ASP this would be

Dim UserSelection : UserSelection = request.form("SelectBoxName")
Select Case UserSelection
Case "cat"
response.redirect("www.thecatpage.com")
Case else
response.redirect("www.indexpage.com")
End Select

If the user enters "cat" then you get thecatpage, otherwise you get the indexpage.

Hi! I appreciate your reply, however, this does not achieve what I want, since the user MUST type in the word 'cat' to get to the page. What I want it to to is for it to take what the user types into the form, and stick it into the address bar at the end of my website's address.

If your hosting service supports PHP, i would go for that.
I hope this helps you out

thanks for your reply. This really helped. I have a quick question about this though. When I first type in something into the form and press submit, the form behaves as if I have just pressed the refresh button the browser. That is, the same page loads again. Once I type it in again, it then takes me to the respective page.

What is the reason for this?

Thanks again for your time.

aommaster
05-14-2007, 12:16 PM
Upon further searching on the internet, I found this site:
http://www.htmlcodetutorial.com/help/ptopic8000.html

If you take a look at the second post, the person had posted a javascript code:

<head>
<script type="text/javascript">
function getURL(val){
base = 'http://www.domain.com/';
location = base + val;
return false;
}
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="" onsubmit="return getURL(this.url.value)">
<label>
<input type="text" name="url" />
</label>
<label>
<input type="submit" name="Submit" value="Submit" />
</label>
</form>
</body>


This code is almost exactly what I need. Only thing is, I am wondering whether javascript would be a suitable language to use, since many browser's have that disabled.

What other alternatives could I use? I want it to be pretty general, since I noticed that some web hosting services do not offer languages such as php, etc.

Thanks a lot for your time guys!

MarcD
05-15-2007, 08:11 AM
thanks for your reply. This really helped. I have a quick question about this though. When I first type in something into the form and press submit, the form behaves as if I have just pressed the refresh button the browser. That is, the same page loads again. Once I type it in again, it then takes me to the respective page.

What is the reason for this?

That is a problem i have overlooked.

However, after evaluating my script, i came up with a simpler solution:

<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
if(!empty($_POST['keyword'])){
$target = 'folder/' . $_POST['keyword'] . '.html';
header("Location: " . $target);
exit;
}
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" id="keyword" name="keyword" />
<input type="submit" value="Submit" />
</form>

This essentially does the same as the Javascript you posted above, but with the benefit that PHP cannot be disabled.

aommaster
05-15-2007, 09:56 AM
Hi!

I tried out the script that you gave me, but it gave me this error when I tried to use it:
Warning: Cannot modify header information

Would you mind telling what 'problem' you overlooked when you wrote your first script? I'm interested in learning pHp and I think that learning by example is probably the best way to start :)

thanks a lot for your time!