I am facing some issue in my login page, I have three categories i.e.
- Staff
- Lecturer
- Student
By selecting a category, my page should redirect to a specific page.
Forexample: If I select Staff and click on sumbit button, it should redirect me to ‘verify-admin.php’ page.
The following is my current code:
<?php
if(isset($_POST["sub"])) {
$value = $_POST["sp"];
header("location: /$value");
}
?>
<head>
<script>
$("#dtls").submit(function() {
var value = $("#sponsor").val();
if(value == "Admin") {
window.location.href = "/verify-admin.php";
} else if(value == "Lecturer") {
window.location.href = "/verify-lecturer.php";
} else if(value == "Student") {
window.location.href = "/verify-student.php";
}
});
</script>
</head>
<body>
<form action="#" method="post" class="login100-form" id="dtls">
<select name="sp" id="sponsor" class="input100" style="border:0" >
<option selected>Select Category</option>
<option value="Admin">Admin</option>
<option value="Lecturer">Lecturer</option>
<option value="Student">Student</option>
</select>
<input type="username" name="username" class="input100" placeholder="Staff / Student ID"/>
<input type="password" name="password" class="input100" placeholder="Password"/>
<input type="submit" name="sub" class="login100-form-btn" id="but">
</form>
</body>
2
Answers
Your problem is you can access form but instead of this you should target your button if button is press then you should verify and you should use onload method which will prevent you from error.
You are using
jQuery
in your code, but you have not included it. SeejQuery
documentation here.You should also write your jQuery code inside
$(document).ready()
function. This would help https://learn.jquery.com/using-jquery-core/document-ready/.You need to use
preventDefault()
to prevent the Default function of the element, This might help https://api.jquery.com/event.preventDefault/#event-preventDefaultFollowing is the correct code.
Here is a working example of your code without PHP.