Open page from html depending on if statement

0

I am creating a web page where you can create two types of users, A and B. For this I have a script where people can decide what type of account to use. Each account re-directs one or the other but I do not know how to do it from a script. Here I have the code:

<h2> Crear cuenta </h2>

<select name="cuenta" id="tipo" >
   <option selected value="0"> Tipo de cuenta </option> 
       <option valor="A">A</option> 
       <option valor="B">B</option> 
 </select>

 <p id="demo"></p>

 <script>

    function elegir(){

        a = document.getElementById("tipo");        
        if(a.value == "A")           

           document.getElementById("demo").innerHTML = "A";

        else if (a.value == "B")

            document.getElementById("demo").innerHTML = "B";

        }

 </script>
 <button onclick="elegir()">Crear</button>

Right now what he does is print A or B according to the choice, I would like him not to show that, but to go there when he re-directs to one place or another. Would it be possible?

    
asked by user5673573 12.11.2017 в 16:36
source

4 answers

0

To direct the user to one place or another, the javascript location.assign function, that is, the code would remain :

if (a.value == "A") {
  document.location.assign('/user/A')
} else if (a.value == "B") {
  document.location.assign('/user/B')
}

Likewise, if what you want is to give options on which page to go to, it is advisable to simply use links:

<p>Seleccione su tipo de usuario</p>
<a href="/user/A">Anónimo</a>
<a href="/user/B">Beta user</a>

That way you do not need any javascript

    
answered by 12.11.2017 / 16:52
source
0

hi I suggest doing this process from php and sql storing accounts type a and b

<select name="cuenta" id="tipo" >
   <option selected value="0"> Tipo de cuenta </option> 
       <option valor="A">A</option> 
       <option valor="B">B</option> 
</select>
    <?php
$cuenta=$_POST['cuenta']
if($cuenta === a) {
    header('location:cuenta_a.php');
}
if($cuenta === b) {
    header('location:cuenta_b.php');
}
?>

tell us how you did

    
answered by 12.11.2017 в 17:05
0

It's easy to do with Jquery, here's the example:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <title>Document</title>
</head>

<body>
    <select name="cuenta" id="tipo">
        <option selected value="0"> Tipo de cuenta </option>
        <option valor="A">A</option>
        <option valor="B">B</option>
    </select>

    <p id="demo"></p>

    <script>
        function elegir() {
            var valor = $('#tipo').val();
            if (valor == "A") {
                window.location.href = "/web1.php";
            } else if (valor == "B") {
                window.location.href = "/web2.php";
            }
        }
    </script>
    <button onclick="elegir()">Crear</button>

</body>

</html>

Here you see it running:

function elegir(){
      var valor = $('#tipo').val();
      if (valor == "A") {
        location.href = "http://es.stackoverflow.com";
      }
      else if (valor == "B"){
        location.href = "https://google.com.co";
      }
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<select name="cuenta" id="tipo" >
         <option selected value="0"> Tipo de cuenta </option> 
         <option valor="A">A</option> 
         <option valor="B">B</option> 
</select>
<button onclick="elegir()">Crear</button>
  

Eye   You can use:

  • window.location.href - to go to a tab within your website.

  • location.href - to go to a different URL.

answered by 12.11.2017 в 17:00
0

modify your selection so that in each option it contains the link to which the user should be directed according to the type of account, as follows:

<select name="cuenta" id="tipo" onchange="location = this.value;">
   <option selected='' value="0"> Tipo de cuenta </option> 
       <option value="/user/A">A</option> 
       <option value="/user/B">B</option> 
</select>

so you do not need to use javascript.

    
answered by 12.11.2017 в 17:30