title: How to insert value of input text value when radio button is checked in PHP?
body: <p>I creating a short question form and I want to insert the value of input text when the radio button is checked. Currently I can only insert the value of radio button but not the value of my input text.
  How can I do this ?</p>
  
  <p>HTML code:-</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;link rel="stylesheet" type="text/css" href="Assets/index_style.css" /&gt;
      &lt;link rel="stylesheet"  href="bootstrap/css/bootstrap.css" /&gt;
      &lt;link href="https://fonts.googleapis.com/css?family=Comfortaa" rel="stylesheet"&gt;
      &lt;script src="Assets/jquery-1.11.3-jquery.min.js"&gt;&lt;/script&gt;
  
      &lt;script type="text/javascript" src="//code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
  
      &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
      &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"&gt;
      &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"&gt;&lt;/script&gt;
      &lt;script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"&gt;&lt;/script&gt;
  
      &lt;script&gt;
          //back button function
          function goBack() {
              window.history.back();
          }
      &lt;/script&gt;
  
  
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;div class="container"&gt;
      &lt;div class="header clearfix"&gt;
          &lt;nav&gt;
              &lt;ul class="nav nav-pills pull-right"&gt;
                  &lt;li role="presentation" class="active"&gt;&lt;a href="settings.php"&gt;Settings&lt;/a&gt;&lt;/li&gt;
                  &lt;li role="presentation"&gt;&lt;a href="logout.php"&gt;Logout&lt;/a&gt;&lt;/li&gt;
              &lt;/ul&gt;
          &lt;/nav&gt;
      &lt;/div&gt; &lt;!-- /container --&gt;
  
      &lt;div class="container"&gt;
  
          &lt;button type="button" class="btn btn-success" onclick="window.location.href='admin_home.php'"&gt;
              &lt;span class="glyphicon glyphicon-arrow-left"&gt;&lt;/span&gt; Back to home
          &lt;/button&gt;&lt;br&gt;
  
          &lt;h3 class="text-muted"&gt;Short Question Form&lt;/h3&gt;
  
          &lt;form action="admin_shortquestform.php" method="post"&gt;
  
              &lt;table class="table"&gt;
  
                  &lt;tbody&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Question:&lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" size="70" name="questiontext"&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;1. &lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" size="60" name="ans1"&gt;&lt;input type="radio" name="radioans" value="1" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;2. &lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" size="60" name="ans2"&gt;&lt;input type="radio" name="radioans" value="2" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;3. &lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" size="60" name="ans3"&gt;&lt;input type="radio" name="radioans" value="3" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;4. &lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" size="60" name="ans4"&gt;&lt;input type="radio" name="radioans" value="4" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td align="center"&gt;&lt;input type="submit" name="submit" value="Create"&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;/tbody&gt;
  
              &lt;/table&gt;&lt;/form&gt;
      &lt;/div&gt;
  
      &lt;footer class="footer"&gt;
          &lt;p&gt;&amp;copy; 2016 FYP, Inc.&lt;/p&gt;
      &lt;/footer&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>PHP code:-</p>
  
  <pre><code>&lt;?php
  
  session_start();
  
  require_once 'dbConn.php';
  
  if(!empty($_POST{'submit'})) {
  
  
      $questiontext = $_POST['questiontext'];
      $anstext1 = $_POST['ans1'];
      $anstext2 = $_POST['ans2'];
      $anstext3 = $_POST['ans3'];
      $anstext4 = $_POST['ans4'];
  
  
      if(empty($questiontext) &amp;&amp; empty($anstext1) &amp;&amp; empty($anstext2) &amp;&amp; empty($anstext3) &amp;&amp; empty($anstext4)) {
          echo "Please enter data";
          echo "&lt;script&gt;setTimeout(\"location.href = 'admin_mcqform.php';\",2000);&lt;/script&gt;";
          return false;
  
      }
      if(!isset($_POST['radioans']))
      {
          echo "Please enter data";
          echo "&lt;script&gt;setTimeout(\"location.href = 'admin_mcqform.php';\",2000);&lt;/script&gt;";
          return false;
      }
  
      $radiobtn = $_POST['radioans'];
      try {
  
  
          //add the first record into question table
          $stmt1 = $conn-&gt;prepare("INSERT INTO `question`(question_text, question_type) VALUES(:questiontext, 'shortquestion')");
          $stmt1-&gt;bindParam(":questiontext",$questiontext);
          $stmt1-&gt;execute();
  
          //add the last id insert for question into option tbl question_id
          $questionID = $conn-&gt;lastInsertId();
          $answerID = $conn-&gt;lastInsertId();
  
          //add second record into option_tbl
          $stmt2 = $conn-&gt;prepare("INSERT INTO `option_tbl`(option_answer,question_id) VALUES(:ans,$questionID)");
  
          $stmt2-&gt;bindParam(":ans",$anstext1);
          $stmt2-&gt;execute();
          $stmt2-&gt;bindParam(":ans",$anstext2);
          $stmt2-&gt;execute();
          $stmt2-&gt;bindParam(":ans",$anstext3);
          $stmt2-&gt;execute();
          $stmt2-&gt;bindParam(":ans",$anstext4);
          $stmt2-&gt;execute();
  
          //add third record into answer table
          $stmt3 = $conn-&gt;prepare("INSERT INTO `answer`(answer_text,question_id) VALUES(:radioans,$answerID)");
          $stmt3-&gt;bindParam(":radioans",$radiobtn);
          $stmt3-&gt;execute();
  
          header('Location: '.$_SERVER['PHP_SELF']);
          exit;
  
      }
      catch (PDOException $e) {
          echo $e-&gt;getMessage();
      }
  
  }
  
  ?&gt;
  </code></pre>