Jack
Jack

Reputation: 3799

Javascript redirect vs form submit to a php script

I've got a form on my websites homepage that contains a box for a user to enter a members name. From there the form currently submits to a PHP script which just calls Header('Location: blah); and then directs them to /search/username/.

Would it be worth it (i'm assuming so) doing that redirection in javascript so that directs them straight to /search/username? If so how would I go about redirecting with javascript, just plain old window.location = "http://www.google.com/"

Upvotes: 0

Views: 786

Answers (2)

iroel
iroel

Reputation: 1798

Server side is always better method as it's more difficult to bypass than client side method. Be sure to add exit function after redirection using header.

ob_clean();
header('Location: target.php');
exit();

Upvotes: 2

webbiedave
webbiedave

Reputation: 48887

To do it in JavaScript, you could:

<input id="username" type="text" onclick="" />
<input type="button" onclick="window.location='/search/'+document.getElementById('username').value" />

You could add some validation to the event as well.

Would it be worth it

That depends on your specific situation. If your server is getting 100's of these requests a second, then sure. Otherwise, it really doesn't matter which way you do it.

Upvotes: 0

Related Questions