Skip to content
Advertisement

Create a simple page redirect system in WP based on IDs

What I’m trying to do is to have my students enter their name (assigned code) in a field and once they click the “Go” button they will be redirected to their page. I’m trying to do this in WP and I’m already working with a theme. I don’t have any programming knowledge for myself but I have this code from a friend. Unfortunately, the code doesn’t work in WP as the “onclick” atribute is stripped automatically.

This is how the head section looks like:

<head>
<script type='text/javascript' src='students.js'></script>
</head>

This is the body section:

<body>
<input id="textfield" type="text" name="name">
<input type="button" value="Go" onclick="dosomething()">
</body>

And I “define” my student IDs in the students.js which looks like this:

function dosomething() {
    name = document.getElementById('textfield').value;
    address = 'error.html';
    switch(name.toLowerCase()) {
        case '':
            alert('Please enter your name');
            break;
        case 'StudentID':
            address = 'StudentID.html';
            break;

    default:
            address = 'error.html';
            break;
            
    }
    window.location = address;
}

Do you know how can I accomplish this in a more modern way? From my understanding the .click() event in jQuery is what I’m looking for. Unfortunately I don’t know exactly how to code this. Any help is appreciated.

Advertisement

Answer

You can do something like this

HTML

<body>
<input id="textfield" type="text" name="name">
<input id="go" type="button" value="Go">
</body>

jQuery

$('#go').click(function() {
var page = $('#textfield').val();
 switch (page) {
 case 'StudentID':
   address = 'StudentID.html';
   break;
 default:
   address = 'home.html';
 }
 window.location = address;
});
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement