Detecting input content to redirect to a specific page using PHP

My HTML has the following:

<input type="text" name="code" />
<input type="submit" value="Submit" />

What I want to do is redirect the user to page1.htmlwhen he / she enters, say, 12345and redirect him / her to page2.htmlwhen he / she enters 56789. How can I do this with PHP? I also use JavaScript.

Let me rephrase that (just in case). I want to write 12345in the input field and after clicking the button Submitredirect to page1.html. The same principle for 56789and page2.html.

I found this old jQuery code and modified it a bit. I would be glad to hear your suggestions on polishing and achieving what I want.

$('input').keyup(function () {
    var val = $(this).val();

    var newVal = val.split('1234').join('HELLO');

    if (newVal !== val) {
        window.open("http://localhost:8000/page1.html");
    }
});

Also, is this correct (for PHP)? (not tried yet)

if ($_POST['code'] == '1234'){
  header("Location: redirect_to_me.php");
}

, , .

.

+4
3

$(document).ready(function(){
var pages = {
    "123456" : "page1.html",
    "56789" : "page2.html",
    "5252" : "page3.html"
}
$('input[type=submit]').click(function (e) {
e.preventDefault();
var val = $('input[name=code]').val();
var page = pages[val];

 window.location = page;

 });
 });
+1

(_POST _GET), "". , , ..... "" .

if(isset($_POST)){

    if($_POST['text'] == '12345'){

        header ("Location: page1.html");
        exit;

   } elseif($_POST['text'] == '6789'){

        header ("Location: page2.html");
        exit;

   } 

}
+1

Do it in PHP;

<form action="file.php" method="POST">
    <input type="text" name="code" />
    <input type="submit" value="Submit" />
</form>

And yours file.phpusing an operator switch;

switch($_POST['code']):

    case '123456':
        header('Location: page1.html');
    break;

    case '56789':
        header('Location: page1.html');
    break;

    case '5252':
        header('Location: page3.html');
    break;

    default:
        header('Location: codenotfound.html');

endswitch;

Or a more elegant solution;

$page = 'codenotfound.html';

$pages = array(
    '123456' => 'page1.html',
    '56789' =>'page2.html',
    '5252' => 'page3.html'
);

if(isset($pages[$_POST['code']])):
    $page = $pages[$_POST['code']];
endif;

header('Location: ' . $page);
+1
source

All Articles