How do i set the different redirect options for two different values in button?

I’ve made a sign up form to register a user whether he is a doctor or patient for a website.
and this is the html code i used for form front end

 <label class="text-left" >Are you a:</label>
    <select class="options" id="Options" onchange="handleSelection()">
        <option value="1">Doctor</option>
        <option value="2">Patient</option>
      </select>
</div>
            <div id="Button" >
            <button href="#" type="submit" id="buttonLink" class="btn" onclick="response(event)">Continue</button>
            </div>

this is the code I’ve written in JS for the function:

function response(event){
    let fn=document.getElementById('first_name').value
    let ln=document.getElementById('last_name').value
    let phNo=document.getElementById('phone').value
    let mail=document.getElementById('email').value
    let pwd=document.getElementById('pwd').value


    var options = document.getElementById('Options').value;
//took this to check whether phone number has alphabets or not
    let check=/^[a-zA-Z]+$/

    if(phNo.length>10){
        
        alert('Enter a valid Phone number.')
        event.preventDefault()
    }
    
    if(check.test(phNo)){
alert('Enter a valid phone number.')
event.preventDefault()
    }
    if(pwd.length<8 || pwd.length>12){
        alert('Enter a valid password')
        event.preventDefault()
    }


    //here's the code to redirect to other website based on the choice you've selected
    if (selectedOption === "1") {
        
      buttonLink.href="Doctor_website"

    } else if (selectedOption === "2") {
        
      buttonLink.href="Patient_website"

    }

}


if I press on continue button after filling the details it shows “HTTP ERROR 405”

I’m trying to seperate the users for my website based on doctor and patient for that I’ve created this form and this JS code. I’ll link my database later

  • Error 405 indicates, that the method you are using to fetch the data from the server is not allowed, in this case GET. You should look how your routing is set up for your project. Maybe you could post some more code.

    – 

  • should i send html?

    – 

It looks like you’re trying to redirect users based on their selection (doctor or patient) using the JavaScript code. However, there are a few issues in your code that need to be addressed.

Variable Naming Issue:
You are trying to access a variable named selectedOption, but in your code, you defined the variable as options. Update the variable name to be consistent:

var selectedOption = document.getElementById('Options').value;

Undefined Variable:
It seems like you’re trying to set the href attribute of a button with ID buttonLink, but the variable buttonLink is not defined. You need to define it before using it:

var buttonLink = document.getElementById('buttonLink');

Submit Form:
The form is missing a form tag around your input fields. Ensure you have a form tag that includes all the input fields.

Leave a Comment