user7475343
user7475343

Reputation:

How to tie radio button with input text fields?

This is a sign up form for an employee. The employee has to check in a radio button if he is temporary or permament. If temporary he should fill the input contract number and if permament the hiring date. How can I bind the radio button with the input so that he cannot "cross-complete" , for example fill the hiring date if he is temporary. I want to enable him to fill out each input only if he has pressed that specific radio button.

php code :

<?php include 'dbconfig.php';?>
<?php header('Content-Type: text/html; charset=utf-8');?>
<!DOCTYPE HTML PUCLIC "-//W3C//DTDHTML
4.0 Transitional//EN"><HTML>
  <HEAD>
      <link rel="stylesheet" type="text/css" href="logintest.css">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </HEAD>
  <button class="btn" TYPE="submit" name="goback" onclick="window.location.href='login.php'">Go Back </button>

      <?php
      error_reporting(E_ALL); ini_set('display_errors', 1);
      if(isset($_POST['submit']))
      {
        $sql = "INSERT INTO employee (empID,EFirst,ELast,username, passcode)
        VALUES ('".$_POST["empID"]."','".$_POST["EFirst"]."','".$_POST["ELast"]."','".$_POST["username"]."','".$_POST["passcode"]."')";

        $result = mysqli_query($conn,$sql);
        $answer=$_POST['kind'];
        if($answer='permament'){
          $sql1 = "INSERT INTO permament_employee (empID,Hiring_Date) VALUES ('".$_POST["empID"]."','".$_POST["date"]."')";
          $result1 = mysqli_query($conn,$sql1);
        }

        if ($answer='temporary'){
          $sql2= "INSERT INTO temporary_employee(empID) VALUES ('".$_POST["empID"]."')";
          $result2 = mysqli_query($conn,$sql2);
        }
        echo "<script> location.replace('login.php') </script>";
      }
      ?>

    <FORM METHOD="post" ACTION="">
        <div class="input-group">
        <label>id</label>
           <INPUT TYPE="text" name="empID" SIZE="30" required>
        </div>
        <div class="input-group">
           <label>First Name</label>
           <INPUT TYPE="text" name="EFirst" SIZE="30" required>
        </div>
        <div class="input-group">
           <label>Last Name</label>
           <INPUT TYPE="text" name="ELast" SIZE="30" required>
        </div>
        <div class="input-group">
            <label>username</label>
            <INPUT TYPE="text" name="username" SIZE="30" required>  
        </div> 
        <div class="input-group">
             <label>password</label>
             <INPUT TYPE="password" name="passcode" SIZE="30" required>
        </div>   
        <div class="input-group">
          <div class="some-class">
          <label> Permament  <input type="radio" name="kind" value="permament" id="permament" required>  <br>   <input type="date" name="date" value="date" id="date" required>  <br> </label>
          <label> Temporary  <input type="radio" name="kind" value="temporary" id="temporary" required>  <br>  <input type="number" name="ContractNr" value="ContractNr" id="ContractNr" placeholder="Contract Number" required>  <br>     </label>
          </div>
        </div>
        <br>
        <br>
        <br>
        <br>

        <button class="btn" TYPE="submit" name="submit">Submit Info </button>
        <button class="btn" TYPE="reset" name="Reset">Reset </button>
    </FORM>

</HTML>

Upvotes: 1

Views: 474

Answers (2)

LewBlu
LewBlu

Reputation: 240

If you are using jQuery you could add the following :

<script>
    $(‘input[name=“kind”]’).change(function(){
        if($(this).val() == ‘temporary’){
            $(‘input[name=“date”]).hide();
            $(‘input[name=“ContractNr”]).show();
        }else{
            $(‘input[name=“date”]).show();
            $(‘input[name=“ContractNr”]).hide();
        }
    });
</script>

Or if you just want to disable :

<script>
    $(‘input[name=“kind”]’).change(function(){
        if($(this).val() == ‘temporary’){
            $(‘input[name=“date”]).prop(‘disabled’, true);
            $(‘input[name=“ContractNr”]).prop(‘disabled’, false);
        }else{
            $(‘input[name=“date”]).prop(‘disabled’, false);
            $(‘input[name=“ContractNr”]).prop(‘disabled’, true);
        }
    });
</script>

Upvotes: 1

fred727
fred727

Reputation: 2819

You need javascript. You have to bind a 'change' event on your radio buttons, and set visibility of your inputs according to radio button selected value.

Upvotes: 1

Related Questions