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

时间:2013-11-25 13:07:52

标签: php 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="">
    <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>

一切正常......除了从文本框中获取价值。任何人都可以帮我如何获取文本框值并插入数据库..

5 个答案:

答案 0 :(得分:1)

您的html页面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页面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\n", 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();
   }?>

答案 1 :(得分:0)

您可以在php代码中使用if语句,如下所示:

<?php

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

?>

然后你可以像往常一样将$ type插入数据库,显然要小心正确验证和转义数据。

答案 2 :(得分:0)

您可以将值视为

         firsrt check 

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

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


                 }
       else
          {
             echo "post ! set";
           }

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

我假设您将表单发布到另一个页面,并使用MySQL。

您的表单应该有action="page.php" Page.php作为您提交的页面。

这是page.php可能是:

<?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.


?>

另外,您需要阻止SQL注入,这里有一个链接,可以帮助您开始使用它。 http://www.veracode.com/security/sql-injection

您希望确保在将数据插入数据库之前进行验证,清理,检查错误等。如果有人试图攻击您的数据库,则不这样做可能会导致严重问题。

此外,您可能希望将数据库连接变量设置在另一个文件中并包含它,这样您的信息就会更加安全。