如何获取基于用户选择创建的文本框的值

I have a drop down where the users's select the types and when the user clicks on other in the drop down I have to display a text box that was done..

But now How can I get the value of the text box and insert into the database.

Here is my script which displays the text box when user select's others option.

<script type="text/javascript">
    function CheckColors(val) {
        var element = document.getElementById('others');
        if (val == 'others') element.style.display = 'block';
        else element.style.display = 'none';
    }
</script> 

<form name="f1" method="POST" action="">
    <select name="type" onchange='CheckColors(this.value);'>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="others">others</option>
    </select>
    <input type="text" name="others" id="others" style='display:none' />
    <input type="submit" name="submit" />
</form>

Everything working fine.. Except getting value from text box. Can anybody help me how to get the textbox value and insert into the db..

your html page a.html

<script type="text/javascript">
    function CheckColors(val) {
        var element = document.getElementById('others');
        if (val == 'others') element.style.display = 'block';
        else element.style.display = 'none';
    }
</script> 

<form name="f1" method="POST" action="b.php">    <!--post data to b.php-->
    <select name="type" onchange='CheckColors(this.value);'>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="others">others</option>
    </select>
    <input type="text" name="others" id="others" style='display:none' />
    <input type="submit" name="submit" />
</form>

php page b.php

    <?php
      if(isset($_POST['submit']))
     {

        $selectType=$_POST['type']; 
        $inputText=$_POST['others'];

        $mysqli = new mysqli("localhost", "my_user", "my_password", "dbname");

       /* check connection */
      if (mysqli_connect_errno()) {
          printf("Connect failed: %s
", mysqli_connect_error());
          exit();
      }

      $sql="insert into tablename (keyType,keyOther) values(?,?)";

      /* create a prepared statement */
      if ($stmt = $mysqli->prepare($sql)) {

        /* bind parameters for markers */
        $stmt->bind_param("ds", $selectType,$inputText);

        /* execute query */
        $stmt->execute();

       /* close statement */
       $stmt->close();
     } 

     /* close connection */
     $mysqli->close();
   }?>

You can just use an if statement in your php code, like this:

<?php

if ($_POST['type'] == 'others') {
    $type = $_POST['others'];
} else {
    $type = $_POST['type'];
}

?>

Then you can insert $type into the database as you would normally, obviously being careful to correctly validate and escape the data.

you can take value as

         firsrt check 

           if(isset($_POST["submit"]))
              {
                $other = $_POST["other"];

                then fire your sql insert query by making connection to database.


                 }
       else
          {
             echo "post ! set";
           }
<?php
  if(isset($_POST['submit']))
 {

    $others=$_POST['others']; 
  }
 ?>

I assume you're posting the form to another page, and using MySQL.

Your form should have action="page.php" Page.php being the page your submitting to.

Here is what page.php could be:

<?php 

   $con=mysqli_connect("ipAddress","my_user","my_password","my_db");
   //your database connection

   $other = $_POST['others'];
   //The "others" is the name of the field your posting

   mysqli_query($con,"INSERT INTO... fieldname = '$other'");
   // your SQL query goes here.


?>

Also, you'll want to prevent SQL injection, here is a link to get you started with that. http://www.veracode.com/security/sql-injection

You want to make sure you validate, sanitize, check for errors, etc BEFORE you insert the data into your database. Failure to do so could cause serious problems if someone tries to attack your database.

Also, you may want to set your database connection variable in another file and include it, that way your info is a little more secure.