/    Sign up×
Community /Pin to ProfileBookmark

Form submit+URL redirection

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 [U]without[/U] the use of javascript, since many browsers have that disabled.

Thanks for your time

to post a comment
HTML

6 Comments(s)

Copy linkTweet thisAlerts:
@web_bertMay 13.2007 — 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.
Copy linkTweet thisAlerts:
@MarcDMay 13.2007 — If your hosting service supports PHP, i would go for that.

I would image it would be something like this:
[code=php]
<?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>
[/code]


I hope this helps you out
Copy linkTweet thisAlerts:
@aommasterauthorMay 14.2007 — 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.[/QUOTE]


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[/QUOTE]


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.
Copy linkTweet thisAlerts:
@aommasterauthorMay 14.2007 — 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:
[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>
[/CODE]


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!
Copy linkTweet thisAlerts:
@MarcDMay 15.2007 — 
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?[/quote]


That is a problem i have overlooked.

However, after evaluating my script, i came up with a simpler solution:
[code=php]
<?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>
[/code]

This essentially does the same as the Javascript you posted above, but with the benefit that PHP cannot be disabled.
Copy linkTweet thisAlerts:
@aommasterauthorMay 15.2007 — 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!
×

Success!

Help @aommaster spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...