HTML/PHP Submit button redirect

I know this may not be the best way to do this however I try ttext boo figure it out.

I have and index.php that has a text box and a submit button.

What I need is advice on the best way to look up a value in the text box an redirect to a website based on the value.  

If the value does not exist then a message displaying the value does not exist.

Example

textbox value  = "google" when submit is clicked look for the value redirect redirect to

google = www.google.com

the text will have letters or numbers.

Thanks
MPDenverAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ramyajanarthananCommented:
Do you have any specific set of urls alone to be redirected or any text?

Do you want to redirect having your own baseurl or to common open sites like google?
0
Julian HansenCommented:
Two ways
Server side
<?php
function sanitize(&val)
{
    // sanitize your post here
}

function lookup_value($value)
{
    // Do your lookup here
}
$msg = "";
if ($_POST) {
    $request = sanitize($_POST['request']);

    $url = lookup_value($request);

    if (!empty($value)) {
        header("location: $url");
        die();
    }
}
else {
    $msg = "Search term not found";
}
?>
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
<form method="post">
    Search term <input type="text" name="request" /> <span id="msg"><?php echo (!empty($msg) ? $msg : '');?></span><br/>
   <input type="submit" />
</form>
</body>
</html>

Open in new window


AJAX
PHP - lookup.php
<?php
function sanitize(&val)
{
    // sanitize your post here
}

function lookup_value($value)
{
    // Do your lookup here
}

if ($_POST) {
    $request = sanitize($_POST['request']);
    $url = lookup_value($request);
    die($url);
}
?>

Open in new window

HTML
<!doctype html>
<html>
<head>
<title></title>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
    $('form').submit(function(e) {
        e.preventDefault();
        $.post('lookup.url',{request: $('#request').val()}, function(resp) {
             if (resp == '') {
                   $('#msg').html('Search term not found');
             }
             else {
                 window.loction = resp;
             }
        }
    });
});
</script>

</head>
<body>
<form method="post">
    Search term <input type="text" name="request" id="request"/> <span id="msg"><?php echo (!empty($msg) ? $msg : '');?></span><br/>
   <input type="submit" />
</form>
</body>
</html>

Open in new window

0
magentoCommented:
You can javascript case if you have set of urls.

<!DOCTYPE html>
<html>
<body>
<h1>EE solution</h1>
<input type=text id="txt"><button onclick="redirect()">Redirect Me</button>
<script>
function redirect()
{
var x;
x=document.getElementById("txt").value;
switch(x)
{
case "google":
window.location="http://www.google.com";
  break;
case "yahoo":
window.location="http://www.yahoo.com";
  break;
default:
window.location="http://www.yoursite.com";
}
}
</script>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MPDenverAuthor Commented:
Great Work Thanks
0
magentoCommented:
MPDenver, this is my first javascript program :). Hope it helps you . Thanks for the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.